Unverified Commit c46e99d3 authored by david's avatar david Committed by GitHub

fix with-web-worker example. (#23634)



## Documentation / Examples

- [x] Make sure the linting passes

***

Related to #23607

Instead of using a third-party loader `worker-plugin`, we leverage the use of `webpack 5` built-in worker support.

https://nextjs.org/docs/messages/webpack5
https://webpack.js.org/guides/web-workers/
parent be095765
# WebWorker example # WebWorker example
This example shows how to run code in WebWorkers by utilizing [worker-plugin](https://github.com/GoogleChromeLabs/worker-plugin). This example shows how to run code in WebWorkers by utilizing [webpack 5](https://nextjs.org/docs/messages/webpack5).
## Deploy your own ## Deploy your own
......
const WorkerPlugin = require('worker-plugin')
module.exports = { module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => { future: {
if (!isServer) { webpack5: true,
config.plugins.push(
new WorkerPlugin({
// use "self" as the global object when receiving hot updates.
globalObject: 'self',
})
)
}
return config
}, },
} }
...@@ -10,8 +10,7 @@ ...@@ -10,8 +10,7 @@
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "16.13.1", "react": "16.13.1",
"react-dom": "16.13.1", "react-dom": "16.13.1"
"worker-plugin": "^4.0.3"
}, },
"license": "MIT" "license": "MIT"
} }
...@@ -3,7 +3,7 @@ import { useEffect, useRef, useCallback } from 'react' ...@@ -3,7 +3,7 @@ import { useEffect, useRef, useCallback } from 'react'
export default function Index() { export default function Index() {
const workerRef = useRef() const workerRef = useRef()
useEffect(() => { useEffect(() => {
workerRef.current = new Worker('../worker.js', { type: 'module' }) workerRef.current = new Worker(new URL('../worker.js', import.meta.url))
workerRef.current.onmessage = (evt) => workerRef.current.onmessage = (evt) =>
alert(`WebWorker Response => ${evt.data}`) alert(`WebWorker Response => ${evt.data}`)
return () => { return () => {
......
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