Commit 1d3554af authored by Zack Tanner's avatar Zack Tanner

fix: RDC data should be merged when generating fallback shells

parent 9725c47a
......@@ -977,5 +977,8 @@
"976": "Decompressed resume data cache exceeded %s byte limit",
"977": "maxPostponedStateSize must be a valid number (bytes) or filesize format string (e.g., \"5mb\")",
"978": "Next.js has blocked a javascript: URL as a security precaution.",
"979": "invariant: expected %s bytes of postponed state but only received %s bytes"
"979": "invariant: expected %s bytes of postponed state but only received %s bytes",
"980": "`parseSerializedResumeDataCache` should not be called in edge runtime.",
"981": "`mergeResumeDataCaches` should not be called in edge runtime.",
"982": "`stringifyParsedResumeDataCache` should not be called in edge runtime."
}
......@@ -68,6 +68,11 @@ import { isInterceptionRouteRewrite } from '../lib/generate-interception-routes-
import type { ActionManifest } from '../build/webpack/plugins/flight-client-entry-plugin'
import { extractInfoFromServerReferenceId } from '../shared/lib/server-reference-info'
import { convertSegmentPathToStaticExportFilename } from '../shared/lib/segment-cache/segment-value-encoding'
import {
parseSerializedResumeDataCache,
stringifyParsedResumeDataCache,
type ResumeStoreSerialized,
} from './resume-data-cache-utils'
import { getNextBuildDebuggerPortOffset } from '../lib/worker'
export class ExportError extends Error {
......@@ -663,7 +668,10 @@ async function exportAppImpl(
results = await exportPagesInBatches(worker, initialPhaseExportPaths)
if (finalPhaseExportPaths.length > 0) {
const renderResumeDataCachesByPage: Record<string, string> = {}
// Collect and merge RDC entries from all routes for each page.
// Different routes (e.g., /en/blog/hello-world and /fr/blog/hello-world)
// may have populated different cache entries that the fallback shell needs.
const parsedCachesByPage: Record<string, ResumeStoreSerialized> = {}
for (const { page, result } of results) {
if (!result) {
......@@ -671,16 +679,42 @@ async function exportAppImpl(
}
if ('renderResumeDataCache' in result && result.renderResumeDataCache) {
// The last RDC for each page is used. We only need one. It should have
// all the entries that the fallback shell also needs. We don't need to
// merge them per page.
renderResumeDataCachesByPage[page] = result.renderResumeDataCache
const parsed = parseSerializedResumeDataCache(
result.renderResumeDataCache
)
if (parsed) {
const existing = parsedCachesByPage[page]
if (existing) {
// Merge entries (later entries win for duplicates)
existing.store.cache = {
...existing.store.cache,
...parsed.store.cache,
}
existing.store.fetch = {
...existing.store.fetch,
...parsed.store.fetch,
}
existing.store.encryptedBoundArgs = {
...existing.store.encryptedBoundArgs,
...parsed.store.encryptedBoundArgs,
}
} else {
parsedCachesByPage[page] = parsed
}
}
// Remove the RDC string from the result so that it can be garbage
// collected, when there are more results for the same page.
result.renderResumeDataCache = undefined
}
}
// Serialize merged caches once for the final phase
const renderResumeDataCachesByPage: Record<string, string> = {}
for (const [page, parsed] of Object.entries(parsedCachesByPage)) {
renderResumeDataCachesByPage[page] =
stringifyParsedResumeDataCache(parsed)
}
const finalPhaseResults = await exportPagesInBatches(
worker,
finalPhaseExportPaths,
......
/**
* Utilities for parsing and serializing Resume Data Cache during build.
*/
export type ResumeStoreSerialized = {
store: {
cache: { [key: string]: any }
fetch: { [key: string]: any }
encryptedBoundArgs: { [key: string]: string }
}
}
/**
* Parses a compressed serialized resume data cache string into a JSON object.
* Used during build to collect and merge RDCs from multiple routes.
*/
export function parseSerializedResumeDataCache(
serialized: string
): ResumeStoreSerialized | null {
if (serialized === 'null' || !serialized) return null
const { inflateSync } = require('node:zlib') as typeof import('node:zlib')
return JSON.parse(
inflateSync(Buffer.from(serialized, 'base64'), {
maxOutputLength: 500 * 1024 * 1024, // 500MB limit for zip bomb protection
}).toString('utf-8')
)
}
/**
* Compresses a ResumeStoreSerialized object into a base64 string.
* Used during build after merging RDCs from multiple routes.
*/
export function stringifyParsedResumeDataCache(
parsed: ResumeStoreSerialized
): string {
const { deflateSync } = require('node:zlib') as typeof import('node:zlib')
return deflateSync(JSON.stringify(parsed)).toString('base64')
}
......@@ -87,7 +87,7 @@ import { INFINITE_CACHE } from '../../../lib/constants'
import { executeRevalidates } from '../../revalidation-utils'
import { trackPendingModules } from '../../app-render/module-loading/track-module-loading.external'
import { InvariantError } from '../../../shared/lib/invariant-error'
import { createPrerenderResumeDataCache } from '../../resume-data-cache/resume-data-cache'
import type { PrerenderResumeDataCache } from '../../resume-data-cache/resume-data-cache'
export class WrappedNextRouterError {
constructor(
......@@ -395,7 +395,12 @@ export class AppRouteRouteModule extends RouteModule<
// `renderResumeDataCache` with a single `dataCache` property that is
// conceptually not tied to resuming, and also avoids the unnecessary
// complexity of using a mutable and an immutable resume data cache.
const prerenderResumeDataCache = createPrerenderResumeDataCache()
const prerenderResumeDataCache: PrerenderResumeDataCache = {
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
}
const prospectiveRoutePrerenderStore: PrerenderStore =
(prerenderStore = {
......
import { Suspense } from 'react'
import { cookies } from 'next/headers'
import Link from 'next/link'
async function getBlogPost(locale: string, slug: string) {
'use cache'
// Simulate expensive operation like fetching from a CMS
await new Promise((resolve) => setTimeout(resolve, 700))
return {
title: `Blog Post: ${slug}`,
locale,
content: 'This content was fetched from the CMS...',
relatedPosts: ['Related Post 1', 'Related Post 2', 'Related Post 3'],
}
}
export default async function BlogPost({
params,
}: {
params: Promise<{ locale: string; slug: string }>
}) {
return (
<main>
<div id="static-blog-header">Blog Article</div>
<div id="static-reading-time">Estimated reading time: 5 minutes</div>
<Suspense fallback={<div id="blog-loading">Loading article...</div>}>
<BlogContent params={params} />
</Suspense>
<Suspense fallback={<div id="dynamic-loading">Loading comments...</div>}>
<DynamicComments />
</Suspense>
</main>
)
}
// This component depends on locale and slug, so it's in Suspense
async function BlogContent({
params,
}: {
params: Promise<{ locale: string; slug: string }>
}) {
const { locale, slug } = await params
const post = await getBlogPost(locale, slug)
return (
<>
<article id="blog-content">
<h1>{post.title}</h1>
<p>Locale: {post.locale}</p>
<p>{post.content}</p>
</article>
<aside id="related-posts">
<h2>Related Posts ({post.locale})</h2>
<ul>
{post.relatedPosts.map((title, i) => (
<li key={i}>
<Link href={`/en/blog/${i}`}>{title}</Link>
</li>
))}
</ul>
</aside>
</>
)
}
async function DynamicComments() {
// This is truly dynamic (uses cookies), always renders at request time
const cookieStore = await cookies()
const user = cookieStore.get('user')?.value || 'anonymous'
return (
<section id="comments">
<h3>Comments</h3>
<p>Viewing as: {user}</p>
</section>
)
}
// Generate some slugs for each locale
// This means for 'en' and 'fr', we have route shells for specific slugs
// But for 'de' (not in parent generateStaticParams), we have nothing
export function generateStaticParams() {
return [{ slug: 'hello-world' }, { slug: 'getting-started' }]
}
import { Suspense } from 'react'
import { cookies } from 'next/headers'
// Simulate an expensive cached operation (like fetching translations, config, etc.)
async function getLocaleConfig(localeParam: string) {
'use cache'
// Simulate expensive operation like fetching from a CMS or database
await new Promise((resolve) => setTimeout(resolve, 800))
return {
locale: localeParam,
translations: {
home: `Home (${localeParam})`,
blog: `Blog (${localeParam})`,
about: `About (${localeParam})`,
},
}
}
export default async function LocaleLayout({
children,
params,
}: {
children: React.ReactNode
params: Promise<{ locale: string }>
}) {
return (
<html>
<body>
{/* STATIC CONTENT - This does NOT depend on params and can be pre-rendered */}
<div id="static-header">Welcome to our Blog Platform</div>
<nav id="static-nav">
<ul>
<li>Home</li>
<li>Blog</li>
<li>About</li>
</ul>
</nav>
{/* DYNAMIC CONTENT - This depends on locale and is wrapped in Suspense */}
<Suspense
fallback={<div id="locale-loading">Loading locale info...</div>}
>
<LocaleInfo params={params} />
</Suspense>
<Suspense fallback={<div id="dynamic-loading">Loading user...</div>}>
<UserInfo />
</Suspense>
{children}
</body>
</html>
)
}
// This component depends on locale, so it's in Suspense
async function LocaleInfo({ params }: { params: Promise<{ locale: string }> }) {
const { locale } = await params
const config = await getLocaleConfig(locale)
return (
<header id="locale-header">
<span>Locale: {config.locale}</span>
<div id="translations">
{config.translations.home} | {config.translations.blog} |{' '}
{config.translations.about}
</div>
</header>
)
}
async function UserInfo() {
// This is truly dynamic (uses cookies), so it's always in Suspense
const cookieStore = await cookies()
const user = cookieStore.get('user')?.value || 'anonymous'
return <div id="user-info">Logged in as: {user}</div>
}
// Partial generateStaticParams - only 'en' and 'fr' are pregenerated
// 'de', 'es', etc. are NOT pregenerated
export function generateStaticParams() {
return [{ locale: 'en' }, { locale: 'fr' }]
}
import { Suspense } from 'react'
export default function LocaleHome({
params,
}: {
params: Promise<{ locale: string }>
}) {
return (
<main>
<Suspense fallback={<div id="home-loading">Loading...</div>}>
<HomeContent params={params} />
</Suspense>
</main>
)
}
async function HomeContent({
params,
}: {
params: Promise<{ locale: string }>
}) {
return (
<div id="home-content">
<h1>Home</h1>
<p>Welcome.</p>
</div>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('ppr-root-param-fallback', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should have use-cache content in fallback shells for all pregenerated locales', async () => {
// Setup: The app has a [locale] param with generateStaticParams returning
// ['en', 'fr'], and a nested /[locale]/blog/[slug] route. The layout uses
// a 'use cache' function to fetch locale-specific translations.
//
// This test ensures that we generate fallback shells with the correct locale
// filled in for all pregenerated locales.
for (const locale of ['en', 'fr']) {
// next.render$ doesn't stream, so we get just the shell content
const $ = await next.render$(`/${locale}/blog/new-post`)
// The shell should have the locale-header with cached content,
// NOT the locale-loading Suspense fallback
expect($('#locale-header').length).toBe(1)
expect($('#locale-header').text()).toContain(`Locale: ${locale}`)
expect($('#translations').text()).toContain(`Home (${locale})`)
// The Suspense fallback should NOT be in the shell
expect($('#locale-loading').length).toBe(0)
}
})
})
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