-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure router.refresh() matches revalidatePath('/') behavior (#46723)
Ensures `router.refresh()` matches the upcoming `revalidatePath('/')` api. This also ensures that when server context has changed it applies to all routes, not just the one that triggered the refresh. - When `router.refresh()` is called we fetch the full RSC payload from the server (root layout till the page) - The client-side router cache is fully invalidated, effectively making it empty - The client-side router prefetch cache is fully invalidated, effectively making it empty - RSC payload is applied to the state fix NEXT-590 ([link](https://linear.app/vercel/issue/NEXT-590)) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: --> ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https:/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https:/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https:/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https:/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
1 parent
a3dff7c
commit 341daf9
Showing
13 changed files
with
440 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/next/src/client/components/router-reducer/apply-flight-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CacheNode, CacheStates } from '../../../shared/lib/app-router-context' | ||
import { FlightDataPath } from '../../../server/app-render/types' | ||
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head' | ||
import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data' | ||
import { ReadonlyReducerState } from './router-reducer-types' | ||
|
||
export function applyFlightData( | ||
state: ReadonlyReducerState, | ||
cache: CacheNode, | ||
flightDataPath: FlightDataPath | ||
): boolean { | ||
// The one before last item is the router state tree patch | ||
const [treePatch, subTreeData, head] = flightDataPath.slice(-3) | ||
|
||
// Handles case where prefetch only returns the router tree patch without rendered components. | ||
if (subTreeData === null) { | ||
return false | ||
} | ||
|
||
if (flightDataPath.length === 3) { | ||
cache.status = CacheStates.READY | ||
cache.subTreeData = subTreeData | ||
fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head) | ||
} else { | ||
// Copy subTreeData for the root node of the cache. | ||
cache.status = CacheStates.READY | ||
cache.subTreeData = state.cache.subTreeData | ||
// Create a copy of the existing cache with the subTreeData applied. | ||
fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath) | ||
} | ||
|
||
return true | ||
} |
5 changes: 3 additions & 2 deletions
5
packages/next/src/client/components/router-reducer/create-href-from-url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export function createHrefFromUrl( | ||
url: Pick<URL, 'pathname' | 'search' | 'hash'> | ||
url: Pick<URL, 'pathname' | 'search' | 'hash'>, | ||
includeHash: boolean = true | ||
): string { | ||
return url.pathname + url.search + url.hash | ||
return url.pathname + url.search + (includeHash ? url.hash : '') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/next/src/client/components/router-reducer/handle-mutable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
Mutable, | ||
ReadonlyReducerState, | ||
ReducerState, | ||
} from './router-reducer-types' | ||
|
||
export function handleMutable( | ||
state: ReadonlyReducerState, | ||
mutable: Mutable | ||
): ReducerState { | ||
return { | ||
// Set href. | ||
canonicalUrl: | ||
typeof mutable.canonicalUrl !== 'undefined' | ||
? mutable.canonicalUrl === state.canonicalUrl | ||
? state.canonicalUrl | ||
: mutable.canonicalUrl | ||
: state.canonicalUrl, | ||
pushRef: { | ||
pendingPush: | ||
typeof mutable.pendingPush !== 'undefined' | ||
? mutable.pendingPush | ||
: state.pushRef.pendingPush, | ||
mpaNavigation: | ||
typeof mutable.mpaNavigation !== 'undefined' | ||
? mutable.mpaNavigation | ||
: state.pushRef.mpaNavigation, | ||
}, | ||
// All navigation requires scroll and focus management to trigger. | ||
focusAndScrollRef: { | ||
apply: | ||
typeof mutable.applyFocusAndScroll !== 'undefined' | ||
? mutable.applyFocusAndScroll | ||
: state.focusAndScrollRef.apply, | ||
hashFragment: | ||
// Empty hash should trigger default behavior of scrolling layout into view. | ||
// #top is handled in layout-router. | ||
mutable.hashFragment && mutable.hashFragment !== '' | ||
? // Remove leading # and decode hash to make non-latin hashes work. | ||
decodeURIComponent(mutable.hashFragment.slice(1)) | ||
: null, | ||
}, | ||
// Apply cache. | ||
cache: mutable.cache ? mutable.cache : state.cache, | ||
prefetchCache: mutable.prefetchCache | ||
? mutable.prefetchCache | ||
: state.prefetchCache, | ||
// Apply patched router state. | ||
tree: | ||
typeof mutable.patchedTree !== 'undefined' | ||
? mutable.patchedTree | ||
: state.tree, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.