Workaround for Cross Origin Web Workers when using Asset Prefix #29415
Replies: 4 comments 8 replies
-
Any solution for this? i also facing the same problem here. Using the blob solution not working. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Here is a solution: It patches WebWorkers to use import "remote-web-worker"; Check the code (~300bytes) here Just make sure that the patch runs before you start using a worker. |
Beta Was this translation helpful? Give feedback.
-
I recently encountered an issue while working with web workers in Next.js, especially when dealing with different asset prefixes. I wanted to share a solution that might help others facing a similar problem. Problem: Solution:
If you have a basic worker file, give it a .js extension and place it in the public folder. This way, it will be served from the same root, avoiding cross-origin issues. If you need to reuse functions/methods from your codebase inside your worker file, create a worker file with a .ts extension and set up the worker using Webpack 5 syntax. You can refer to Webpack's guide on Web Workers for more details.
It's essential to note that the assetPrefix configuration in your next.config.js file is equivalent to the publicPath configuration in Webpack 5. You can learn more about this in Webpack's documentation on publicPath and nextjs source code.
To address the cross-origin issue when using an assetPrefix value, you can override the assetPrefix path at runtime. Webpack 5 exposes a global variable called webpack_public_path. You can use this variable to dynamically set and reset the asset prefix for your worker. Here's how to do it:
` I hope this explanation helps others facing a similar challenge! Feel free to reach out if you have any questions or need further clarification. @TimoWilhelm If this fixes your problem, please mark it as an answer. |
Beta Was this translation helpful? Give feedback.
-
It works for me: Add the following code to the next.config.js:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Browsers will not allow you to create a worker with a URL pointing to a different domain. This creates an issue when using the Webpack 5 syntax for creating web workers
new URL('/webworker/sampleworker', import.meta.url)
in combination with theassetPrefix
key in the config pointing at a CDN origin, since it will try to load the worker script from the CDN origin instead.A common workaround is using a blob object URL with the script content, or using
importScripts()
to load the script in the worker context.I've tried using the following snipplet to load the worker file:
However, in this case the
webworker/sampleworker.ts
is not transpiled to JS, but rather served as a TypeScript file.Is there any way to get a reference to the transpiled worker script without defining a custom entry point in the webpack config?
Beta Was this translation helpful? Give feedback.
All reactions