Skip to content

Commit 829c638

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 0516d67 commit 829c638

File tree

1 file changed

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

1 file changed

+3
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ function Root() {
5656
const fetchPromise = fetchContent(nextLocation)
5757
const nextContentPromise = createFromFetch(fetchPromise)
5858
contentCache.set(historyKey, nextContentPromise)
59-
startTransition(() => setContentKey(historyKey)) // Fetch may suspend
59+
startTransition(() => setContentKey(historyKey))
60+
} else {
61+
setContentKey(historyKey)
6062
}
61-
// Cache hit: Synchronous update
62-
// ⚠️ No startTransition here:
63-
// Resolved promises don’t suspend, and transitions add unnecessary scheduler work, risking freezes during rapid back/forward spamming.
64-
setContentKey(historyKey)
6563
}
6664
window.addEventListener('popstate', handlePopState)
6765
return () => window.removeEventListener('popstate', handlePopState)

0 commit comments

Comments
 (0)