Unverified Commit 4a31419c authored by nt's avatar nt Committed by GitHub

Persist fully static PPR payloads in filesystem cache (#100759)

Problem: Complete PPR pages were saved without the .rsc file that
disk-cache reads require. After memory eviction or restart, those reads
missed and pages rendered again.
Change: Save the complete RSC payload for non-postponed PPR pages.
Continue excluding fallbacks and postponed pages.
Regression evidence: Without the fix, the complete-PPR unit test fails;
the other 21 pass. With the fix, all 22 pass.
HTTP validation: Disk-only repeat requests and cache persistence across
restart pass under both Turbopack and Webpack.

Fixes #100670
Fixes #98306
Closes #98307
Co-authored-by: 's avatarShtokarev Yuriy <shtokarev@gmail.com>
parent c15cede5
...@@ -681,8 +681,14 @@ export default class FileSystemCache implements CacheHandler { ...@@ -681,8 +681,14 @@ export default class FileSystemCache implements CacheHandler {
writer.append(htmlPath, data.html) writer.append(htmlPath, data.html)
// Fallbacks don't generate a data file. // Complete PPR pages also need an RSC payload for disk-cache reads.
if (!ctx.fetchCache && !ctx.isFallback && !ctx.isRoutePPREnabled) { // Fallbacks and postponed PPR pages don't generate a complete data file.
if (
!ctx.fetchCache &&
!ctx.isFallback &&
(!ctx.isRoutePPREnabled ||
(data.kind === CachedRouteKind.APP_PAGE && data.postponed == null))
) {
writer.append( writer.append(
this.getFilePath( this.getFilePath(
`${key}${isAppPath ? RSC_SUFFIX : NEXT_DATA_SUFFIX}`, `${key}${isAppPath ? RSC_SUFFIX : NEXT_DATA_SUFFIX}`,
......
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { cacheLife } from 'next/cache'
export function generateStaticParams() {
return [{ slug: '_build-placeholder' }]
}
async function getRenderId(slug: string) {
'use cache'
cacheLife({ stale: 60, revalidate: 300, expire: 3600 })
return `${slug}:${Date.now()}`
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
return <p id="render-id">{await getRenderId(slug)}</p>
}
export default function Page() {
return <p>Disk cache regression fixture</p>
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { load } from 'cheerio'
describe('cache-components-disk-readback', () => {
const { next } = nextTestSetup({
files: __dirname,
})
async function read(slug: string) {
const response = await next.fetch(`/p/${slug}`)
expect(response.status).toBe(200)
const $ = load(await response.text())
const renderId = $('#render-id').text()
expect(renderId).toMatch(new RegExp(`^${slug}:\\d+$`))
return {
renderId,
cacheStatus: response.headers.get('x-nextjs-cache'),
}
}
async function waitForCachedPage(slug: string) {
return retry(async () => {
const result = await read(slug)
expect(result.cacheStatus).toBe('HIT')
return result
})
}
it('reads a runtime-created page from disk with memory caching disabled', async () => {
await read('repeat')
const cached = await waitForCachedPage('repeat')
const repeated = await read('repeat')
expect(repeated.cacheStatus).toBe('HIT')
expect(repeated.renderId).toBe(cached.renderId)
})
it('reads a runtime-created page from disk after a server restart', async () => {
await read('restart')
const cached = await waitForCachedPage('restart')
await next.stop()
await next.start({ skipBuild: true })
const restarted = await read('restart')
expect(restarted.cacheStatus).toBe('HIT')
expect(restarted.renderId).toBe(cached.renderId)
})
})
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
partialPrefetching: true,
cacheMaxMemorySize: 0,
}
export default nextConfig
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { gate } from 'next-test-utils'
import { load } from 'cheerio' import { load } from 'cheerio'
import { createServer } from 'node:http' import { createServer } from 'node:http'
import type { AddressInfo } from 'node:net' import type { AddressInfo } from 'node:net'
...@@ -99,13 +98,7 @@ describe('file-system-cache-clock', () => { ...@@ -99,13 +98,7 @@ describe('file-system-cache-clock', () => {
expect(await readPageEntry(pathname)).toContain(rendered.data) expect(await readPageEntry(pathname)).toContain(rendered.data)
const cached = await request(pathname) const cached = await request(pathname)
expect(cached.data).toBe(rendered.data) expect(cached.data).toBe(rendered.data)
// The file-system cache doesn't write the RSC payload of a PPR page, so expect(cached.cache).toBe('HIT')
// it can't read that entry back. With the memory cache off, the page is
// rendered again from the cached fetch.
const isPPR = await gate((c) => c.cacheComponents)
expect(cached.cache).toBe(
isPPR && cacheMaxMemorySize === '0' ? 'MISS' : 'HIT'
)
await revalidate() await revalidate()
const fresh = await request(pathname) const fresh = await request(pathname)
......
...@@ -69,6 +69,73 @@ describe('FileSystemCache (isrMemory 0)', () => { ...@@ -69,6 +69,73 @@ describe('FileSystemCache (isrMemory 0)', () => {
maxMemoryCacheSize: 0, // disable memory cache maxMemoryCacheSize: 0, // disable memory cache
}) })
it.each([
{ name: 'complete PPR page', isFallback: false, postponed: undefined },
{ name: 'postponed PPR page', isFallback: false, postponed: 'postponed' },
{ name: 'PPR fallback', isFallback: true, postponed: undefined },
])('round-trips a $name through disk', async ({ isFallback, postponed }) => {
const serverDistDir = await fs.mkdtemp(join(tmpdir(), 'next-ppr-disk-'))
try {
const createCache = () =>
new FileSystemCache({
_requestHeaders: {},
flushToDisk: true,
fs: nodeFs,
serverDistDir,
revalidatedTags: [],
maxMemoryCacheSize: 0,
})
const key = getRouteCacheKey('/p/example', {
kind: RouteKind.APP_PAGE,
sourceRoute: '/p/[slug]/page',
})
const context = {
isFallback,
isRoutePPREnabled: true,
}
const rscData =
!isFallback && postponed === undefined
? Buffer.from('complete RSC payload')
: undefined
await createCache().set(
key,
{
kind: CachedRouteKind.APP_PAGE,
html: '<p>cached page</p>',
rscData,
postponed,
headers: {},
status: 200,
segmentData: undefined,
},
context
)
const result = await createCache().get(key, {
kind: IncrementalCacheKind.APP_PAGE,
...context,
})
expect(result?.value).toMatchObject({
kind: CachedRouteKind.APP_PAGE,
html: '<p>cached page</p>',
rscData,
postponed,
})
if (rscData === undefined) {
await expect(
fs.stat(join(serverDistDir, `${key}.rsc`))
).rejects.toMatchObject({
code: 'ENOENT',
})
}
} finally {
await fs.rm(serverDistDir, { recursive: true, force: true })
}
})
it('should cache fetch', async () => { it('should cache fetch', async () => {
await fsCache.set( await fsCache.set(
'fetch-cache', 'fetch-cache',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment