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
Failed to render dynamic content
+}
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 (
+
+ This page gates its dynamic content on connection().
+ Loading 1...}>
+
+
+
+ )
+}
+
+async function RuntimePrefetchable() {
+ const headersStore = await headers()
+ const headerValue = headersStore.get('host') === null ? 'missing' : 'present'
+ return (
+
+
+ Loading 2...
}>
+
+
+
+ )
+}
+
+// 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>()
+async function getRequestData(): Promise {
+ 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 Dynamic content: {data}
+}
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 (
+
+ {children}
+
+ )
+}
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 (
+
+ Index
+
+
+ )
+}
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 (
+ <>
+ setIsVisible(!isVisible)}
+ data-link-accordion={href}
+ />
+ {isVisible ? (
+
+ {children}
+
+ ) : (
+ <>{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