forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic parameter marker to router cache key (vercel#47957)
### What? Took a bit to investigate this one, eventually found that the case where it broke is this one: ``` app ├── [slug] // This matches `/blog` │ └── page.js └── blog └── [name] // This matches `/blog/a-post` └── page.js ``` The router cache key is based on the "static key" / "dynamic parameter value" in the tree. This means that the cache key for `/blog` that matches `/[slug]` would be the same as the static segment `blog`. This caused the cache to become intertwined between those paths, it's accidental that the router got stuck in that case, main reason it got stuck is that the fetch for the RSC payload returned a deeper value than expected. In `walkAddRefetch` we bailed because that walked the `segmentPath` didn't match up. The underlying problem with this was that the render would override the cache nodes incorrectly. This would also cause wrong behavior, even though that wasn't reported. E.g. `app/[slug]/layout.js` would apply on `app/blog/[name]/page.js` because they'd share the `blog` cache node. ### How? This PR changes the cache key to include the dynamic parameter name and type, e.g. the dynamic segment `['slug', 'blog', 'd']` previously turned into `'blog'` as the cache key, with these changes it turns into `'slug|blog|d'`. For static segments like `blog` in `app/blog/[name]` the key is still just `'blog'`. I've also refactored the cases where we read the segment as the code was duplicated in a few places. Closes NEXT-877 Fixes vercel#47297 <!-- 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: ## For Contributors ### Improving Documentation or adding/fixing Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> fix NEXT-877
- Loading branch information
1 parent
10dbcc6
commit d32ee25
Showing
21 changed files
with
241 additions
and
40 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
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
19 changes: 19 additions & 0 deletions
19
packages/next/src/client/components/router-reducer/create-router-cache-key.test.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,19 @@ | ||
import { createRouterCacheKey } from './create-router-cache-key' | ||
|
||
describe('createRouterCacheKey', () => { | ||
it('should support string segment', () => { | ||
expect(createRouterCacheKey('foo')).toEqual('foo') | ||
}) | ||
|
||
it('should support dynamic segment', () => { | ||
expect(createRouterCacheKey(['slug', 'hello-world', 'd'])).toEqual( | ||
'slug|hello-world|d' | ||
) | ||
}) | ||
|
||
it('should support catch all segment', () => { | ||
expect(createRouterCacheKey(['slug', 'blog/hello-world', 'c'])).toEqual( | ||
'slug|blog/hello-world|c' | ||
) | ||
}) | ||
}) |
7 changes: 7 additions & 0 deletions
7
packages/next/src/client/components/router-reducer/create-router-cache-key.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,7 @@ | ||
import { Segment } from '../../../server/app-render/types' | ||
|
||
export function createRouterCacheKey(segment: Segment) { | ||
return Array.isArray(segment) | ||
? `${segment[0]}|${segment[1]}|${segment[2]}` | ||
: segment | ||
} |
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
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
17 changes: 17 additions & 0 deletions
17
packages/next/src/client/components/router-reducer/get-segment-value.test.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,17 @@ | ||
import { getSegmentValue } from './reducers/get-segment-value' | ||
|
||
describe('getSegmentValue', () => { | ||
it('should support string segment', () => { | ||
expect(getSegmentValue('foo')).toEqual('foo') | ||
}) | ||
|
||
it('should support dynamic segment', () => { | ||
expect(getSegmentValue(['slug', 'hello-world', 'd'])).toEqual('hello-world') | ||
}) | ||
|
||
it('should support catch all segment', () => { | ||
expect(getSegmentValue(['slug', 'blog/hello-world', 'c'])).toEqual( | ||
'blog/hello-world' | ||
) | ||
}) | ||
}) |
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
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
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
5 changes: 5 additions & 0 deletions
5
packages/next/src/client/components/router-reducer/reducers/get-segment-value.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,5 @@ | ||
import { Segment } from '../../../../server/app-render/types' | ||
|
||
export function getSegmentValue(segment: Segment) { | ||
return Array.isArray(segment) ? segment[1] : segment | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/app-routes/app/conflicting-dynamic-static-segments/[slug]/page.tsx
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,3 @@ | ||
export default function Page({ params }) { | ||
return <h1>Page {params.slug}</h1> | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/app-routes/app/conflicting-dynamic-static-segments/blog/[slug]/page.tsx
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,3 @@ | ||
export default function Page({ params }) { | ||
return <h1>Blog Sub Page {params.slug}</h1> | ||
} |
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,16 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
test/e2e/app-dir/router-stuck-dynamic-static-segment/app/[slug]/page.tsx
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,15 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page({ params }) { | ||
return ( | ||
<div> | ||
<h1 id="slug-page">Visiting page {params.slug}</h1> | ||
<Link href="/blog/a-post" style={{ display: 'block' }} id="to-blog-post"> | ||
Go to a post | ||
</Link> | ||
<Link href="/" style={{ display: 'block' }}> | ||
Go home | ||
</Link> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.