| |
| |
| |
| |
| @@ -3,6 +3,7 @@ |
| */ |
| const nextConfig = { |
| cacheComponents: true, |
| + partialPrefetching: true, |
| } |
| |
| module.exports = nextConfig |
| |
| |
| |
| |
| @@ -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('<div id="slug">two</div>') |
| |
| 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('<div id="slug">other</div>') |
| }) |
| } |
| |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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<NextAdapter['onBuildComplete']>[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<NextAdapter['onBuildComplete']>[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<NextAdapter['onBuildComplete']>[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', |
| + ]) |
| + }) |
| +}) |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 ( |
| + <div> |
| + {one}:{two} |
| + </div> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 ( |
| + <html> |
| + <body>{children}</body> |
| + </html> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 ( |
| + <div> |
| + <div id="lang">{lang}</div> |
| + <div id="category">{category}</div> |
| + <div id="id">{id}</div> |
| + <div id="rendered-at">{renderedAt}</div> |
| + </div> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 ( |
| + <html lang={lang}> |
| + <body>{children}</body> |
| + </html> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 ( |
| + <> |
| + <div id="id">{id}</div> |
| + <div id="rendered-at">{renderedAt}</div> |
| + </> |
| + ) |
| +} |
| + |
| +export default function Page({ |
| + params, |
| +}: { |
| + params: Promise<{ lang: string; category: string; id: string }> |
| +}) { |
| + return ( |
| + <div> |
| + <div id="static">static page content</div> |
| + <Suspense |
| + fallback={ |
| + <div id="id-fallback" data-fallback> |
| + loading id... |
| + </div> |
| + } |
| + > |
| + <Id params={params} /> |
| + </Suspense> |
| + </div> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 ( |
| + <div> |
| + <div id="category">{category}</div> |
| + {children} |
| + </div> |
| + ) |
| +} |
| + |
| +export default function Layout(props: { |
| + children: ReactNode |
| + params: Promise<{ category: string }> |
| +}) { |
| + return ( |
| + <Suspense |
| + fallback={ |
| + <div id="category-fallback" data-fallback> |
| + loading category... |
| + </div> |
| + } |
| + > |
| + <LayoutImpl {...props} /> |
| + </Suspense> |
| + ) |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -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)) |
| + }, |
| +} |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,10 @@ |
| +/** |
| + * @type {import('next').NextConfig} |
| + */ |
| +const nextConfig = { |
| + cacheComponents: true, |
| + partialPrefetching: true, |
| + adapterPath: require.resolve('./my-adapter.mjs'), |
| +} |
| + |
| +module.exports = nextConfig |
|
|