-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cross-origin Web Workers still require worker-loader #16696
Comments
In Webpack5 We need a universal solution!! Is there a god who knows? |
How do I load web workers across domains in Webpack5? Does Webpack5 support inline workers? |
Sorry for delay, we don't create blob by default because it will increase your bundle, you need to create Blob on application side and handle it there, I provide solutions multiple times with different examples and how to handle them, look at dicussions, worker-loader is depcreated and should not be used anymore. For worker public path we have #16671 and it will be in the next release.
It should be handled and configured on server side, yes, you can setup proxy for such purposes. Sorry but we can't do something here. Blob is good approach only for old browsers as a fallback. Also you provided https://medium.com/@krishnachirumamilla/content-security-policy-worker-src-cd06ecfa2fe8, and there is solution, if you need such things you can use multi compiler mode and set feel free to feedback If you need I can write you configuration to you but then I need an repository with your real problem, no need to provide whole code, anyway I strongly recommend to look at multi compoer mode with |
Can you please point out the proper one. There are too many different discussions and advices, it would be great to get the right one from you, since you're the expert.
Will it work same way as
Author of the article says: "Browsers don’t allow you to create a worker with a URL pointing to a different domain.". Chrome ignores the I have tried I have used following technique: const myWorker = function createWorker(workerUrl) {
const blob = new Blob([`importScripts('${workerUrl}');`], {'type': 'application/javascript'});
return new Worker(URL.createObjectURL(blob));
}; Here's the repo: https://github.com/kirill-konshin/cross-domain-worker/tree/target-webworker (note the branch Unfortunately blob worker throws error when adding
The whole code has been provided: https://github.com/kirill-konshin/cross-domain-worker/tree/original-issue, along with comments in the code and here. |
Looks like I've solved most of the issues, the trick was to explicitly supply a true worker path as What's supported:
https://github.com/kirill-konshin/cross-domain-worker/blob/main/packages/cross-domain-worker/src/index.js I have published the NPM package https://github.com/kirill-konshin/cross-domain-worker/blob/main/packages/host/src/index.js usage in main thread. https://github.com/kirill-konshin/cross-domain-worker/blob/main/packages/worker/src/worker.js usage in worker. |
Hello, sorry for delay, I see you have solved everything, my solution is similar (and it's somewhere here in the questions 😄), we also merge a PR to allow to public path for workers (so Do you help help with something else? Also I think we need document this |
The problem persists imho if we want to use this in combination with webpack transpiling of the We are moving from a solution:
where webpack is able to transpile and output the js file, then modify here and replace the URL with the correct filePath, to this (using
that doesn't work as expected, unless I'm missing some other configuration anywhere else. |
I ended up using fetch instead: export class CorsWorker {
private readonly url: string | URL;
private readonly options?: WorkerOptions;
// Have this only to trick Webpack into properly transpiling
// the url address from the component which uses it
constructor(url: string | URL, options?: WorkerOptions) {
this.url = url;
this.options = options;
}
async createWorker(): Promise<Worker> {
const f = await fetch(this.url);
const t = await f.text();
const b = new Blob([t], {
type: 'application/javascript',
});
const url = URL.createObjectURL(b);
const worker = new Worker(url, this.options);
return worker;
}
} and then import { CorsWorker as Worker } from './cors-worker';
// aliasing CorsWorker to Worker makes it statically analyzable
const corsWorker = await (new Worker(
new URL('../workers/my-worker.worker', import.meta.url),
{ type: 'module', name: 'my-worker' },
)).createWorker(); |
Bug report
What is the current behavior?
It's known that Web Workers must be served from same origin as the page itself. Seems that even
Content-Security-Policy: worker-src http://xxx
has no effect on this.We can use
worker-loader
to create a blob instead of a worker file.BUT documentation https://webpack.js.org/loaders/worker-loader/ says:
DEPRECATED for v5: https://webpack.js.org/guides/web-workers/
The problem is, despite deprecation, this is the only way for now to overcome cross origin issues, documentation does not say anything about cross domain.
There are various solutions, but none of them work properly with Webpack...
import
statements etc.)If the current behavior is a bug, please provide the steps to reproduce.
I have put together the repo: https://github.com/kirill-konshin/cross-domain-worker/tree/original-issue, clone, run
npm install
andnpm start
, then just openhttp://localhost:3000
and check console. The demo exhibits following:2 servers, static HTML on
:3000
and Webpack Dev Server on:4001
.Page served from
:3000
:JS served from
:4001
:If you open the
http://localhost:3000
, you'll get:Uncaught DOMException: Failed to construct 'Worker': Script at 'http://localhost:4000/test/src_worker_js.js' cannot be accessed from origin 'http://localhost:3000'.
Documentation does not suggest any solution for this.
What is the expected behavior?
Two options:
worker-loader
and provide documentation when to use it, and hopefully fix the additional issueAdditional issue
If 2nd approach will be taken, I'd like to report an additional problem, but the loader repo is archived, so I'll put it here, with the workaround.
Since
worker-loader
will create a blob, it has no access to proper__webpack_public_path__
, in worker it will beblob:http://localhost:3000/
, e.g. host origin, so worker loaded this way won't have access to assets.I've assembled a short demo, where I added
devServer.proxy
which can serve same content from/test/
and/
to mimic how content can be served from different CDNs and paths usingoutput.publicPath='auto'
.Solution is to
postMessage
proper__webpack_public_path__
from main thread, and then wrap all asset requests in the worker:Very awkward, but works well.
Hopefully others can either copy-paste the hack, or, fingers crossed,
worker-loader
can be revived and can provide some normal way to recover dynamic public paths.The text was updated successfully, but these errors were encountered: