Skip to content

Commit 94ec0aa

Browse files
fix(popstate): skip unnecessary startTransition on cached navigations
Previously, every popstate update, including cached content, was wrapped in startTransition. Rapid back/forward spam caused multiple transitions to enqueue in React's concurrent scheduler, leading to heavy reconciliation and freezing the browser tab.
1 parent 11abcbc commit 94ec0aa

File tree

1 file changed

+4
-3
lines changed
  • exercises/04.router/05.solution.cache/ui

1 file changed

+4
-3
lines changed

exercises/04.router/05.solution.cache/ui/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'react'
1212
import { createRoot } from 'react-dom/client'
1313
import * as RSC from 'react-server-dom-esm/client'
14-
import { contentCache, useContentCache, generateKey } from './content-cache.js'
14+
import { contentCache, generateKey, useContentCache } from './content-cache.js'
1515
import { ErrorBoundary } from './error-boundary.js'
1616
import { shipFallbackSrc } from './img-utils.js'
1717
import { RouterContext, getGlobalLocation, useLinkHandler } from './router.js'
@@ -56,9 +56,10 @@ function Root() {
5656
const fetchPromise = fetchContent(nextLocation)
5757
const nextContentPromise = createFromFetch(fetchPromise)
5858
contentCache.set(historyKey, nextContentPromise)
59+
startTransition(() => setContentKey(historyKey))
60+
} else {
61+
setContentKey(historyKey)
5962
}
60-
61-
startTransition(() => setContentKey(historyKey))
6263
}
6364
window.addEventListener('popstate', handlePopState)
6465
return () => window.removeEventListener('popstate', handlePopState)

0 commit comments

Comments
 (0)