File size: 8,938 Bytes
94786da | 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/dynamic/error.tsx b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/dynamic/error.tsx
new file mode 100644
index 00000000..7d696a30
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/dynamic/error.tsx
@@ -0,0 +1,5 @@
+'use client'
+
+export default function DynamicError() {
+ return <div id="dynamic-error">Failed to render dynamic content</div>
+}
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/dynamic/page.tsx b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/dynamic/page.tsx
new file mode 100644
index 00000000..108ce3f0
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/dynamic/page.tsx
@@ -0,0 +1,66 @@
+import { Suspense } from 'react'
+import { headers } from 'next/headers'
+import { connection } from 'next/server'
+
+export const instant = {
+ unstable_samples: [{ headers: [['host', 'test-host']] }],
+}
+export const prefetch = 'partial'
+
+export default function Page() {
+ return (
+ <main>
+ <p id="intro">This page gates its dynamic content on connection().</p>
+ <Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
+ <RuntimePrefetchable />
+ </Suspense>
+ </main>
+ )
+}
+
+async function RuntimePrefetchable() {
+ const headersStore = await headers()
+ const headerValue = headersStore.get('host') === null ? 'missing' : 'present'
+ return (
+ <div>
+ <div id="header-value">{`Header: ${headerValue}`}</div>
+ <Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
+ <Dynamic />
+ </Suspense>
+ </div>
+ )
+}
+
+// A module-level cache keyed on the identity of the headers object (like
+// `dedupe()` from the Flags SDK, or any per-request memoization that treats
+// the headers object as "the request"), gating its data on `connection()` so
+// it only produces data during actual navigations, never during (runtime)
+// prefetches.
+//
+// A request can be rendered by multiple passes with different semantics for
+// `connection()`: the prospective and final prerenders of a runtime prefetch,
+// or a navigation's dynamic render and the runtime prerender that is spawned
+// from it to refresh the client's prefetch cache. In prerenders the
+// connection() promise hangs and is rejected when the pass is aborted; during
+// navigations it resolves. Each render pass resolves `await headers()` to a
+// distinct object, which scopes identity-keyed memoization like this cache to
+// a single pass: a promise created under one pass's semantics is never
+// consumed by another pass.
+const requestDataCache = new WeakMap<object, Promise<string>>()
+async function getRequestData(): Promise<string> {
+ const headersStore = await headers()
+ let dataPromise = requestDataCache.get(headersStore)
+ if (dataPromise === undefined) {
+ dataPromise = (async () => {
+ await connection()
+ return 'request data'
+ })()
+ requestDataCache.set(headersStore, dataPromise)
+ }
+ return dataPromise
+}
+
+async function Dynamic() {
+ const data = await getRequestData()
+ return <div id="dynamic-content">Dynamic content: {data}</div>
+}
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/layout.tsx b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/layout.tsx
new file mode 100644
index 00000000..c36226d9
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/layout.tsx
@@ -0,0 +1,9 @@
+import { ReactNode } from 'react'
+
+export default function RootLayout({ children }: { children: ReactNode }) {
+ return (
+ <html>
+ <body style={{ fontFamily: 'monospace' }}>{children}</body>
+ </html>
+ )
+}
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/page.tsx b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/page.tsx
new file mode 100644
index 00000000..ff594c24
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/app/page.tsx
@@ -0,0 +1,14 @@
+import { LinkAccordion } from '../components/link-accordion'
+
+export default function Page() {
+ return (
+ <main>
+ <h1>Index</h1>
+ <ul>
+ <li>
+ <LinkAccordion href="/dynamic">Dynamic page</LinkAccordion>
+ </li>
+ </ul>
+ </main>
+ )
+}
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/components/link-accordion.tsx b/test/e2e/app-dir/segment-cache/headers-keyed-caches/components/link-accordion.tsx
new file mode 100644
index 00000000..1b57ffef
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/components/link-accordion.tsx
@@ -0,0 +1,31 @@
+'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} prefetch>
+ {children}
+ </Link>
+ ) : (
+ <>{children} (link is hidden)</>
+ )}
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/headers-keyed-caches.test.ts b/test/e2e/app-dir/segment-cache/headers-keyed-caches/headers-keyed-caches.test.ts
new file mode 100644
index 00000000..dfffdd92
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/headers-keyed-caches.test.ts
@@ -0,0 +1,60 @@
+import { nextTestSetup } from 'e2e-utils'
+import { retry } from 'next-test-utils'
+import type * as Playwright from 'playwright'
+import { createRouterAct } from 'router-act'
+
+describe('module-level caches keyed on the headers object', () => {
+ const { next, isNextDev } = nextTestSetup({
+ files: __dirname,
+ })
+
+ if (isNextDev) {
+ // Runtime prefetching only happens in production builds.
+ it('is skipped in dev', () => {})
+ return
+ }
+
+ it('renders dynamic content on navigation even when the spawned runtime prerender populated the cache first', async () => {
+ const cliOutputStart = next.cliOutput.length
+
+ let page: Playwright.Page
+ const browser = await next.browser('/', {
+ beforePageLoad(p: Playwright.Page) {
+ page = p
+ },
+ })
+ const act = createRouterAct(page)
+
+ // Reveal the link, triggering the runtime prefetch.
+ await act(
+ async () => {
+ const linkToggle = await browser.elementByCss(
+ 'input[data-link-accordion="/dynamic"]'
+ )
+ await linkToggle.click()
+ },
+ { includes: 'Header:' }
+ )
+
+ // Navigate. The navigation request also spawns a runtime prerender to
+ // refresh the client's prefetch cache, which reaches the module-level
+ // cache before the stage-gated dynamic render of the navigation does.
+ // Because each render pass resolves `headers()` to a distinct object, the
+ // hanging connection() promise it memoizes is keyed to the prerender pass
+ // only: the navigation's dynamic render misses the cache, creates its own
+ // promise, and connection() resolves, so the dynamic content renders.
+ await browser.elementByCss('a[href="/dynamic"]').click()
+
+ await retry(async () => {
+ expect(await browser.elementById('dynamic-content').text()).toBe(
+ 'Dynamic content: request data'
+ )
+ })
+ expect(await browser.hasElementByCssSelector('#dynamic-error')).toBe(false)
+
+ // The rejection of the prerender pass's hanging promise stays within the
+ // pass that created it, so nothing is reported to onRequestError either.
+ const cliOutput = next.cliOutput.slice(cliOutputStart)
+ expect(cliOutput).not.toContain('[instrumentation] onRequestError:')
+ })
+})
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/instrumentation.ts b/test/e2e/app-dir/segment-cache/headers-keyed-caches/instrumentation.ts
new file mode 100644
index 00000000..a4eaac62
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/instrumentation.ts
@@ -0,0 +1,5 @@
+import { type Instrumentation } from 'next'
+
+export const onRequestError: Instrumentation.onRequestError = (err) => {
+ console.log(`[instrumentation] onRequestError:${(err as Error).message}`)
+}
diff --git a/test/e2e/app-dir/segment-cache/headers-keyed-caches/next.config.ts b/test/e2e/app-dir/segment-cache/headers-keyed-caches/next.config.ts
new file mode 100644
index 00000000..c7ffd9f8
--- /dev/null
+++ b/test/e2e/app-dir/segment-cache/headers-keyed-caches/next.config.ts
@@ -0,0 +1,10 @@
+import type { NextConfig } from 'next'
+
+const nextConfig: NextConfig = {
+ cacheComponents: true,
+ experimental: {
+ cachedNavigations: true,
+ },
+}
+
+export default nextConfig
|