Skip to content
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

router.prefetch should not trigger a React update #75238

Merged
merged 1 commit into from
Jan 23, 2025
Merged
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
17 changes: 11 additions & 6 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import type { AppRouterActionQueue } from '../../shared/lib/router/action-queue'
import { prefetch as prefetchWithSegmentCache } from '../components/segment-cache/prefetch'
import { getRedirectTypeFromError, getURLFromRedirectError } from './redirect'
import { isRedirectError, RedirectType } from './redirect-error'
import { prefetchReducer } from './router-reducer/reducers/prefetch-reducer'

const globalMutable: {
pendingMpaPath?: string
Expand Down Expand Up @@ -296,12 +297,16 @@ function Router({
// Use the old prefetch implementation.
const url = createPrefetchURL(href)
if (url !== null) {
startTransition(() => {
dispatch({
type: ACTION_PREFETCH,
url,
kind: options?.kind ?? PrefetchKind.FULL,
})
// The prefetch reducer doesn't actually update any state or
// trigger a rerender. 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.
// TODO: Refactor this away from a "reducer" so it's
// less confusing.
prefetchReducer(actionQueue.state, {
type: ACTION_PREFETCH,
url,
kind: options?.kind ?? PrefetchKind.FULL,
})
}
},
Expand Down
Loading