diff --git a/test/e2e/app-dir/partial-fallback-root-blocking/next.config.js b/test/e2e/app-dir/partial-fallback-root-blocking/next.config.js index e64bae22..5e50f0b7 100644 --- a/test/e2e/app-dir/partial-fallback-root-blocking/next.config.js +++ b/test/e2e/app-dir/partial-fallback-root-blocking/next.config.js @@ -3,6 +3,7 @@ */ const nextConfig = { cacheComponents: true, + partialPrefetching: true, } module.exports = nextConfig diff --git a/test/e2e/app-dir/partial-fallback-root-blocking/partial-fallback-root-blocking.test.ts b/test/e2e/app-dir/partial-fallback-root-blocking/partial-fallback-root-blocking.test.ts index 76293240..bfe46a4a 100644 --- a/test/e2e/app-dir/partial-fallback-root-blocking/partial-fallback-root-blocking.test.ts +++ b/test/e2e/app-dir/partial-fallback-root-blocking/partial-fallback-root-blocking.test.ts @@ -42,22 +42,29 @@ describe('partial-fallback-root-blocking', () => { // TODO: Re-enable once infra supports multiple layers of fallbacks if (!isNextDeploy) { - it('should not reuse a shared shell cache entry for unknown root branches', async () => { + it('should serve unknown root branches with the root param resolved and other params deferred', async () => { const firstResult = await fetchSplitHTML('/fr/two') expect(firstResult.response.status).toBe(200) expect(firstResult.static$('#lang-fallback').length).toBe(0) expect(firstResult.static$('#lang').text()).toBe('fr') - expect(firstResult.static$('#slug').text()).toBe('two') - expect(firstResult.dynamicPart).toBe('') + + expect(firstResult.static$('#slug-fallback').text()).toBe( + 'loading slug...' + ) + expect(firstResult.static$('#slug').length).toBe(0) + expect(firstResult.dynamicPart).toContain('
two
') const secondResult = await fetchSplitHTML('/fr/other') expect(secondResult.response.status).toBe(200) expect(secondResult.static$('#lang-fallback').length).toBe(0) expect(secondResult.static$('#lang').text()).toBe('fr') - expect(secondResult.static$('#slug').text()).toBe('other') - expect(secondResult.dynamicPart).toBe('') + expect(secondResult.static$('#slug-fallback').text()).toBe( + 'loading slug...' + ) + expect(secondResult.static$('#slug').length).toBe(0) + expect(secondResult.dynamicPart).toContain('
other
') }) } diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/adapter-partial-fallback-blocking.test.ts b/test/production/app-dir/adapter-partial-fallback-blocking/adapter-partial-fallback-blocking.test.ts new file mode 100644 index 00000000..acdfa239 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/adapter-partial-fallback-blocking.test.ts @@ -0,0 +1,132 @@ +import { nextTestSetup } from 'e2e-utils' +import type { NextAdapter } from 'next' + +describe('adapter-partial-fallback-blocking', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) + + function expectAllowedParams(allowQuery: string[], expected: string[]) { + // Adapter query allowlists are sets. Their serialized order is not part + // of the build output contract. + expect(new Set(allowQuery)).toEqual(new Set(expected)) + } + + it('should exclude never-prerenderable params from allowQuery for blocking routes with empty shells', async () => { + const { outputs }: Parameters[0] = + await next.readJSON('build-complete.json') + + const genericEmptyShellPrerender = outputs.prerenders.find( + (output) => output.pathname === '/empty-shell/[one]/[two]' + ) + const genericEmptyShellDataPrerender = outputs.prerenders.find( + (output) => output.pathname === '/empty-shell/[one]/[two].rsc' + ) + const genericEmptyShellSegmentPrerenders = outputs.prerenders.filter( + (output) => + output.pathname.startsWith('/empty-shell/[one]/[two].segments/') + ) + const generatedEmptyShellPrerender = outputs.prerenders.find( + (output) => output.pathname === '/empty-shell/a/[two]' + ) + + expect(genericEmptyShellPrerender).toBeDefined() + expect(genericEmptyShellDataPrerender).toBeDefined() + expect(genericEmptyShellSegmentPrerenders.length).toBeGreaterThan(0) + expect(generatedEmptyShellPrerender).toBeDefined() + + expectAllowedParams(genericEmptyShellPrerender.config.allowQuery, [ + 'nxtPone', + ]) + expectAllowedParams(genericEmptyShellDataPrerender.config.allowQuery, [ + 'nxtPone', + ]) + for (const output of genericEmptyShellSegmentPrerenders) { + expectAllowedParams(output.config.allowQuery, ['nxtPone']) + } + + expect(generatedEmptyShellPrerender.config.partialFallback).toBeUndefined() + expectAllowedParams(generatedEmptyShellPrerender.config.allowQuery, []) + }) + + it('should exclude never-prerenderable params from allowQuery for entries with a resolved root param', async () => { + const { outputs }: Parameters[0] = + await next.readJSON('build-complete.json') + + const emptyShellPrerender = outputs.prerenders.find( + (output) => + output.pathname === '/with-root-param/en/empty-shell/[category]/[id]' + ) + const emptyShellDataPrerender = outputs.prerenders.find( + (output) => + output.pathname === + '/with-root-param/en/empty-shell/[category]/[id].rsc' + ) + const nonEmptyShellPrerender = outputs.prerenders.find( + (output) => + output.pathname === + '/with-root-param/en/non-empty-shell/[category]/[id]' + ) + const emptyShellLeafPrerender = outputs.prerenders.find( + (output) => + output.pathname === '/with-root-param/en/empty-shell/shoes/[id]' + ) + + expect(emptyShellPrerender).toBeDefined() + expect(emptyShellDataPrerender).toBeDefined() + expect(nonEmptyShellPrerender).toBeDefined() + expect(emptyShellLeafPrerender).toBeDefined() + + expectAllowedParams(emptyShellPrerender.config.allowQuery, [ + 'nxtPcategory', + ]) + expectAllowedParams(emptyShellDataPrerender.config.allowQuery, [ + 'nxtPcategory', + ]) + + expectAllowedParams(nonEmptyShellPrerender.config.allowQuery, [ + 'nxtPcategory', + ]) + expect(nonEmptyShellPrerender.config.partialFallback).toBe(true) + + expectAllowedParams(emptyShellLeafPrerender.config.allowQuery, []) + }) + + it('should exclude never-prerenderable params from allowQuery for entries with an unresolved root param', async () => { + const { outputs }: Parameters[0] = + await next.readJSON('build-complete.json') + + const emptyShellBasePrerender = outputs.prerenders.find( + (output) => + output.pathname === + '/with-root-param/[lang]/empty-shell/[category]/[id]' + ) + const emptyShellBaseDataPrerender = outputs.prerenders.find( + (output) => + output.pathname === + '/with-root-param/[lang]/empty-shell/[category]/[id].rsc' + ) + const nonEmptyShellBasePrerender = outputs.prerenders.find( + (output) => + output.pathname === + '/with-root-param/[lang]/non-empty-shell/[category]/[id]' + ) + + expect(emptyShellBasePrerender).toBeDefined() + expect(emptyShellBaseDataPrerender).toBeDefined() + expect(nonEmptyShellBasePrerender).toBeDefined() + + expectAllowedParams(emptyShellBasePrerender.config.allowQuery, [ + 'nxtPlang', + 'nxtPcategory', + ]) + expectAllowedParams(emptyShellBaseDataPrerender.config.allowQuery, [ + 'nxtPlang', + 'nxtPcategory', + ]) + expectAllowedParams(nonEmptyShellBasePrerender.config.allowQuery, [ + 'nxtPlang', + 'nxtPcategory', + ]) + }) +}) diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/app/(standard)/empty-shell/[one]/[two]/page.tsx b/test/production/app-dir/adapter-partial-fallback-blocking/app/(standard)/empty-shell/[one]/[two]/page.tsx new file mode 100644 index 00000000..65d28a99 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/app/(standard)/empty-shell/[one]/[two]/page.tsx @@ -0,0 +1,27 @@ +// This route intentionally produces EMPTY build-time shells: the params are +// read outside of any Suspense boundary (and there is no loading.tsx), so the +// postpone propagates to the root. `instant = false` opts the route out of +// requiring an instant (non-empty) shell. +// +// The empty shells downgrade the generic route to a blocking route, but `two` +// is still never provided by `generateStaticParams`: an on-demand render may +// only complete `one`, so only `one` may participate in the cache key. +export function generateStaticParams() { + return [{ one: 'a' }] +} + +export const instant = false + +export default async function Page({ + params, +}: { + params: Promise<{ one: string; two: string }> +}) { + const { one, two } = await params + + return ( +
+ {one}:{two} +
+ ) +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/app/(standard)/layout.tsx b/test/production/app-dir/adapter-partial-fallback-blocking/app/(standard)/layout.tsx new file mode 100644 index 00000000..e7a42507 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/app/(standard)/layout.tsx @@ -0,0 +1,11 @@ +import { ReactNode } from 'react' + +// Root layout for the (standard) branch: all params in this branch are +// below the root layout, so none of them are root params. +export default function Root({ children }: { children: ReactNode }) { + return ( + + {children} + + ) +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/empty-shell/[category]/[id]/page.tsx b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/empty-shell/[category]/[id]/page.tsx new file mode 100644 index 00000000..d2b6d1b1 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/empty-shell/[category]/[id]/page.tsx @@ -0,0 +1,38 @@ +// Empty-shell variant under a root param: the params are read outside of +// any Suspense boundary, so the postpone propagates to the root and every +// build-time shell is empty. `instant = false` opts the route out of +// requiring an instant (non-empty) shell. +// +// `generateStaticParams` provides `lang` in every entry (required for root +// params) and partially covers `category`; `id` is never provided, so `id` +// must never be resolved into a cached shell and must never be part of a +// cache key — including for requests whose `lang` value was not enumerated +// (e.g. /fr/...), which match the base route entry where `lang` is an +// unresolved root param. +export async function generateStaticParams() { + return [{ lang: 'en' }, { lang: 'en', category: 'shoes' }] +} + +export const instant = false + +export default async function Page({ + params, +}: { + params: Promise<{ lang: string; category: string; id: string }> +}) { + const { lang, category, id } = await params + + // Per-render marker (prerenderable): must be re-rendered per request and + // never repeat across fetches — a repeated value proves a stored render + // is being replayed from the cache. + const renderedAt = performance.now() + + return ( +
+
{lang}
+
{category}
+
{id}
+
{renderedAt}
+
+ ) +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/layout.tsx b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/layout.tsx new file mode 100644 index 00000000..bc155c67 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/layout.tsx @@ -0,0 +1,19 @@ +// The ROOT layout lives inside [lang], making `lang` a ROOT param: the +// document itself (the html tag) varies by lang with no Suspense boundary. +// Root params must be provided by every generateStaticParams result — the +// build enforces this — so `lang` is always a prerenderable param. +export default async function RootLayout({ + children, + params, +}: { + children: React.ReactNode + params: Promise<{ lang: string }> +}) { + const { lang } = await params + + return ( + + {children} + + ) +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/non-empty-shell/[category]/[id]/page.tsx b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/non-empty-shell/[category]/[id]/page.tsx new file mode 100644 index 00000000..ba731670 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/non-empty-shell/[category]/[id]/page.tsx @@ -0,0 +1,49 @@ +import { Suspense } from 'react' + +// Non-empty-shell variant under a root param: static page content plus +// Suspense boundaries around the non-root param reads, so shells contain +// static content and no empty-shell downgrade happens. +// +// `generateStaticParams` provides `lang` in every entry (required for root +// params) and partially covers `category`; `id` is never provided and must +// never resolve into a cached shell nor participate in a cache key. +export async function generateStaticParams() { + return [{ lang: 'en' }, { lang: 'en', category: 'shoes' }] +} + +async function Id({ params }: { params: Promise<{ id: string }> }) { + const { id } = await params + + // Per-render marker (prerenderable), read after `await params` so it + // belongs to the deferred region: it must be re-rendered (resumed) per + // request and never repeat across fetches. + const renderedAt = performance.now() + + return ( + <> +
{id}
+
{renderedAt}
+ + ) +} + +export default function Page({ + params, +}: { + params: Promise<{ lang: string; category: string; id: string }> +}) { + return ( +
+
static page content
+ + loading id... +
+ } + > + + + + ) +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/non-empty-shell/[category]/layout.tsx b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/non-empty-shell/[category]/layout.tsx new file mode 100644 index 00000000..a0da238a --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/app/with-root-param/[lang]/non-empty-shell/[category]/layout.tsx @@ -0,0 +1,40 @@ +import { Suspense, type ReactNode } from 'react' + +// `category` is read in its own Suspense boundary: shells where it is +// concrete render it statically, and generic shells show +// `#category-fallback`. This makes the served shell's specialization +// observable from the static part of the response. (`lang` is a root param +// and is part of the document itself, rendered by the root layout.) +async function LayoutImpl({ + children, + params, +}: { + children: ReactNode + params: Promise<{ category: string }> +}) { + const { category } = await params + + return ( +
+
{category}
+ {children} +
+ ) +} + +export default function Layout(props: { + children: ReactNode + params: Promise<{ category: string }> +}) { + return ( + + loading category... + + } + > + + + ) +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/my-adapter.mjs b/test/production/app-dir/adapter-partial-fallback-blocking/my-adapter.mjs new file mode 100644 index 00000000..548129ec --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/my-adapter.mjs @@ -0,0 +1,9 @@ +import fs from 'fs/promises' + +/** @type {import('next').NextAdapter } */ +export default { + name: 'adapter-partial-fallback-blocking', + async onBuildComplete(ctx) { + await fs.writeFile('build-complete.json', JSON.stringify(ctx, null, 2)) + }, +} diff --git a/test/production/app-dir/adapter-partial-fallback-blocking/next.config.js b/test/production/app-dir/adapter-partial-fallback-blocking/next.config.js new file mode 100644 index 00000000..39584cc1 --- /dev/null +++ b/test/production/app-dir/adapter-partial-fallback-blocking/next.config.js @@ -0,0 +1,10 @@ +/** + * @type {import('next').NextConfig} + */ +const nextConfig = { + cacheComponents: true, + partialPrefetching: true, + adapterPath: require.resolve('./my-adapter.mjs'), +} + +module.exports = nextConfig