Open pantoninho opened 1 week ago
I'm facing the same issue. I can confirm the repro example above is also producing the error on my machine.
EDIT:
When trying to construct the URL outside the useWorker
hook, a different error occurs: Refused to execute script from 'http://localhost:3000/_next/static/media/worker.de10a9a9.ts' because its MIME type ('video/mp2t') is not executable.
The page is rendered correctly, but the web worker does not seem to start (also can't find the worker script under 'sources' inside Chrome Dev Tools, as I think it should be?)
Code:
// this doesn't work
function Component() {
const worker = useWorker(new URL("./worker.ts", import.meta.url));
return <div/>
}
function useWorker(url) {
const workerRef = React.useRef();
React.useEffect(() => {
workerRef.current = new Worker(url); // <-- Error is thrown
return () => {
workerRef.current?.terminate();
};
}, [path]);
return workerRef;
}
Link to the code that reproduces this issue
https://github.com/pantoninho/nextjs-worker-bug
To Reproduce
(I'm getting errors creating a next-app with
reproduction-template
, so I created a regular next-app instead)I'm able to create web workers inside a component, but a
SecurityError
is thrown when instantiating them inside a hook.npm run dev
localhost:3000
send message
buttonlocalhost:3000/issue
(or click the link in the root page) and aSecurityError
is thrown (attached below)app/page.tsx
andapp/issue/page.tsx
:Current vs. Expected behavior
I expected to be able to create web workers inside hooks.
Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure, Runtime, Webpack
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local)
Additional context
No response