Unverified Commit 44fbc24c authored by Michael Albertz's avatar Michael Albertz Committed by GitHub

[Example] Add with-web-worker example (#13444)

Resolves: https://github.com/zeit/next.js/issues/12545

This PR adds an example which show how to bundle and use WebWorkers in a next.js app.

The issue mentions Typescript, but adding typescript to the example should be fairly simple by following the documentation and seems out of scope for a web-worker example.
parent c64cb60a
# WebWorker example
This example shows how to run code in WebWorkers by utilizing [worker-plugin](https://github.com/GoogleChromeLabs/worker-plugin).
## Deploy your own
Deploy the example using [Vercel](https://vercel.com/):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-web-worker)
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npm init next-app --example with-web-worker with-web-worker-app
# or
yarn create next-app --example with-web-worker with-web-worker-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-web-worker
cd with-web-worker
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
const WorkerPlugin = require('worker-plugin')
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
if (!isServer) {
config.plugins.push(
new WorkerPlugin({
// use "self" as the global object when receiving hot updates.
globalObject: 'self',
})
)
}
return config
},
}
{
"name": "with-web-worker",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "16.13.1",
"react-dom": "16.13.1",
"worker-plugin": "^4.0.3"
}
}
import { useEffect, useRef, useCallback } from 'react'
export default function Index() {
const workerRef = useRef()
useEffect(() => {
workerRef.current = new Worker('../worker.js', { type: 'module' })
workerRef.current.onmessage = (evt) =>
alert(`WebWorker Response => ${evt.data}`)
return () => {
workerRef.current.terminate()
}
}, [])
const handleWork = useCallback(async () => {
workerRef.current.postMessage(100000)
}, [])
return (
<div>
<p>Do work in a WebWorker!</p>
<button onClick={handleWork}>Calculate PI</button>
</div>
)
}
// https://stackoverflow.com/a/39575124
export default function pi(n) {
var v = 0
for (let i = 1; i <= n; i += 4) {
// increment by 4
v += 1 / i - 1 / (i + 2) // add the value of the series
}
return 4 * v // apply the factor at last
}
// This is a module worker, so we can use imports (in the browser too!)
import pi from './utils/pi'
addEventListener('message', (event) => {
postMessage(pi(event.data))
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment