Skip to content
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: only run DOM operations in the client when there is a document (#17570) #18202

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

schontz
Copy link

@schontz schontz commented Sep 26, 2024

Description

Fixes #17570

Copy link

stackblitz bot commented Sep 26, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@schontz schontz changed the title Only run DOM operations in the client when there is a document (#17570) fix: Only run DOM operations in the client when there is a document (#17570) Sep 26, 2024
@schontz schontz changed the title fix: Only run DOM operations in the client when there is a document (#17570) fix: only run DOM operations in the client when there is a document (#17570) Sep 26, 2024
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a warning that tells importing CSS inside worker does not work.

Comment on lines +211 to +214
if (!hasDocument) {
return
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only change needed is this line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we only change this line? There are many DOM operations that will fail in workers, so it seemed best to make each one of them safe. Even if some may be safe today (e.g., first hasDocument check prevents others from running), if the code paths change at all they will throw errors as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's true. I actually forgot about this line.

`import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(

While I think adding hasDocument check everywhere feels error prone as well, I think it's fine for now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it is still somewhat error prone, and there are other latent bugs, such as location.reload(), which isn't available in workers either.

I suppose we could make the client more headless with hooks and guard all the "has DOM" things in one spot, but that would require some extra refactoring.

@sapphi-red sapphi-red added feat: css p2-nice-to-have Not breaking anything but nice to have (priority) feat: web workers labels Sep 30, 2024
@schontz
Copy link
Author

schontz commented Sep 30, 2024

I ran prettier and created another commit but the linter still fails. Not sure what to do.

@sapphi-red
Copy link
Member

I ran prettier and created another commit but the linter still fails. Not sure what to do.

Probably you need to run pnpm i before running pnpm format.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a warning that tells importing CSS inside worker does not work.

For dev, at this line

`__vite__updateStyle(__vite__id, __vite__css)`,

For build, at this line (when a CSS chunk is extracted and config.isWorker is true)

@schontz
Copy link
Author

schontz commented Oct 2, 2024

Do you mean we would display a warning? Or that the code should be skipped? I'm not sure how a warning would make sense, as there is no CSS in a worker, so it's not something that you would want to hear about not happening.

@sapphi-red
Copy link
Member

I mean displaying a warning. There's no CSS in a worker and the CSS import won't work, so I think a warning should be there. Why do you think the warning shouldn't be happening? (I am trying to understand your thoughts.)

@schontz
Copy link
Author

schontz commented Oct 3, 2024

The way I see it is that the client is just vite injected code that I have no control over as a developer. I am just trying to write code, and I don't even "care" that vite injects that code into my browser and worker modules.

The whole reason I started this PR is because I was getting errors in the console. It is not obvious that these are worker errors from dev tools. My thought is that adding other warnings would just cause confusion because seeing something like "CSS could not load in a worker" would not make sense because I am not trying to load CSS in a worker consciously. Maybe I happen to import a file that also imports CSS, but since CSS doesn't work in that context, it would just not be loaded.

In other words, what would I do with that warning, other than be slightly confused where it is coming from?

I would suggest that we keep each PR focused. This one fixes a bug: throwing uncaught errors in workers (which can cause real problems in workers). If there is a desire to add more warnings around things, or to refactor to be more worker-conscious, that could happen in a follow-up PR as a feature not a bug.

@sapphi-red
Copy link
Member

I agree that the client is not something that developers would / should care about. But the CSS import means appending that CSS into the document and if that is not desired the developer should remove that import. I think this is same situation when you have a code that depends on document (e.g. document.title = 'title'), in this case you have to remove that code. The warning will tell the developers to remove the CSS import.

If you want to trim down the scope much more, I'd say let's only changing the error message and don't ignore the error. If we're ignoring the developer's code error, IMO we should have a warning message. But I'll let others chime in.

@schontz
Copy link
Author

schontz commented Oct 4, 2024

I don't have enough insight into vite to make a call on this. All I know is that the error messages I fixed are ones that are showing up as a result of errors in the client and had nothing to do with my code. I started this PR because the client was trying to look for its own overlays, which causes errors. That is why I'm suggesting that we separate "don't let vite client break" from "find the right amount of warnings."

@schontz
Copy link
Author

schontz commented Oct 4, 2024

In other words, this addresses #17570. If we want to address warnings, I'd prefer that be a separate issue with a separate PR. I don't have the time to contribute more than this fix anyway, so either this goes stale and the bug persists, or there can be a separate issue on the back log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css feat: web workers p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document not defined in worker import
2 participants