File size: 8,949 Bytes
b2cad4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/docs/[[...slug]]/page.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/docs/[[...slug]]/page.tsx
new file mode 100644
index 0000000000000000000000000000000000000000..dd9ad409e52d6e8d39efcaece3fc8d26a395afdd
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/docs/[[...slug]]/page.tsx
@@ -0,0 +1,36 @@
+import { LinkAccordion } from '../../../components/link-accordion'
+
+type Params = { slug?: string[] }
+
+export function generateStaticParams(): Params[] {
+  return [{ slug: [] }, { slug: ['alpha'] }, { slug: ['beta'] }]
+}
+
+/**
+ * An optional catch-all where the index (empty slug) and the named pages are
+ * the same route. The page reads `params`, so every one of these segments
+ * varies by `slug` — the server reports that in the response's vary params.
+ */
+export default async function DocsPage({
+  params,
+}: {
+  params: Promise<Params>
+}) {
+  const { slug } = await params
+  const name = slug && slug.length > 0 ? slug.join('/') : 'index'
+  return (
+    <div>
+      {/*
+        The id is slug-specific so a test can wait for the navigation to
+        actually commit — an id shared by every slug would match the
+        pre-navigation DOM and read stale content.
+
+        One interpolated string so the text is a single node in the RSC
+        payload, which lets the test assert on response contents.
+      */}
+      <div id={`docs-page-${name}`}>{`Docs: ${name}`}</div>
+      <LinkAccordion href="/docs/alpha">alpha</LinkAccordion>
+      <LinkAccordion href="/docs/beta">beta</LinkAccordion>
+    </div>
+  )
+}
diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/layout.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/layout.tsx
new file mode 100644
index 0000000000000000000000000000000000000000..08eaa94fdc8896acfd343a0dbb61c713eec93a55
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/layout.tsx
@@ -0,0 +1,11 @@
+export default function RootLayout({
+  children,
+}: {
+  children: React.ReactNode
+}) {
+  return (
+    <html>
+      <body>{children}</body>
+    </html>
+  )
+}
diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/page.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/page.tsx
new file mode 100644
index 0000000000000000000000000000000000000000..ef3f0b14c0a9145a09a4c0f2acb45d9065805aeb
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/page.tsx
@@ -0,0 +1,15 @@
+import { LinkAccordion } from '../components/link-accordion'
+
+export default function HomePage() {
+  return (
+    <div>
+      <div id="home-page">Home</div>
+      {/*
+        Revealing this link prefetches /docs — the fully static index of the
+        optional catch-all route. That prefetch is what used to poison the
+        param-wildcard slot for every other slug.
+      */}
+      <LinkAccordion href="/docs">Docs index</LinkAccordion>
+    </div>
+  )
+}
diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/components/link-accordion.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/components/link-accordion.tsx
new file mode 100644
index 0000000000000000000000000000000000000000..2b1e68daafd32988e83f7b40b2365d36dd387b5f
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/components/link-accordion.tsx
@@ -0,0 +1,29 @@
+'use client'
+
+import Link from 'next/link'
+import { useState } from 'react'
+
+export function LinkAccordion({
+  href,
+  children,
+}: {
+  href: string
+  children: React.ReactNode
+}) {
+  const [isVisible, setIsVisible] = useState(false)
+  return (
+    <>
+      <input
+        type="checkbox"
+        checked={isVisible}
+        onChange={() => setIsVisible(!isVisible)}
+        data-link-accordion={href}
+      />
+      {isVisible ? (
+        <Link href={href}>{children}</Link>
+      ) : (
+        <>{children} (link is hidden)</>
+      )}
+    </>
+  )
+}
diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/next.config.js b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/next.config.js
new file mode 100644
index 0000000000000000000000000000000000000000..62e0151ab5ef666e250c00fa21208dfddd544ad3
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/next.config.js
@@ -0,0 +1,13 @@
+/**
+ * @type {import('next').NextConfig}
+ */
+const nextConfig = {
+  cacheComponents: true,
+  // Partial Prefetching enables the Shell prefetch phase, where the bug lived.
+  partialPrefetching: true,
+  experimental: {
+    varyParams: process.env.TEST_VARY_PARAMS === 'true',
+  },
+}
+
+module.exports = nextConfig
diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/static-shell-vary-params-regression.test.ts b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/static-shell-vary-params-regression.test.ts
new file mode 100644
index 0000000000000000000000000000000000000000..8b7b8174363aa6cb833e186de3832a5c0f06faf7
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/static-shell-vary-params-regression.test.ts
@@ -0,0 +1,91 @@
+import { nextTestSetup } from 'e2e-utils'
+import type * as Playwright from 'playwright'
+import { createRouterAct } from 'router-act'
+
+describe.each([
+  ['vary params enabled', 'true'],
+  ['vary params disabled', 'false'],
+])(
+  'segment cache - static shell vary params regression (%s)',
+  (_, varyParams) => {
+    const { next, isNextDev } = nextTestSetup({
+      files: __dirname,
+      env: { TEST_VARY_PARAMS: varyParams },
+    })
+
+    if (isNextDev) {
+      // Depends on build-time prerenders, which don't exist in dev.
+      test('skipped in dev mode', () => {})
+      return
+    }
+
+    // Regression test for a segment cache keying bug.
+    //
+    // During the Shell prefetch phase the client walks at the StaticShell
+    // strategy, and it used to key whatever came back at `tree.shellVaryPath`,
+    // which replaces every non-root param with Fallback. That's only correct
+    // when the payload really is the param-independent shell.
+    //
+    // When a route renders with no dynamic hole, the response has no shell/full
+    // split — the "shell" payload IS the concrete render for the params that
+    // were requested. Keying it at the wildcard path published one param's page
+    // as the answer for every other param value, and recorded it at the highest
+    // tier, so later navigations rendered the wrong page and skipped the network
+    // entirely.
+    //
+    // On an optional catch-all, the index (empty slug) is the case that hits
+    // this: prefetching /docs poisoned /docs/alpha and /docs/beta.
+    //
+    // The fix makes the server-reported vary params authoritative, so the shell
+    // path is used exactly when the segment really is param-independent.
+    it('does not serve the catch-all index page for a different slug', async () => {
+      let page: Playwright.Page
+      const browser = await next.browser('/', {
+        beforePageLoad(p: Playwright.Page) {
+          page = p
+        },
+      })
+      const act = createRouterAct(page, { includeAppShellRequests: true })
+
+      // Prefetch the fully static index of the optional catch-all route. Its
+      // response has no shell/full split, so this is the write that used to land
+      // in the param-wildcard slot.
+      await act(
+        async () => {
+          await browser
+            .elementByCss('input[data-link-accordion="/docs"]')
+            .click()
+        },
+        { includes: 'Docs: index' }
+      )
+
+      // Navigate there. Fully prefetched, so nothing should be requested.
+      await act(async () => {
+        await browser.elementByCss('a[href="/docs"]').click()
+        expect(await browser.elementById('docs-page-index').text()).toBe(
+          'Docs: index'
+        )
+      }, 'no-requests')
+
+      // Now prefetch a different slug on the same route. Before the fix the
+      // index's page segment was sitting in the wildcard slot marked complete,
+      // so this fired no request at all for the page content and this
+      // expectation would time out.
+      await act(
+        async () => {
+          await browser
+            .elementByCss('input[data-link-accordion="/docs/alpha"]')
+            .click()
+        },
+        { includes: 'Docs: alpha' }
+      )
+
+      await act(async () => {
+        await browser.elementByCss('a[href="/docs/alpha"]').click()
+        expect(await browser.elementById('docs-page-alpha').text()).toBe(
+          'Docs: alpha'
+        )
+      }, 'no-requests')
+    })
+  }
+)