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
HMR is not working for web workers #14722
Comments
Yep, the same webpack/webpack-dev-server#3794 |
@alexander-akait should we track issue in webpack repo? |
No, it should be implemented on webpack-dev-server side |
This issue had no activity for at least three months. It's subject to automatic issue closing if there is no activity in the next 15 days. |
I'm also having a use-case where I would like to use hot reloading in a worker thread. I'm using a custom react renderer in a worker to perform heavy computations. Reloading the page every time in development is becoming a bit of a deal breaker for that use case. I have spent a lot of time researching this topic and it looks like this is not supported in Webpack?! @alexander-akait (hope you don't mind me asking directly), do you have any tips / hints if / how this could be done? I would be happy to create a minimal reproducer (using a dummy react renderer in a worker?!), if you're interested. |
@donalffons yes, it will be useful, I will investigate it deeply |
I greatly appreciate that! Thanks for your work on this! Fwiw, I have found this PR for vite, which aims to implement HMR for workers. This does seem to work and provides the For my specific use case & webpack, I wonder if it would be possible to
@alexander-akait, does this sound like a reasonable approach or am I missing something? |
In theory you can multi compiler mode, so you don't need to manually start, also you can set But yes, approach sounds good |
@alexander-akait Just wanted to quickly thank you (your suggestion to use multi compiler mode was very helpful) and share my learnings here. I was able to make HMR and react-refresh work nicely with my custom React renderer (at least I haven't noticed any issues yet 😉). The setup is fairly simple and updates work as expected and preserve the application state. I did not have to make any modifications to webpack or @pmmmwh/react-refresh-webpack-plugin (they don't rely on
// ...
entry: {
- main: './src/index.tsx',
+ worker: './src/worker/index.tsx',
},
+ target: "webworker",
plugins: [
isDevelopment && new ReactRefreshPlugin(),
new ForkTsCheckerWebpackPlugin(),
- new HtmlWebpackPlugin({
- filename: './index.html',
- template: './public/index.html',
- }),
].filter(Boolean),
// ...
import webpack from 'webpack';
import config from './webpack.config.js';
import configWorker from './webpack.config.worker.js';
import WebpackDevServer from 'webpack-dev-server';
const compiler = webpack([configWorker, config]);
const devServer = new WebpackDevServer({}, compiler);
devServer.start();
const w = new Worker("/worker.js") After that, I can (This was much less painful than I anticipated 🙂) |
hm, sounds like a bug in webpack-dev-server too, dev server assumes we have window's context and try to reload, but it is wrong 😕 can you provide small example with original configuration too, I would like to to look |
Here is a simple example that reproduces the error I had: ocjsx-hello-world-webpack-issue.zip @alexander-akait To reproduce:
I'm ~80% happy with this proof of concept. What's missing for me would be the possibility to ship the custom react renderer (i.e. ~ src/worker/index.tsx) as an npm package that can be used with user-defined models (src/worker/Model.tsx). Currently, /src/worker/index.tsx statically imports src/worker/Model.tsx, which wouldn't work in that case. I'm not sure what would be the best way of doing that - maybe by writing some sort of Webpack Plugin? |
Yep, bug, we should fix it, if you want to help feel free to send a PR
Yep, a good question, ideally you need to pass path to worker as argument (you will faced with the same problem not only for webpack, for any other bundlers), multi compiler mode can be useful here too |
Will do! |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
Bug report
What is the current behavior?
When updating the code loaded by web worker nothing happens.
If the current behavior is a bug, please provide the steps to reproduce.
This is a very simple configuration where HMR doesn't work:
https://github.com/wclr/webpack-worker-hot-reload
What is the expected behavior?
I believe it should work the following way:
new URL
of the worker script was created).Other relevant information:
webpack version: 5
Node.js version: 14
Operating System:
Additional tools:
The text was updated successfully, but these errors were encountered: