| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,9 @@ |
| +import { ReactNode } from 'react' |
| + |
| +export default function Root({ children }: { children: ReactNode }) { |
| + return ( |
| + <html> |
| + <body>{children}</body> |
| + </html> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,9 @@ |
| +import { LinkAccordion } from '../components/link-accordion' |
| + |
| +export default function Page() { |
| + return ( |
| + <main> |
| + <LinkAccordion href="/uses-cache">/uses-cache</LinkAccordion> |
| + </main> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,31 @@ |
| +import { revalidatePath, revalidateTag } from 'next/cache' |
| + |
| +type State = { attempts: number; executions: number; settlements: number } |
| + |
| +function getState(): State { |
| + const testGlobal = globalThis as typeof globalThis & { |
| + __nextUseCacheAfterUncachedIOState?: State |
| + } |
| + return (testGlobal.__nextUseCacheAfterUncachedIOState ??= { |
| + attempts: 0, |
| + executions: 0, |
| + settlements: 0, |
| + }) |
| +} |
| + |
| +export function GET() { |
| + return Response.json(getState()) |
| +} |
| + |
| +/** Evicts the entry so the prefetch's fill actually runs. */ |
| +export async function POST() { |
| + revalidateTag('data', { expire: 0 }) |
| + revalidatePath('/uses-cache') |
| + |
| + const state = getState() |
| + state.attempts = 0 |
| + state.executions = 0 |
| + state.settlements = 0 |
| + |
| + return Response.json({ ok: true }) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,53 @@ |
| +import { cacheLife, cacheTag } from 'next/cache' |
| +import { Suspense } from 'react' |
| + |
| +type State = { attempts: number; executions: number; settlements: number } |
| + |
| +function getState(): State { |
| + const testGlobal = globalThis as typeof globalThis & { |
| + __nextUseCacheAfterUncachedIOState?: State |
| + } |
| + return (testGlobal.__nextUseCacheAfterUncachedIOState ??= { |
| + attempts: 0, |
| + executions: 0, |
| + settlements: 0, |
| + }) |
| +} |
| + |
| +export default function Page() { |
| + return ( |
| + <main> |
| + This page uses a cache |
| + <Suspense fallback={<p id="fallback">Loading…</p>}> |
| + <Late /> |
| + </Suspense> |
| + </main> |
| + ) |
| +} |
| + |
| +async function Late() { |
| + // In a prerender, this will resolve after the prerender is already aborted |
| + // (both in prospective and final prerenders) |
| + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| + |
| + getState().attempts++ |
| + try { |
| + const result = await getCachedData() |
| + return <p id="data">{result}</p> |
| + } finally { |
| + getState().settlements++ |
| + // Retained for compatibility with older versions of this regression test. |
| + console.log('after-cache-read') |
| + } |
| +} |
| + |
| +async function getCachedData(): Promise<string> { |
| + 'use cache' |
| + cacheLife('hours') |
| + cacheTag('data') |
| + |
| + getState().executions++ |
| + console.log('running getCachedData') |
| + |
| + return 'cached-data: ' + Date.now() |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,32 @@ |
| +'use client' |
| +import Link from 'next/link' |
| +import { useState } from 'react' |
| + |
| +export function LinkAccordion({ |
| + href, |
| + children, |
| + prefetch, |
| +}: { |
| + href: string |
| + children: React.ReactNode |
| + prefetch?: boolean |
| +}) { |
| + const [isVisible, setIsVisible] = useState(false) |
| + return ( |
| + <> |
| + <input |
| + type="checkbox" |
| + checked={isVisible} |
| + onChange={() => setIsVisible(!isVisible)} |
| + data-link-accordion={href} |
| + /> |
| + {isVisible ? ( |
| + <Link href={href} prefetch={prefetch}> |
| + {children} |
| + </Link> |
| + ) : ( |
| + `${children} (link is hidden)` |
| + )} |
| + </> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,7 @@ |
| +import { NextConfig } from 'next' |
| + |
| +const nextConfig: NextConfig = { |
| + cacheComponents: true, |
| +} |
| + |
| +export default nextConfig |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,90 @@ |
| +import { nextTestSetup } from 'e2e-utils' |
| +import * as Playwright from 'playwright' |
| +import { createRouterAct } from '../../../lib/router-act' |
| +import { retry } from '../../../lib/next-test-utils' |
| + |
| +type CacheState = { |
| + attempts: number |
| + executions: number |
| + settlements: number |
| +} |
| + |
| +describe('use cache reached after uncancellable asynchronous work', () => { |
| + const { next, isNextStart } = nextTestSetup({ |
| + files: __dirname, |
| + }) |
| + |
| + if (isNextStart) { |
| + it('does not start an abandoned fill or poison a later request', async () => { |
| + // This is a regression test for: |
| + // https://github.com/vercel/next.js/issues/96339 |
| + // Asynchronous work can reach a cache call after its prerender has ended. |
| + // That abandoned call must not execute a fill or affect a later live request. |
| + |
| + // Revalidate /uses-cache (and, just to be safe, the cache that it references) |
| + // so that we have a fresh prerender we can assert on |
| + await next.fetch('/revalidate-uses-cache', { method: 'POST' }) |
| + |
| + let page: Playwright.Page |
| + const browser = await next.browser('/', { |
| + beforePageLoad(p) { |
| + page = p |
| + }, |
| + }) |
| + const act = createRouterAct(page) |
| + |
| + // Prefetch the page, triggering a fresh prerender |
| + await act(async () => { |
| + await browser |
| + .elementByCss('input[data-link-accordion="/uses-cache"]') |
| + .click() |
| + }) |
| + |
| + // Synchronize through a fixture endpoint instead of implementation logs. |
| + // The delayed component reached and settled its abandoned cache call, but |
| + // the cache function itself must not have started. |
| + let abandonedAttempts = 0 |
| + await retry(async () => { |
| + const state = (await next |
| + .fetch('/revalidate-uses-cache') |
| + .then((response) => response.json())) as CacheState |
| + expect(state.attempts).toBeGreaterThan(0) |
| + expect(state.executions).toBe(0) |
| + expect(state.settlements).toBe(state.attempts) |
| + abandonedAttempts = state.attempts |
| + }) |
| + |
| + // Navigate to the page. The response should include the cache |
| + await act( |
| + async () => { |
| + await browser.elementByCss('a[href="/uses-cache"]').click() |
| + }, |
| + |
| + { includes: 'cached-data' } |
| + ) |
| + |
| + const value = await browser.elementByCss('#data').text() |
| + expect(value).toMatch(/cached-data: \d+/) |
| + |
| + // The later live render performs the first and only cache fill. |
| + expect( |
| + (await next |
| + .fetch('/revalidate-uses-cache') |
| + .then((response) => response.json())) as CacheState |
| + ).toEqual({ |
| + attempts: abandonedAttempts + 1, |
| + executions: 1, |
| + settlements: abandonedAttempts + 1, |
| + }) |
| + }) |
| + } else { |
| + it('resolves in dev', async () => { |
| + // There's no prefetching in dev, so the best we can do is |
| + // test that the cache resolves as expected. |
| + const browser = await next.browser('/uses-cache') |
| + expect(await browser.elementByCss('#data').text()).toMatch( |
| + /cached-data: \d+/ |
| + ) |
| + }) |
| + } |
| +}) |
|
|