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

feat: reuse existing style elements in dev #12678

Merged
merged 1 commit into from
Apr 5, 2023

Conversation

cyco130
Copy link
Contributor

@cyco130 cyco130 commented Mar 31, 2023

Description

During dev, Vite injects imported CSS files' contents into the document inside style tags. When doing SSR, this causes FOUC until the JavaScript is loaded.

In Rakkas, I solve this by scanning the module graph for the route that is being rendered to discover which CSS files will be needed and I inject link elements to them into the HTML. Since Vite doesn't know about this, it goes on and adds its style tags anyway, causing duplicate styles. It becomes a problem when CSS files are HMRed: The original link doesn't get updated so some styles start clashing. So Rakkas has to remove the links from the document once the styles are loaded. Most other Vite-based frameworks must be doing something similar.

Although it works, it would be much cleaner if we inserted style tags just like Vite does and Vite picked them up at startup. This PR implements this.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Mar 31, 2023

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

bluwy
bluwy previously approved these changes Mar 31, 2023
@bluwy bluwy added feat: css p2-nice-to-have Not breaking anything but nice to have (priority) feat: ssr labels Mar 31, 2023
Comment on lines +336 to +341
document.querySelectorAll('style[data-vite-dev-id]').forEach((el) => {
sheetsMap.set(el.getAttribute('data-vite-dev-id')!, el as HTMLStyleElement)
})
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 it would be good to add a comment here to help others understand this part. So we avoid the risk of someone deleting it as unneeded in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@patak-dev patak-dev merged commit 3a41bd8 into vitejs:main Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css feat: ssr 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.

3 participants