GoogleChromeLabs / worker-plugin

👩‍🏭 Adds native Web Worker bundling support to Webpack.
https://npm.im/worker-plugin
Apache License 2.0
1.91k stars 79 forks source link

Question: New Worker Inside Loop Continuously Requests Script? #72

Closed jfuehner closed 4 years ago

jfuehner commented 4 years ago

Curious if there is a more appropriate way to use the worker-plugin inside a loop? What I'm seeing is on every iteration the browser reloads the ./worker.js file on each new Worker...

Say I have something like inside a loop...

     const worker = new Worker('./worker.js', { type: 'module' });
     const wrapper = Comlink.wrap(worker);
     const proxy = await new proxy();

    results.push(await proxy.data);     

    await proxy[Comlink.releaseProxy]();
    await worker.terminate();

Having the browser continuously request the same ./worker.js file seems inappropriate. Ideally I would request the script once and be able to simply create new/multiple workers with the same script. I'm sure I am doing something wrong...

developit commented 4 years ago

The script is only re-requested if your headers require it to be, same as any HTTP resource. If you're testing this with DevTools open, make sure you don't have caching disabled otherwise any usage of a resource will trigger a re-fetch.

developit commented 4 years ago

Closing this out since it's likely working-as-designed (caching fixes it, use caching).