Unverified Commit 8daa66b6 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Add test to verify that inner caches are excluded from Resume Data Cache (#75883)

The Resume Data Cache (RDC) is used to provide cache consistency when
resuming a partial prerendered static shell. When reading from this
cache, it is not possible for there to be a miss because, regardless of
the cache age, we always consume the cache entries on resume because
they are temporarily bound to the static shell being served for this
particular request. Since the cache entry can never miss, there is no
need to store inner cache entries in the Resume Data Cache because the
only time you would encounter an inner entry is if the outer entry had
to revalidate.

We have already optimized the Resume Data Cache by excluding writing to
it during prerendering (and dev warmup rendering) if we're already
inside a cache scope. We also handle that you can have an inner cache
entry fill the backing cache and omit the RDC entry, and then later we
encounter that same cache entry in the prerender scope, and even though
we don't need to produce a new cache entry (it was already cached in the
backing cache implementation), we do still write it to the Resume Data
Cache.

The test added in this PR verifies this behavior and ensures that we
don't regress in the future.

closes NAR-54
parent 0915a508
...@@ -8,7 +8,10 @@ import type { CachedFetchValue } from '../response-cache/types' ...@@ -8,7 +8,10 @@ import type { CachedFetchValue } from '../response-cache/types'
/** /**
* A generic cache store type that provides a subset of Map functionality * A generic cache store type that provides a subset of Map functionality
*/ */
type CacheStore<T> = Pick<Map<string, T>, 'entries' | 'size' | 'get' | 'set'> type CacheStore<T> = Pick<
Map<string, T>,
'entries' | 'keys' | 'size' | 'get' | 'set'
>
/** /**
* A cache store specifically for fetch cache values * A cache store specifically for fetch cache values
......
import { connection } from 'next/server'
import { Suspense } from 'react'
async function outermost(id: string) {
'use cache'
return id + middle('middle')
}
async function middle(id: string) {
'use cache'
return id + innermost('inner')
}
async function innermost(id: string) {
'use cache'
return id
}
async function Dynamic() {
await connection()
return null
}
export default async function Page() {
await outermost('outer')
await innermost('inner')
return (
<Suspense>
<Dynamic />
</Suspense>
)
}
...@@ -3,6 +3,10 @@ import { retry, waitFor } from 'next-test-utils' ...@@ -3,6 +3,10 @@ import { retry, waitFor } from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { format } from 'util' import { format } from 'util'
import { BrowserInterface } from 'next-webdriver' import { BrowserInterface } from 'next-webdriver'
import {
createRenderResumeDataCache,
RenderResumeDataCache,
} from 'next/dist/server/resume-data-cache/resume-data-cache'
const GENERIC_RSC_ERROR = const GENERIC_RSC_ERROR =
'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.' 'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.'
...@@ -596,6 +600,27 @@ describe('use-cache', () => { ...@@ -596,6 +600,27 @@ describe('use-cache', () => {
}) })
}) })
} }
if (isNextStart && process.env.__NEXT_EXPERIMENTAL_PPR === 'true') {
it('should exclude inner caches from the resume data cache (RDC)', async () => {
await next.fetch('/rdc')
const resumeDataCache = extractResumeDataCacheFromPostponedState(
JSON.parse(await next.readFile('.next/server/app/rdc.meta')).postponed
)
const cacheKeys = Array.from(resumeDataCache.cache.keys())
// There should be no cache entry for the "middle" cache function, because
// it's only used inside another cache scope ("outer"). Whereas "inner" is
// also used inside a prerender scope (the page). Note: We're matching on
// the "id" args that are encoded into the respective cache keys.
expect(cacheKeys).toMatchObject([
expect.stringContaining('["outer"]'),
expect.stringContaining('["inner"]'),
])
})
}
}) })
async function getSanitizedLogs(browser: BrowserInterface): Promise<string[]> { async function getSanitizedLogs(browser: BrowserInterface): Promise<string[]> {
...@@ -607,3 +632,14 @@ async function getSanitizedLogs(browser: BrowserInterface): Promise<string[]> { ...@@ -607,3 +632,14 @@ async function getSanitizedLogs(browser: BrowserInterface): Promise<string[]> {
) )
) )
} }
function extractResumeDataCacheFromPostponedState(
state: string
): RenderResumeDataCache {
const postponedStringLengthMatch = state.match(/^([0-9]*):/)![1]
const postponedStringLength = parseInt(postponedStringLengthMatch)
return createRenderResumeDataCache(
state.slice(postponedStringLengthMatch.length + postponedStringLength + 1)
)
}
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