Skip to content

Get critical CSS in dev from client env #13503

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

Merged
merged 1 commit into from
Apr 30, 2025

Conversation

markdalgleish
Copy link
Member

Fixes #13379

When we extract critical CSS during dev, we're currently doing this from the SSR environment, or a custom environment when using Vite's Environment API to support non-Node SSR environments. This seems to cause issues if the SSR environment differs from the client environment in terms of resolving CSS files. In the case of the linked issue above, our critical CSS extraction logic doesn't work for the virtual CSS modules that Pigment CSS introduces.

To fix this issue, we now request the same CSS that the browser does by passing the CSS URL to viteDevServer.transformRequest. The only down side of this approach is that it returns a string of JS code with the CSS embedded within it as a string (along with HMR code etc.). To work around this, we use Babel to parse the JS and find the value for the __vite__css variable.

Copy link

changeset-bot bot commented Apr 30, 2025

🦋 Changeset detected

Latest commit: a25eed8

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

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

Comment on lines -154 to -162
// We use a separate environment for loading the critical CSS during
// development. This is because "ssrLoadModule" isn't available if the "ssr"
// environment has been defined by another plugin (e.g.
// vite-plugin-cloudflare) as a custom Vite.DevEnvironment rather than a
// Vite.RunnableDevEnvironment:
// https://vite.dev/guide/api-environment-frameworks.html#runtime-agnostic-ssr
const CSS_DEV_HELPER_ENVIRONMENT_NAME =
"__react_router_css_dev_helper__" as const;
type CssDevHelperEnvironmentName = typeof CSS_DEV_HELPER_ENVIRONMENT_NAME;
Copy link
Member Author

Choose a reason for hiding this comment

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

Now that we're loading the CSS from the client environment, we don't need to introduce a custom Vite.RunnableDevEnvironment anymore. This was only introduced because we were originally importing the CSS from the SSR environment, but when using vite-plugin-cloudflare, the SSR environment was a custom Vite.DevEnvironment that couldn't be imported from. With our new approach in this PR, this distinction doesn't matter.

@markdalgleish markdalgleish merged commit dd520b0 into dev Apr 30, 2025
8 checks passed
@markdalgleish markdalgleish deleted the markdalgleish/get-critical-css-from-client-env branch April 30, 2025 06:20
Copy link
Contributor

github-actions bot commented May 8, 2025

🤖 Hello there,

We just published version 7.6.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 7.6.1 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant