Skip to content

fix: clear critical css after initial render in HydratedRouter #13872

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-lobsters-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

In Framework Mode, clear critical CSS in development after initial render
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
- FilipJirsak
- focusotter
- foxscotch
- frodi-karlsson
- frontsideair
- fucancode
- fyzhu
Expand Down
2 changes: 0 additions & 2 deletions packages/react-router-dev/vite/static/refresh-utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ const routeUpdates = new Map();
window.__reactRouterRouteModuleUpdates = new Map();

import.meta.hot.on("react-router:hmr", async ({ route }) => {
window.__reactRouterClearCriticalCss();

if (route) {
routeUpdates.set(route.id, route);
}
Expand Down
14 changes: 7 additions & 7 deletions packages/react-router/lib/dom-export/hydrated-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,19 @@ export function HydratedRouter(props: HydratedRouterProps) {
});
}

// Critical CSS can become stale after code changes, e.g. styles might be
// removed from a component, but the styles will still be present in the
// server HTML. This allows our HMR logic to clear the critical CSS state.
// We only want to show critical CSS in dev for the initial server render to
// avoid a flash of unstyled content. Once the client-side JS kicks in, we can
// clear it to avoid duplicate styles.
let [criticalCss, setCriticalCss] = React.useState(
process.env.NODE_ENV === "development"
? ssrInfo?.context.criticalCss
: undefined
);
if (process.env.NODE_ENV === "development") {
if (ssrInfo) {
window.__reactRouterClearCriticalCss = () => setCriticalCss(undefined);
React.useEffect(() => {
if (process.env.NODE_ENV === "development") {
setCriticalCss(undefined);
}
}
}, []);

let [location, setLocation] = React.useState(router.state.location);

Expand Down
1 change: 0 additions & 1 deletion packages/react-router/lib/dom/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ declare global {
var __reactRouterRouteModules: RouteModules | undefined;
var __reactRouterDataRouter: DataRouter | undefined;
var __reactRouterHdrActive: boolean;
var __reactRouterClearCriticalCss: (() => void) | undefined;
var $RefreshRuntime$:
| {
performReactRefresh: () => void;
Expand Down