Skip to content

Commit d0b08bf

Browse files
fix: clear critical css after initial render in HydratedRouter (#13872)
Co-authored-by: Mark Dalgleish <[email protected]>
1 parent b92ddad commit d0b08bf

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

.changeset/great-lobsters-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
In Framework Mode, clear critical CSS in development after initial render

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
- FilipJirsak
114114
- focusotter
115115
- foxscotch
116+
- frodi-karlsson
116117
- frontsideair
117118
- fucancode
118119
- fyzhu

packages/react-router-dev/vite/static/refresh-utils.cjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ const routeUpdates = new Map();
158158
window.__reactRouterRouteModuleUpdates = new Map();
159159

160160
import.meta.hot.on("react-router:hmr", async ({ route }) => {
161-
window.__reactRouterClearCriticalCss();
162-
163161
if (route) {
164162
routeUpdates.set(route.id, route);
165163
}

packages/react-router/lib/dom-export/hydrated-router.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,19 @@ export function HydratedRouter(props: HydratedRouterProps) {
229229
});
230230
}
231231

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

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

packages/react-router/lib/dom/global.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ declare global {
2929
var __reactRouterRouteModules: RouteModules | undefined;
3030
var __reactRouterDataRouter: DataRouter | undefined;
3131
var __reactRouterHdrActive: boolean;
32-
var __reactRouterClearCriticalCss: (() => void) | undefined;
3332
var $RefreshRuntime$:
3433
| {
3534
performReactRefresh: () => void;

0 commit comments

Comments
 (0)