-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix: check the existence of "document" before using it #15262
Conversation
When loading the client in a worker "document" is not available and should not be used.
Run & review this pull request in StackBlitz Codeflow. |
You have large hurdles for a simple pull request, guys. To run prettier I have to install pnpm (which I don't use). I'm not ready to start using it. Can you suggest a different approach without having to do many changes in the repo? btw. I checked manually that everything in my patch is using the same style as the code around it (2 spaces indentation, no trailing semicolon). What else could be wrong? |
Can you explain how and why the |
This issue is related to #12988 and #9879, so this problem is well known, which makes me scratching my head why this hasn't been fixed months ago. The first linked issue (actually a PR) fixes one place without touching the others. From what I understood (I have never worked in the vite code base before) the issue stems from the fact that I should add that this issue is related to HMR. Otherwise there's no problem. I have a dozen web workers running in my project and when HMR happens I get a stream of errors about |
#12988 was a quick fix as we only then discovered that I assume in your workers code you imported things that had HMR, and that indirectly brought it vite/packages/vite/src/client/client.ts Lines 19 to 71 in 3e42611
But I might need some thoughts from other maintainers about this. With this PR's changes instead, maybe we can get HMR to work in workers, but somewhat only partially and that could be nice to have. |
I see the errors also when applying changes to code which is definitely not used in web workers (e.g. preact stuff). I wouldn't change the entire worker handling in vite. It's working well so far. Only these errors are a nuisance. And checking something that might not be available before using it is always a good thing. |
I investigated a similar issue in the past #15036 (comment), so I just wanted to chime in. Sorry if it's a different a topic. I think there's a one pitfall when using Vite Preact plugin (compared to React plugin), which is that Preact plugin applies hmr-related transform on plain js/ts files by default, but on the other hand, react/babel plugin only applies on jsx/tsx files. (Actually there seems to be one check to skip In addition to this, Preact seems to apply transform to class component (e.g. So, the difference of the reproduction condition is:
I created a quick repro here:
I checked plugin's transform from a devtool network tab. For example, you can see part of transformed codeexport class CapitalClass {
something() {}
}
_c = CapitalClass;
var _c;
$RefreshReg$(_c, "CapitalClass");
if (import.meta.hot) {
self.$RefreshReg$ = prevRefreshReg;
self.$RefreshSig$ = prevRefreshSig;
import.meta.hot.accept((m)=>{
try {
flushUpdates();
} catch (e) {
self.location.reload();
}
}
);
} One quick workaround for Preact users could be to explicitly set a plugin option like
I think that might also help Audioworklet users who somehow unintentionally ends up with |
I can confirm @hi-ogawa feedback. I saw multiple people having issues with that by having React component imported in a worker, which was often not wanted and caused by barrel exports. Having I prefer to not add to many edge case for this so that if someone find a way to get HMR working in worker we don't have to deal with people having currently half-working injected client |
@ArnaudBarre what is a "barrel export"? Never heard this term before. And what do you actually mean by "I prefer to not add many edge case for this"? Are you referring to fixes for this problem? Or considering cases like this one (which doesn't seem to be an edge case, from what I read so far). Do you prefer not to fix this issue and annoy people until sometimes in the future someone will get something else to work? And may I remind you, this PR is not about preact code! I especially took care not to include any heavy code except what the worker is specifically for (text parsing). |
See https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/ for a look at this and their various issue What I mean is that don't like adding more code that will result in no-op code running in the worker and that would make it harder to deal with latter on. The code is clean, it's just fixing the issue at the last step instead of either:
|
Thanks for the link. An interesting read, for sure! I understand your concern about adding code now that may be obsolete later. But what are the other options? Live with the current behavior until the HMR code is changed to avoid this situation? As we all know it's difficult to predict a timeframe for something to happen, especially in projects which are run by volunteers. So it might take some time until this change to happen. All a matter of priorities, of course. My opinion: Grab the low hanging fruit now and remove the checks, once the HMR issue is resolved. |
This could be a breaking change in the future because people would have start to have code that rely on the fact that HMR code in a worker is no-op and it's impossible to be sure how adding it will affect usages This is a tradeoff, so we need a good reason to take the risk of migrating back, and I don't see a clear example where the issue cannot be solved by having a cleaner split between worker code and UI code (which often will lead to smaller worker bundle which is good) |
Closing as this change seems to be covered by #16318 |
Description
When loading the client in a worker "document" is not available and should not be used.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).