Repository navigation
Labels
Description
Describe the bug
With a vanilla TS project, using WebWorkers with the recommended URL syntax works fine:
const worker = new Worker(new URL('./worker.ts', import.meta.url))Building the project gives me two separate files, a main file and the worker file.
However, if the worker is created with e.g. a factory pattern and still using the URL syntax, some interesting things happen:
const workerFactory = (workerScript: URL, workerOptions: WorkerOptions) => () => new Worker(workerScript, workerOptions);
const main = () => {
const factory = workerFactory(new URL('./worker.ts', import.meta.url), { type: 'module' });
const worker = factory();
...
};Now, the worker is inlined as Base64, but the MIME type is wrong and set to data:video/mp2t (because .ts is a valid video file extension too).
The use case for a pattern like this is e.g. creating a Promisified version of the WebWorker interface and making it usable with any worker.
Reproduction
https://stackblitz.com/edit/vitejs-vite-8ewkf4?file=src/main.ts
Steps to reproduce
Run npm run build and check the bundled code.
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 7.17.0 - /usr/local/bin/npm
npmPackages:
vite: ^4.1.0-beta.1 => 4.1.0-beta.1
Originally encountered locally with Node 16.15.1 and vite: ^4.0.4 => 4.0.4Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Reacted by RichardJECooke, csha, Artyom Stepanishchev, IReallyHateCreatingUsernames, Luke Curley, Tjeerd Bakker, Ryan Christian, Micaël Félix, Hudson Newey, Mikael Finstad and 6 more
Activity
K024 commentedon Jan 28, 2023
Hi, @kulmajaba. When you write your code like
the URL is treated as an asset url, and will be transformed by the asset plugin in an unintended way.
The correct way to get the url of a worker can be found here:
If you also like es module for your workers, you should also change this option.
kulmajaba commentedon Jan 28, 2023
@K024 The query suffix does work, so I suppose there are two separate problems at play:
URLconstructor is the recommended way according to Vite docs, but there should be a mention that this will sometimes cause unintended behavior with more complex patterns.URLconstructor transforming is inconsistent: setting theWorkerconstructor withURLconstructor syntax into a variable bundles the worker separately, while using the constructor inside a function call causes the worker to be inlined (into an incorrect format).roddc commentedon Sep 19, 2023
I'm trying to import a worker script in my Electron app(main process).
Why did I get '/test-438b85ef.js' by
import MyWorker from './workers/test?worker&url';?The path is wrong and the worker file wasn't emitted in the dist directory.
Here is my vite config:
Mica4DEV commentedon Mar 11, 2024
Did you find any solution for this?
rkh commentedon Apr 9, 2024
This solution does not work for using a factory and thus doesn't actually work for SharedWorker with a Worker fallback (which in turn is required for Android support).
rkh commentedon Apr 10, 2024
As a workaround, You can name the factory
WorkerorSharedWorker,and Vite will be happy.So this should work:
Example of how I'm using this:
rkh commentedon Apr 15, 2024
Also note that Vite is also not adding a timestamp to the generated URLs in dev mode:
I'm not sure if this is intentional. I can see reasons for and against it.
If you include a timestamp, you might end up with two different shared workers running at the same time.
However, if you don't, you might end up with the old shared worker still running and no new worker starting up. This makes it impossible to write and easily test code that is supposed to handle this exact scenario in production.
This is possibly a new issue.
NikhilVerma commentedon Oct 10, 2024
This unfortunately does not help with
workerpoolas I need to have a URL for the worker. I found an extremely shitty workaround which works and tricks Vite into returning the right url.ckiee commentedon Nov 6, 2024
Vite also does this with the regular
import x from "~/worker?worker"syntax if~/workeris a folder with anindex.ts, which causes it to silently fail.dt-salvador-diaz commentedon Feb 20, 2025
@NikhilVerma I think this should work:
That's what I did to register my service worker:
danrossi commentedon May 12, 2026
I am trying to import an audio worklet and it also imports as data:video/mp2t;base64. The url param method doesn't work with audio worklets and conflicting documentation about that. There is loading errors as this data url. I needs to load the contents as a blob with a javascript mimetype. Unfortunately a worklet plugin doesn't work in the dev server mode as the plugin never gets the included source when loading dynamically.
fix(build): compile TypeScript workers referenced via new URL() in as…