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 duplicate CSS in dev mode #4157

Merged
merged 4 commits into from
Aug 5, 2022
Merged

Fix duplicate CSS in dev mode #4157

merged 4 commits into from
Aug 5, 2022

Conversation

natemoo-re
Copy link
Member

Changes

  • Closes 🐛 BUG: CSS is duplicated in dev server. #4102
  • During dev, Vite treats all .css files as JS modules that inject themselves into the page.
  • This is great for HMR, but it leads to flashes of unstyled content during astro dev.
  • To avoid this, Astro started injecting the static CSS while server-side rendering the file. This avoids any flashes of unstyled content, but when the CSS is injected for HMR, it leads to duplicate styles.
  • This PR adds a tiny MutationObserver to our client/hmr logic that removes any SSR'd styles as soon as the HMR styles are present on the client. No more duplicates!
  • Also removed the data-astro-injected attribute because it's no longer needed!

Testing

Tests using data-astro-injected updated. HMR already covered by existing tests.

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Aug 4, 2022

🦋 Changeset detected

Latest commit: 29baf0c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
astro Patch
@e2e/astro-component Patch
@e2e/error-react-spectrum Patch
@e2e/error-sass Patch
@e2e/errors Patch
@e2e/lit-component Patch
@e2e/preact-component Patch
@e2e/react-component Patch
@e2e/solid-component Patch
@e2e/svelte-component Patch
@e2e/e2e-tailwindcss Patch
@e2e/ts-resolution Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Aug 4, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2022

⚖️ Bundle Size Check

Latest commit: 29baf0c

File Old Size New Size Change
hmr 479 B 712 B + 233 B

Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM. I'd love to see a simpler solution if you think one exists, but only if it's a light lift. Otherwise, this is more than enough heading into a great v1.0 release!

@natemoo-re natemoo-re merged commit 0257438 into main Aug 5, 2022
@natemoo-re natemoo-re deleted the fix/hmr-css-dupe branch August 5, 2022 18:36
@astrobot-houston astrobot-houston mentioned this pull request Aug 5, 2022
@ChrisSki
Copy link

ChrisSki commented Feb 1, 2023

@natemoo-re Any chance this made it's way back into dev mode? I'm experiencing duplicate css when using the following in an astro file:

<style lang="scss">
  @import "./BlogPost.scss";
</style>

@benmardana
Copy link

I'm also seeing this when importing scss modules into React components. E.g.

import styles from './navigation.module.scss';

They appear to be duplicated between inline and the original file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: CSS is duplicated in dev server.
5 participants