Unverified Commit f641c39d authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

router.prefetch should not trigger a React update (#75238)

The prefetch reducer doesn't actually modify any React state. It just
writes to a mutable cache. So we shouldn't bother calling
setState/dispatch; we can just re-run the reducer directly using the
current state.
parent 55f25fb2
...@@ -61,6 +61,7 @@ import type { AppRouterActionQueue } from '../../shared/lib/router/action-queue' ...@@ -61,6 +61,7 @@ import type { AppRouterActionQueue } from '../../shared/lib/router/action-queue'
import { prefetch as prefetchWithSegmentCache } from '../components/segment-cache/prefetch' import { prefetch as prefetchWithSegmentCache } from '../components/segment-cache/prefetch'
import { getRedirectTypeFromError, getURLFromRedirectError } from './redirect' import { getRedirectTypeFromError, getURLFromRedirectError } from './redirect'
import { isRedirectError, RedirectType } from './redirect-error' import { isRedirectError, RedirectType } from './redirect-error'
import { prefetchReducer } from './router-reducer/reducers/prefetch-reducer'
const globalMutable: { const globalMutable: {
pendingMpaPath?: string pendingMpaPath?: string
...@@ -296,12 +297,16 @@ function Router({ ...@@ -296,12 +297,16 @@ function Router({
// Use the old prefetch implementation. // Use the old prefetch implementation.
const url = createPrefetchURL(href) const url = createPrefetchURL(href)
if (url !== null) { if (url !== null) {
startTransition(() => { // The prefetch reducer doesn't actually update any state or
dispatch({ // trigger a rerender. It just writes to a mutable cache. So we
type: ACTION_PREFETCH, // shouldn't bother calling setState/dispatch; we can just re-run
url, // the reducer directly using the current state.
kind: options?.kind ?? PrefetchKind.FULL, // TODO: Refactor this away from a "reducer" so it's
}) // less confusing.
prefetchReducer(actionQueue.state, {
type: ACTION_PREFETCH,
url,
kind: options?.kind ?? PrefetchKind.FULL,
}) })
} }
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment