Fix "window is not defined" when using HMR on worker #15859
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes an error message that I got while using HMR in webpack-dev-server with
target: "webworker"
. The error message can be reproduced with the example linked in this comment. @alexander-akait suggested to make a PR with a fix.This PR just checks
typeof window
before callingwindow.location.reload()
and changes the warning messages slightly when the page cannot be reloaded by webpack.What kind of change does this PR introduce?
bugfix.
Did you add tests for your changes?
I tried to, but then I realized that the file
/hot/dev-server.js
is not tested anywhere, yet. Setting up a test scenario from scratch seems like a very hard thing to do 😉.I tested this manually with the example above to make sure it works as intended.

Does this PR introduce a breaking change?
no.
What needs to be documented once your changes are merged?
nothing.
PS: Thank you all for your fantastic work with this project!