Download nextjs-cache-after-prerender-abort/tests/test.patch from dari-ai/nextjs-selfbench: direct link, hf CLI and curl.
- Browser
- Download file 8.63 kB
-
https://huggingface.co/datasets/dari-ai/nextjs-selfbench/resolve/main/nextjs-cache-after-prerender-abort/tests/test.patch
- Command line
-
hf download hf://datasets/dari-ai/nextjs-selfbench/nextjs-cache-after-prerender-abort/tests/test.patch
-
curl -L -o test.patch https://huggingface.co/datasets/dari-ai/nextjs-selfbench/resolve/main/nextjs-cache-after-prerender-abort/tests/test.patch
8.63 kB
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/app/layout.tsx b/test/e2e/app-dir/use-cache-after-uncached-io/app/layout.tsx | |
| new file mode 100644 | |
| index 00000000..716a8db3 | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/app/layout.tsx | |
| +import { ReactNode } from 'react' | |
| + | |
| +export default function Root({ children }: { children: ReactNode }) { | |
| + return ( | |
| + <html> | |
| + <body>{children}</body> | |
| + </html> | |
| + ) | |
| +} | |
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/app/page.tsx b/test/e2e/app-dir/use-cache-after-uncached-io/app/page.tsx | |
| new file mode 100644 | |
| index 00000000..4f162a87 | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/app/page.tsx | |
| +import { LinkAccordion } from '../components/link-accordion' | |
| + | |
| +export default function Page() { | |
| + return ( | |
| + <main> | |
| + <LinkAccordion href="/uses-cache">/uses-cache</LinkAccordion> | |
| + </main> | |
| + ) | |
| +} | |
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/app/revalidate-uses-cache/route.ts b/test/e2e/app-dir/use-cache-after-uncached-io/app/revalidate-uses-cache/route.ts | |
| new file mode 100644 | |
| index 00000000..1ba4704e | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/app/revalidate-uses-cache/route.ts | |
| +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 }) | |
| +} | |
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/app/uses-cache/page.tsx b/test/e2e/app-dir/use-cache-after-uncached-io/app/uses-cache/page.tsx | |
| new file mode 100644 | |
| index 00000000..1ea041ab | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/app/uses-cache/page.tsx | |
| +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() | |
| +} | |
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/components/link-accordion.tsx b/test/e2e/app-dir/use-cache-after-uncached-io/components/link-accordion.tsx | |
| new file mode 100644 | |
| index 00000000..b3bf56b6 | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/components/link-accordion.tsx | |
| +'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)` | |
| + )} | |
| + </> | |
| + ) | |
| +} | |
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/next.config.ts b/test/e2e/app-dir/use-cache-after-uncached-io/next.config.ts | |
| new file mode 100644 | |
| index 00000000..49c8b8d7 | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/next.config.ts | |
| +import { NextConfig } from 'next' | |
| + | |
| +const nextConfig: NextConfig = { | |
| + cacheComponents: true, | |
| +} | |
| + | |
| +export default nextConfig | |
| diff --git a/test/e2e/app-dir/use-cache-after-uncached-io/use-cache-after-uncached-io.test.ts b/test/e2e/app-dir/use-cache-after-uncached-io/use-cache-after-uncached-io.test.ts | |
| new file mode 100644 | |
| index 00000000..b4e84ace | |
| --- /dev/null | |
| +++ b/test/e2e/app-dir/use-cache-after-uncached-io/use-cache-after-uncached-io.test.ts | |
| +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+/ | |
| + ) | |
| + }) | |
| + } | |
| +}) | |