Unverified Commit d7a18cc1 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[PPF] Use RDC in runtime prerenders (#98025)

Runtime shell prefetch requests were ignoring the provided RDC. I'm not
sure what the history was, i.e. whether this was introduced when
unifying RDCs like #98005 or if it was like that from the start, but
this PR fixes it.

(the added tests were failing before the fix)
parent 69219b49
...@@ -1652,7 +1652,10 @@ async function generateRuntimePrefetchResult( ...@@ -1652,7 +1652,10 @@ async function generateRuntimePrefetchResult(
// We need to share caches between the prospective prerender and the final prerender, // We need to share caches between the prospective prerender and the final prerender,
// but we're not going to persist this anywhere. // but we're not going to persist this anywhere.
const prerenderResumeDataCache = createPrerenderResumeDataCache() const prerenderResumeDataCache = createPrerenderResumeDataCache(
// Prefill the mutable cache from the RDC if available.
requestStore.resumeDataCache ?? undefined
)
const mode: RuntimePrerenderMode = isShellPrefetch const mode: RuntimePrerenderMode = isShellPrefetch
? { ? {
......
import { connection } from 'next/server'
import { cookies } from 'next/headers'
import { Suspense } from 'react'
type SearchParams = Record<string, string | string[]>
export default async function Page({
searchParams,
}: {
searchParams: Promise<SearchParams>
}) {
const cachedTimestamp = await getCachedTimestamp()
return (
<main>
<p>
This page uses a cache that is included both in the HTML shell and in
runtime shells/prefetches. The values should consistent everywhere
thanks to the RDC.
</p>
<p id="cached-data">{cachedTimestamp}</p>
<Suspense fallback={<div>Loading...</div>}>
<Runtime searchParams={searchParams} />
</Suspense>
<Suspense fallback={<div>Loading dynamic data...</div>}>
{connection().then(() => (
<p id="dynamic-data">Dynamic data</p>
))}
</Suspense>
</main>
)
}
async function Runtime({
searchParams,
}: {
searchParams: Promise<SearchParams>
}) {
// Force the page to use a runtime shell/prefetch
await cookies()
return (
<div>
<p id="cookie-data">Cookie data</p>
<Suspense fallback={<div>Loading...</div>}>
{searchParams.then(() => (
<p id="search-params-data">Search params data</p>
))}
</Suspense>
</div>
)
}
async function getCachedTimestamp() {
'use cache'
await new Promise((resolve) => setTimeout(resolve))
return `cache-timestamp: ${Date.now()}`
}
...@@ -291,6 +291,12 @@ export default async function Page() { ...@@ -291,6 +291,12 @@ export default async function Page() {
<li> <li>
<DebugLinkAccordion href="/fully-static" prefetch /> <DebugLinkAccordion href="/fully-static" prefetch />
</li> </li>
<li>
<DebugLinkAccordion href="/cache-from-rdc" />
</li>
<li>
<DebugLinkAccordion href="/cache-from-rdc" prefetch />
</li>
</ul> </ul>
</main> </main>
) )
......
...@@ -1088,6 +1088,106 @@ describe('runtime prefetching', () => { ...@@ -1088,6 +1088,106 @@ describe('runtime prefetching', () => {
}) })
}) })
describe('cache values are consistent across HTML shell and runtime requests', () => {
const href = '/cache-from-rdc'
const htmlId = 'cached-data'
const getCacheValueFromInitialHTML = async (): Promise<string> => {
const $ = await next.render$(href)
const text = $(`#${htmlId}`).text()
if (!text) {
throw new Error('Cached value not found in HTML')
}
return text
}
it('runtime shell', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page, { includeAppShellRequests: true })
const cacheValueFromHTMLShell = await getCacheValueFromInitialHTML()
// Reveal the link to trigger a runtime prefetch for the page
await act(async () => {
const linkToggle = await browser.elementByCss(
`input[data-prefetch="auto"][data-link-accordion="${href}"]`
)
await linkToggle.click()
}, [
{
includes: 'Cookie data',
kind: 'runtime',
},
])
// Navigate to the page.
await act(async () => {
await browser.elementByCss(`a[href="${href}"]`).click()
// The runtime shell should contain the same cache value as the HTML.
expect(await browser.elementById(htmlId).text()).toBe(
cacheValueFromHTMLShell
)
}, [{ includes: 'Dynamic data' }])
// The navigation response should also contain the same cache value.
expect(await browser.elementById(htmlId).text()).toBe(
cacheValueFromHTMLShell
)
})
it('runtime prefetch', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page, { includeAppShellRequests: true })
const cacheValueFromHTMLShell = await getCacheValueFromInitialHTML()
// Reveal the link to trigger a runtime prefetch for the page
await act(async () => {
const linkToggle = await browser.elementByCss(
`input[data-prefetch="true"][data-link-accordion="${href}"]`
)
await linkToggle.click()
}, [
// Shell
{
includes: 'Cookie data',
kind: 'runtime',
},
// Prefetch
{
includes: 'Search params data',
kind: 'runtime',
},
])
// Navigate to the page.
await act(async () => {
await browser.elementByCss(`a[href="${href}"]`).click()
// The runtime prefetch should contain the same cache value as the HTML.
expect(await browser.elementById(htmlId).text()).toBe(
cacheValueFromHTMLShell
)
}, [{ includes: 'Dynamic data' }])
// The navigation response should also contain the same cache value.
expect(await browser.elementById(htmlId).text()).toBe(
cacheValueFromHTMLShell
)
})
})
describe('errors', () => { describe('errors', () => {
it.each([ it.each([
{ {
......
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