Skip to content

TypeScript WebWorker scripts imported with new URL() get inlined as data:video/mp2t #11823

Description

@kulmajaba

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.4

Used Package Manager

npm

Logs

No response

Validations

Activity

  1. added
    p3-minor-bug
    on Jan 27, 2023
  2. K024 commented on Jan 28, 2023

    @K024

    Hi, @kulmajaba. When you write your code like

    workerFactory(new URL('./worker.ts', import.meta.url), { type: 'module' });

    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:

    import myWorkerUrl from './worker?worker&url'

    If you also like es module for your workers, you should also change this option.

  3. kulmajaba commented on Jan 28, 2023

    @kulmajaba
    Author

    @K024 The query suffix does work, so I suppose there are two separate problems at play:

    1. Documentation: importing WebWorkers with contructors using the URL constructor 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.
    2. The URL constructor transforming is inconsistent: setting the Worker constructor with URL constructor 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).
  4. roddc commented on Sep 19, 2023

    @roddc

    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:

    build: {
        ssr: true,
        sourcemap: 'inline',
        target: `node${node}`,
        outDir: 'dist',
        assetsDir: '.',
        minify: process.env.MODE !== 'development',
        lib: {
          entry: 'src/index.ts',
          formats: ['cjs'],
        },
        rollupOptions: {
          output: {
            entryFileNames: '[name].js',
          },
        
        },
        emptyOutDir: true,
        reportCompressedSize: false,
      },
    
  5. Mica4DEV commented on Mar 11, 2024

    @Mica4DEV

    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.

    Did you find any solution for this?

  6. rkh commented on Apr 9, 2024

    @rkh

    Hi, @kulmajaba. When you write your code like

    workerFactory(new URL('./worker.ts', import.meta.url), { type: 'module' });

    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:

    import myWorkerUrl from './worker?worker&url'

    If you also like es module for your workers, you should also change this option.

    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).

  7. rkh commented on Apr 10, 2024

    @rkh

    As a workaround, You can name the factory Worker or SharedWorker, and Vite will be happy.

    So this should work:

    const Worker = (workerScript: URL, workerOptions: WorkerOptions) => () => new globalThis.Worker(workerScript, workerOptions);
    
    const main = () => {
      const factory = Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
      const worker = factory();
    
      ...
    };

    Example of how I'm using this:

    const hasSharedWorker = (globalThis.SharedWorker as any) != (globalThis.Worker as any)
    const SharedWorker = hasSharedWorker ? globalThis.SharedWorker : Worker
    
    const worker = new SharedWorker(
      new URL("./worker.ts", import.meta.url),
      { type: "module", name: "pinto:back", credentials: "include" }
    )
    
    const port = hasSharedWorker ? (worker as SharedWorker).port : worker as Worker
  8. rkh commented on Apr 15, 2024

    @rkh

    Also note that Vite is also not adding a timestamp to the generated URLs in dev mode:

    import api from "/vite-dev/repository/api.ts?t=1712992006878";
    const worker = new SharedWorker(
      new URL('' + "/vite-dev/entrypoints/worker.ts?type=module&worker_file", import.meta.url),
      { type: "module", name: "pinto:back", credentials: "include" }
    );

    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.

  9. NikhilVerma commented on Oct 10, 2024

    @NikhilVerma

    This unfortunately does not help with workerpool as 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.

    // Vite has an issue where it thinks a TS file is a video (MPEG transport stream) unless you directly pass it to a worker
    // And we can't use the import worker syntax because it conflicts with Bun module resolution
    // So we trick it into think we are initialising a web worker
    class Worker {
    	url: URL;
    	opts: WorkerOptions;
    	constructor(url: URL, opts: WorkerOptions) {
    		this.url = url;
    		this.opts = opts;
    	}
    	getUrl() {
    		return this.url;
    	}
    }
    
    const tempWorker = new Worker(
    	new URL("./my-worker.ts", import.meta.url),
            {
    		type: "module"
    	}
    );
    
    const workerPool = workerpool.pool(tempWorker.getUrl().toString(), {
    	workerOpts: {
    		type: "module"
    	}
    });
  10. ckiee commented on Nov 6, 2024

    @ckiee

    Vite also does this with the regular import x from "~/worker?worker" syntax if ~/worker is a folder with an index.ts, which causes it to silently fail.

  11. dt-salvador-diaz commented on Feb 20, 2025

    @dt-salvador-diaz

    This unfortunately does not help with workerpool as 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.

    @NikhilVerma I think this should work:

    import myWorkerUrl from './my-worker.ts?worker&url';
    const workerPool = workerpool.pool(myWorkerUrl, { type: "module" }); 

    That's what I did to register my service worker:

    import myWorkerUrl from './my-worker.ts?worker&url';
    navigator.serviceWorker.register(myWorkerUrl, { type: "module" });
  12. danrossi commented on May 12, 2026

    @danrossi

    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.

  13. added a commit that references this issue on Jun 9, 2026
    85d3b29
  14. added
    p2-edge-case
    and removed
    p3-minor-bug
    on Jul 16, 2026
  15. added theissue type on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: web workershas workaroundp2-edge-case

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions