| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,5 @@ |
| +'use client' |
| + |
| +export default function DynamicError() { |
| + return <div id="dynamic-error">Failed to render dynamic content</div> |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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> |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,9 @@ |
| +import { ReactNode } from 'react' |
| + |
| +export default function RootLayout({ children }: { children: ReactNode }) { |
| + return ( |
| + <html> |
| + <body style={{ fontFamily: 'monospace' }}>{children}</body> |
| + </html> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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)</> |
| + )} |
| + </> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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:') |
| + }) |
| +}) |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,5 @@ |
| +import { type Instrumentation } from 'next' |
| + |
| +export const onRequestError: Instrumentation.onRequestError = (err) => { |
| + console.log(`[instrumentation] onRequestError:${(err as Error).message}`) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,10 @@ |
| +import type { NextConfig } from 'next' |
| + |
| +const nextConfig: NextConfig = { |
| + cacheComponents: true, |
| + experimental: { |
| + cachedNavigations: true, |
| + }, |
| +} |
| + |
| +export default nextConfig |
|
|