Skip to content

Commit 7099753

Browse files
authored
backport: fix prerender issue with intercepting routes + generateStaticParams (#75170)
Backports the testcase added in #75167 in [9bf61bc](9bf61bc). Confirmed failure via [this run](https://github.com/vercel/next.js/actions/runs/12898849331/job/35966678688?pr=75170) Adds the fix in [0ab1e32](0ab1e32). This change is identical to the code used in canary, which was added as part of the `rootParams` feature via #73816 ([ref](https://github.com/vercel/next.js/blob/d12e2e82b778eef8393f47944a258a55c6c508fe/packages/next/src/build/static-paths/app.ts#L316-L317)) This regression was caused by `segments` containing all possible segments (including parallel routes), not just the page segment. As a result, `paramKeys` was incorrectly being calculated. We don't need to traverse the segments to determine the param keys: we have the route regex & matcher, it's more reliable to extract it from that.
1 parent 819d575 commit 7099753

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

packages/next/src/build/utils.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,18 +1289,8 @@ export async function buildAppStaticPaths({
12891289
minimalMode: ciEnvironment.hasNextSupport,
12901290
})
12911291

1292-
const paramKeys = new Set<string>()
1293-
1294-
const staticParamKeys = new Set<string>()
1295-
for (const segment of segments) {
1296-
if (segment.param) {
1297-
paramKeys.add(segment.param)
1298-
1299-
if (segment.config?.dynamicParams === false) {
1300-
staticParamKeys.add(segment.param)
1301-
}
1302-
}
1303-
}
1292+
const regex = getRouteRegex(page)
1293+
const paramKeys = Object.keys(getRouteMatcher(regex)(page) || {})
13041294

13051295
const afterRunner = new AfterRunner()
13061296

@@ -1417,7 +1407,7 @@ export async function buildAppStaticPaths({
14171407
// Determine if all the segments have had their parameters provided. If there
14181408
// was no dynamic parameters, then we've collected all the params.
14191409
const hadAllParamsGenerated =
1420-
paramKeys.size === 0 ||
1410+
paramKeys.length === 0 ||
14211411
(routeParams.length > 0 &&
14221412
routeParams.every((params) => {
14231413
for (const key of paramKeys) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function CatchAll() {
2+
return null
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return 'intercepted'
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type Props = {
2+
params: Promise<{ slug: string }>
3+
}
4+
5+
export default async function Page({ params }: Props) {
6+
const { slug } = await params
7+
return <div>Hello {slug}</div>
8+
}
9+
10+
export function generateStaticParams() {
11+
return [
12+
{ slug: 'a' },
13+
{ slug: 'b' },
14+
{ slug: 'c' },
15+
{ slug: 'd' },
16+
{ slug: 'e' },
17+
{ slug: 'f' },
18+
]
19+
}

test/e2e/app-dir/interception-dynamic-segment/interception-dynamic-segment.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
22
import { check } from 'next-test-utils'
33

44
describe('interception-dynamic-segment', () => {
5-
const { next } = nextTestSetup({
5+
const { next, isNextStart } = nextTestSetup({
66
files: __dirname,
77
})
88

@@ -15,4 +15,13 @@ describe('interception-dynamic-segment', () => {
1515
await check(() => browser.elementById('modal').text(), '')
1616
await check(() => browser.elementById('children').text(), /not intercepted/)
1717
})
18+
19+
if (isNextStart) {
20+
it('should correctly prerender segments with generateStaticParams', async () => {
21+
expect(next.cliOutput).toContain('/generate-static-params/a')
22+
const res = await next.fetch('/generate-static-params/a')
23+
expect(res.status).toBe(200)
24+
expect(res.headers.get('x-nextjs-cache')).toBe('HIT')
25+
})
26+
}
1827
})

0 commit comments

Comments
 (0)