Web worker not working if base path is not on root / #17258
-
I have app that resides on /foo path. This seems to have broken import of web worker script. I tried to use both versions mentioned in here https://vitejs.dev/guide/features#import-with-constructors Using the ?sharedworker import approach works but the file is being imported as How do I mitigate this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Seems the solution was to use longer path to the script with the src prefix. That loads the file correctly with the URL method. But it screws the code somehow. I cannot figure out what is going on but it breaks boot files. Yeah, so just importing the script in any way breaks boot files. This is definitely related to the base path and vite not being able to properly resolve imports because in apps with / base this is not an issue(same file, same code in general). |
Beta Was this translation helpful? Give feedback.
-
So the solution was to use full url for the file.
This is a must if the app is not residing on root path/url / but rather in a sub-directory. But it also works if it does. PS: If you need to import a dependency into the worker, the import must be done as a module:
|
Beta Was this translation helpful? Give feedback.
So the solution was to use full url for the file.
new SharedWorker(new URL(window.location.origin + window.location.pathname + 'src/foo/bar/baz/worker.js'));
This is a must if the app is not residing on root path/url / but rather in a sub-directory. But it also works if it does.
PS: If you need to import a dependency into the worker, the import must be done as a module:
new SharedWorker(new URL(window.location.origin + window.location.pathname + 'src/foo/bar/baz/worker.js'), {type: "module"});