Unverified Commit 90691cff authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Partial Prerendering Basepath Fix (#69520)

This adds new information to the routes manifests that is used during
deployment for pages rendered using Partial Prerendering (PPR). It
changes the route detection mode from using a pathname to detect a PPR
resume request to using a new header, `Next-Resume: 1`. When a `POST`
request is received with this header, it is assumed to be a resume
request.

The old mechanism is being retained until a future canary release to
validate the changes to infrastructure.
parent 15900e0b
...@@ -29,6 +29,7 @@ import { ...@@ -29,6 +29,7 @@ import {
INSTRUMENTATION_HOOK_FILENAME, INSTRUMENTATION_HOOK_FILENAME,
RSC_PREFETCH_SUFFIX, RSC_PREFETCH_SUFFIX,
RSC_SUFFIX, RSC_SUFFIX,
NEXT_RESUME_HEADER,
} from '../lib/constants' } from '../lib/constants'
import { FileType, fileExists } from '../lib/file-exists' import { FileType, fileExists } from '../lib/file-exists'
import { findPagesDir } from '../lib/find-pages-dir' import { findPagesDir } from '../lib/find-pages-dir'
...@@ -205,36 +206,57 @@ import { storeShuttle } from './flying-shuttle/store-shuttle' ...@@ -205,36 +206,57 @@ import { storeShuttle } from './flying-shuttle/store-shuttle'
import { stitchBuilds } from './flying-shuttle/stitch-builds' import { stitchBuilds } from './flying-shuttle/stitch-builds'
import { inlineStaticEnv } from './flying-shuttle/inline-static-env' import { inlineStaticEnv } from './flying-shuttle/inline-static-env'
import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback' import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback'
import { RenderingMode } from './rendering-mode'
interface ExperimentalBypassForInfo { type Fallback = null | boolean | string
experimentalBypassFor?: RouteHas[]
}
interface ExperimentalPPRInfo {
experimentalPPR: boolean | undefined
}
interface DataRouteRouteInfo { export interface SsgRoute {
dataRoute: string | null dataRoute: string | null
prefetchDataRoute: string | null | undefined experimentalBypassFor?: RouteHas[]
}
type Fallback = null | boolean | string /**
* The headers that should be served along side this prerendered route.
*/
initialHeaders?: Record<string, string>
/**
* The status code that should be served along side this prerendered route.
*/
initialStatus?: number
export interface SsgRoute /**
extends ExperimentalBypassForInfo, * The revalidation configuration for this route.
DataRouteRouteInfo, */
ExperimentalPPRInfo {
initialRevalidateSeconds: Revalidate initialRevalidateSeconds: Revalidate
/**
* The prefetch data route associated with this page. If not defined, this
* page does not support prefetching.
*/
prefetchDataRoute: string | null | undefined
/**
* The dynamic route that this statically prerendered route is based on. If
* this is null, then the route was not based on a dynamic route.
*/
srcRoute: string | null srcRoute: string | null
initialStatus?: number
initialHeaders?: Record<string, string> /**
* @deprecated use `renderingMode` instead
*/
experimentalPPR: boolean | undefined
/**
* The rendering mode for this route. Only `undefined` when not an app router
* route.
*/
renderingMode: RenderingMode | undefined
} }
export interface DynamicSsgRoute export interface DynamicSsgRoute {
extends ExperimentalBypassForInfo, dataRoute: string | null
DataRouteRouteInfo, dataRouteRegex: string | null
ExperimentalPPRInfo { experimentalBypassFor?: RouteHas[]
fallback: Fallback fallback: Fallback
/** /**
...@@ -242,12 +264,30 @@ export interface DynamicSsgRoute ...@@ -242,12 +264,30 @@ export interface DynamicSsgRoute
* route. * route.
*/ */
fallbackRevalidate: Revalidate | undefined fallbackRevalidate: Revalidate | undefined
fallbackStatus?: number
/**
* The headers that should used when serving the fallback.
*/
fallbackHeaders?: Record<string, string> fallbackHeaders?: Record<string, string>
routeRegex: string /**
dataRouteRegex: string | null * The status code that should be used when serving the fallback.
*/
fallbackStatus?: number
prefetchDataRoute: string | null | undefined
prefetchDataRouteRegex: string | null | undefined prefetchDataRouteRegex: string | null | undefined
routeRegex: string
/**
* @deprecated use `renderingMode` instead
*/
experimentalPPR: boolean | undefined
/**
* The rendering mode for this route. Only `undefined` when not an app router
* route.
*/
renderingMode: RenderingMode | undefined
} }
export type PrerenderManifest = { export type PrerenderManifest = {
...@@ -312,6 +352,7 @@ export type RoutesManifest = { ...@@ -312,6 +352,7 @@ export type RoutesManifest = {
rsc: { rsc: {
header: typeof RSC_HEADER header: typeof RSC_HEADER
didPostponeHeader: typeof NEXT_DID_POSTPONE_HEADER didPostponeHeader: typeof NEXT_DID_POSTPONE_HEADER
contentTypeHeader: typeof RSC_CONTENT_TYPE_HEADER
varyHeader: string varyHeader: string
prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER
suffix: typeof RSC_SUFFIX suffix: typeof RSC_SUFFIX
...@@ -319,6 +360,21 @@ export type RoutesManifest = { ...@@ -319,6 +360,21 @@ export type RoutesManifest = {
} }
skipMiddlewareUrlNormalize?: boolean skipMiddlewareUrlNormalize?: boolean
caseSensitive?: boolean caseSensitive?: boolean
/**
* Configuration related to Partial Prerendering.
*/
ppr?: {
/**
* The chained response for the PPR resume.
*/
chain: {
/**
* The headers that will indicate to Next.js that the request is for a PPR
* resume.
*/
headers: Record<string, string>
}
}
} }
function pageToRoute(page: string) { function pageToRoute(page: string) {
...@@ -1099,6 +1155,8 @@ export default async function build( ...@@ -1099,6 +1155,8 @@ export default async function build(
config.basePath ? `${config.basePath}${p}` : p config.basePath ? `${config.basePath}${p}` : p
) )
const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr)
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST) const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
const routesManifest: RoutesManifest = nextBuildSpan const routesManifest: RoutesManifest = nextBuildSpan
.traceChild('generate-routes-manifest') .traceChild('generate-routes-manifest')
...@@ -1143,7 +1201,16 @@ export default async function build( ...@@ -1143,7 +1201,16 @@ export default async function build(
prefetchSuffix: RSC_PREFETCH_SUFFIX, prefetchSuffix: RSC_PREFETCH_SUFFIX,
}, },
skipMiddlewareUrlNormalize: config.skipMiddlewareUrlNormalize, skipMiddlewareUrlNormalize: config.skipMiddlewareUrlNormalize,
} as RoutesManifest ppr: isAppPPREnabled
? {
chain: {
headers: {
[NEXT_RESUME_HEADER]: '1',
},
},
}
: undefined,
} satisfies RoutesManifest
}) })
if (rewrites.beforeFiles.length === 0 && rewrites.fallback.length === 0) { if (rewrites.beforeFiles.length === 0 && rewrites.fallback.length === 0) {
...@@ -1721,8 +1788,6 @@ export default async function build( ...@@ -1721,8 +1788,6 @@ export default async function build(
? await readManifest<AppBuildManifest>(appBuildManifestPath) ? await readManifest<AppBuildManifest>(appBuildManifestPath)
: undefined : undefined
const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr)
const appPathRoutes: Record<string, string> = {} const appPathRoutes: Record<string, string> = {}
if (appDir) { if (appDir) {
...@@ -2954,6 +3019,11 @@ export default async function build( ...@@ -2954,6 +3019,11 @@ export default async function build(
prerenderManifest.routes[route] = { prerenderManifest.routes[route] = {
initialStatus: meta.status, initialStatus: meta.status,
initialHeaders: meta.headers, initialHeaders: meta.headers,
renderingMode: isAppPPREnabled
? isRoutePPREnabled
? RenderingMode.PARTIALLY_STATIC
: RenderingMode.STATIC
: undefined,
experimentalPPR: isRoutePPREnabled, experimentalPPR: isRoutePPREnabled,
experimentalBypassFor: bypassFor, experimentalBypassFor: bypassFor,
initialRevalidateSeconds: revalidate, initialRevalidateSeconds: revalidate,
...@@ -3032,6 +3102,11 @@ export default async function build( ...@@ -3032,6 +3102,11 @@ export default async function build(
prerenderManifest.dynamicRoutes[route] = { prerenderManifest.dynamicRoutes[route] = {
experimentalPPR: isRoutePPREnabled, experimentalPPR: isRoutePPREnabled,
renderingMode: isAppPPREnabled
? isRoutePPREnabled
? RenderingMode.PARTIALLY_STATIC
: RenderingMode.STATIC
: undefined,
experimentalBypassFor: bypassFor, experimentalBypassFor: bypassFor,
routeRegex: normalizeRouteRegex( routeRegex: normalizeRouteRegex(
getNamedRouteRegex(route, false).re.source getNamedRouteRegex(route, false).re.source
...@@ -3284,6 +3359,7 @@ export default async function build( ...@@ -3284,6 +3359,7 @@ export default async function build(
exportResult.byPath.get(localePage)?.revalidate ?? exportResult.byPath.get(localePage)?.revalidate ??
false, false,
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: null, srcRoute: null,
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
...@@ -3298,6 +3374,7 @@ export default async function build( ...@@ -3298,6 +3374,7 @@ export default async function build(
initialRevalidateSeconds: initialRevalidateSeconds:
exportResult.byPath.get(page)?.revalidate ?? false, exportResult.byPath.get(page)?.revalidate ?? false,
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: null, srcRoute: null,
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
...@@ -3367,6 +3444,7 @@ export default async function build( ...@@ -3367,6 +3444,7 @@ export default async function build(
prerenderManifest.routes[route.path] = { prerenderManifest.routes[route.path] = {
initialRevalidateSeconds, initialRevalidateSeconds,
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: page, srcRoute: page,
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
...@@ -3453,6 +3531,7 @@ export default async function build( ...@@ -3453,6 +3531,7 @@ export default async function build(
getNamedRouteRegex(tbdRoute, false).re.source getNamedRouteRegex(tbdRoute, false).re.source
), ),
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined,
dataRoute, dataRoute,
fallback: ssgBlockingFallbackPages.has(tbdRoute) fallback: ssgBlockingFallbackPages.has(tbdRoute)
? null ? null
......
/**
* The rendering mode for a route.
*/
export const enum RenderingMode {
/**
* `STATIC` rendering mode will output a fully static HTML page or error if
* anything dynamic is used.
*/
STATIC = 'STATIC',
/**
* `PARTIALLY_STATIC` rendering mode will output a fully static HTML page if
* the route is fully static, but will output a partially static HTML page if
* the route uses uses any dynamic API's.
*/
PARTIALLY_STATIC = 'PARTIALLY_STATIC',
}
...@@ -20,6 +20,8 @@ export const NEXT_CACHE_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags' ...@@ -20,6 +20,8 @@ export const NEXT_CACHE_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags'
export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER = export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER =
'x-next-revalidate-tag-token' 'x-next-revalidate-tag-token'
export const NEXT_RESUME_HEADER = 'next-resume'
// if these change make sure we update the related // if these change make sure we update the related
// documentation as well // documentation as well
export const NEXT_CACHE_TAG_MAX_ITEMS = 64 export const NEXT_CACHE_TAG_MAX_ITEMS = 64
......
...@@ -132,7 +132,11 @@ import { ...@@ -132,7 +132,11 @@ import {
normalizeNextQueryParam, normalizeNextQueryParam,
toNodeOutgoingHttpHeaders, toNodeOutgoingHttpHeaders,
} from './web/utils' } from './web/utils'
import { CACHE_ONE_YEAR, NEXT_CACHE_TAGS_HEADER } from '../lib/constants' import {
CACHE_ONE_YEAR,
NEXT_CACHE_TAGS_HEADER,
NEXT_RESUME_HEADER,
} from '../lib/constants'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { import {
NextRequestAdapter, NextRequestAdapter,
...@@ -439,6 +443,9 @@ export default abstract class Server< ...@@ -439,6 +443,9 @@ export default abstract class Server<
protected readonly localeNormalizer?: LocaleRouteNormalizer protected readonly localeNormalizer?: LocaleRouteNormalizer
protected readonly normalizers: { protected readonly normalizers: {
/**
* @deprecated
*/
readonly postponed: PostponedPathnameNormalizer | undefined readonly postponed: PostponedPathnameNormalizer | undefined
readonly rsc: RSCPathnameNormalizer | undefined readonly rsc: RSCPathnameNormalizer | undefined
readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined
...@@ -1082,6 +1089,25 @@ export default abstract class Server< ...@@ -1082,6 +1089,25 @@ export default abstract class Server<
) )
} }
} }
// In minimal mode, if PPR is enabled, then we should check to see if
// the request should be a resume request.
else if (
this.isAppPPREnabled &&
this.minimalMode &&
req.headers[NEXT_RESUME_HEADER] === '1' &&
req.method === 'POST'
) {
// Decode the postponed state from the request body, it will come as
// an array of buffers, so collect them and then concat them to form
// the string.
const body: Array<Buffer> = []
for await (const chunk of req.body) {
body.push(chunk)
}
const postponed = Buffer.concat(body).toString('utf8')
addRequestMeta(req, 'postponed', postponed)
}
matchedPath = this.normalize(matchedPath) matchedPath = this.normalize(matchedPath)
const normalizedUrlPath = this.stripNextDataPath(urlPathname) const normalizedUrlPath = this.stripNextDataPath(urlPathname)
...@@ -2052,16 +2078,14 @@ export default abstract class Server< ...@@ -2052,16 +2078,14 @@ export default abstract class Server<
typeof query.__nextppronly !== 'undefined' && typeof query.__nextppronly !== 'undefined' &&
couldSupportPPR couldSupportPPR
// This page supports PPR if it has `experimentalPPR` set to `true` in the // This page supports PPR if it is marked as being `PARTIALLY_STATIC` in the
// prerender manifest and this is an app page. // prerender manifest and this is an app page.
const isRoutePPREnabled: boolean = const isRoutePPREnabled: boolean =
couldSupportPPR && couldSupportPPR &&
// In production, we'd expect to see the `experimentalPPR` flag set in the
// prerender manifest.
(( ((
prerenderManifest.routes[pathname] ?? prerenderManifest.routes[pathname] ??
prerenderManifest.dynamicRoutes[pathname] prerenderManifest.dynamicRoutes[pathname]
)?.experimentalPPR === true || )?.renderingMode === 'PARTIALLY_STATIC' ||
// Ideally we'd want to check the appConfig to see if this page has PPR // Ideally we'd want to check the appConfig to see if this page has PPR
// enabled or not, but that would require plumbing the appConfig through // enabled or not, but that would require plumbing the appConfig through
// to the server during development. We assume that the page supports it // to the server during development. We assume that the page supports it
......
import { RenderingMode } from '../../../build/rendering-mode'
import { SharedRevalidateTimings } from './shared-revalidate-timings' import { SharedRevalidateTimings } from './shared-revalidate-timings'
describe('SharedRevalidateTimings', () => { describe('SharedRevalidateTimings', () => {
...@@ -13,6 +14,7 @@ describe('SharedRevalidateTimings', () => { ...@@ -13,6 +14,7 @@ describe('SharedRevalidateTimings', () => {
srcRoute: null, srcRoute: null,
prefetchDataRoute: null, prefetchDataRoute: null,
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
}, },
'/route2': { '/route2': {
initialRevalidateSeconds: 20, initialRevalidateSeconds: 20,
...@@ -20,6 +22,7 @@ describe('SharedRevalidateTimings', () => { ...@@ -20,6 +22,7 @@ describe('SharedRevalidateTimings', () => {
srcRoute: null, srcRoute: null,
prefetchDataRoute: null, prefetchDataRoute: null,
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
}, },
}, },
dynamicRoutes: {}, dynamicRoutes: {},
......
...@@ -116,11 +116,21 @@ describe('required server files app router', () => { ...@@ -116,11 +116,21 @@ describe('required server files app router', () => {
expect(next.cliOutput).not.toContain('ERR_INVALID_URL') expect(next.cliOutput).not.toContain('ERR_INVALID_URL')
}) })
it('should properly stream resume', async () => { it.each([
const res = await fetchViaHTTP(appPort, '/delayed', undefined, { {
name: 'with Next-Resume',
headers: { headers: {
'x-matched-path': '/_next/postponed/resume/delayed', 'x-matched-path': '/delayed',
'next-resume': '1',
}, },
},
{
name: 'without Next-Resume',
headers: { 'x-matched-path': '/_next/postponed/resume/delayed' },
},
])('should properly stream resume $name', async ({ headers }) => {
const res = await fetchViaHTTP(appPort, '/delayed', undefined, {
headers,
method: 'POST', method: 'POST',
body: delayedPostpone, body: delayedPostpone,
}) })
...@@ -204,7 +214,21 @@ describe('required server files app router', () => { ...@@ -204,7 +214,21 @@ describe('required server files app router', () => {
}) })
describe('middleware rewrite', () => { describe('middleware rewrite', () => {
it('should work with a dynamic path', async () => { it.each([
{
name: 'with Next-Resume',
headers: {
'x-matched-path': '/rewrite/first-cookie',
'next-resume': '1',
},
},
{
name: 'without Next-Resume',
headers: {
'x-matched-path': '/_next/postponed/resume/rewrite/first-cookie',
},
},
])('should work with a dynamic path ($name)', async ({ headers }) => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
'/rewrite-with-cookie', '/rewrite-with-cookie',
...@@ -212,7 +236,8 @@ describe('required server files app router', () => { ...@@ -212,7 +236,8 @@ describe('required server files app router', () => {
{ {
method: 'POST', method: 'POST',
headers: { headers: {
'x-matched-path': '/_next/postponed/resume/rewrite/first-cookie', 'x-matched-path': '/rewrite/first-cookie',
'next-resume': '1',
}, },
body: rewritePostpone, body: rewritePostpone,
} }
...@@ -227,26 +252,41 @@ describe('required server files app router', () => { ...@@ -227,26 +252,41 @@ describe('required server files app router', () => {
}) })
}) })
it('should still render when postponed is corrupted', async () => { it.each([
const random = Math.random().toString(36).substring(2) {
name: 'with Next-Resume',
const res = await fetchViaHTTP(appPort, '/dyn/' + random, undefined, { headers: () => ({
method: 'POST', 'x-matched-path': '/dyn/[slug]',
headers: { 'next-resume': '1',
}),
},
{
name: 'without Next-Resume',
headers: (random) => ({
'x-matched-path': '/_next/postponed/resume/dyn/' + random, 'x-matched-path': '/_next/postponed/resume/dyn/' + random,
}, }),
// This is a corrupted postponed JSON payload. },
body: '{', ])(
}) 'should still render when postponed is corrupted $name',
async ({ headers }) => {
const random = Math.random().toString(36).substring(2)
const res = await fetchViaHTTP(appPort, '/dyn/' + random, undefined, {
method: 'POST',
headers: headers(random),
// This is a corrupted postponed JSON payload.
body: '{',
})
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
// Expect that the closing HTML tag is still present, indicating a // Expect that the closing HTML tag is still present, indicating a
// successful render. // successful render.
expect(html).toContain('</html>') expect(html).toContain('</html>')
}) }
)
it('should send cache tags in minimal mode for ISR', async () => { it('should send cache tags in minimal mode for ISR', async () => {
for (const [path, tags] of [ for (const [path, tags] of [
...@@ -314,7 +354,7 @@ describe('required server files app router', () => { ...@@ -314,7 +354,7 @@ describe('required server files app router', () => {
it('should handle RSC requests', async () => { it('should handle RSC requests', async () => {
const res = await fetchViaHTTP(appPort, '/dyn/first.rsc', undefined, { const res = await fetchViaHTTP(appPort, '/dyn/first.rsc', undefined, {
headers: { headers: {
'x-matched-path': '/dyn/first', 'x-matched-path': '/dyn/[slug]',
}, },
}) })
...@@ -330,7 +370,7 @@ describe('required server files app router', () => { ...@@ -330,7 +370,7 @@ describe('required server files app router', () => {
undefined, undefined,
{ {
headers: { headers: {
'x-matched-path': '/dyn/first', 'x-matched-path': '/dyn/[slug]',
}, },
} }
) )
......
...@@ -16579,6 +16579,8 @@ ...@@ -16579,6 +16579,8 @@
"passed": [], "passed": [],
"failed": [ "failed": [
"required server files app router middleware rewrite should work with a dynamic path", "required server files app router middleware rewrite should work with a dynamic path",
"required server files app router middleware rewrite should work with a dynamic path (with Next-Resume)",
"required server files app router middleware rewrite should work with a dynamic path (without Next-Resume)",
"required server files app router should handle RSC requests", "required server files app router should handle RSC requests",
"required server files app router should handle prefetch RSC requests", "required server files app router should handle prefetch RSC requests",
"required server files app router should not fail caching", "required server files app router should not fail caching",
...@@ -16587,8 +16589,12 @@ ...@@ -16587,8 +16589,12 @@
"required server files app router should properly handle fallback for bot request", "required server files app router should properly handle fallback for bot request",
"required server files app router should properly handle prerender for bot request", "required server files app router should properly handle prerender for bot request",
"required server files app router should properly stream resume", "required server files app router should properly stream resume",
"required server files app router should properly stream resume with Next-Resume",
"required server files app router should properly stream resume without Next-Resume",
"required server files app router should send cache tags in minimal mode for ISR", "required server files app router should send cache tags in minimal mode for ISR",
"required server files app router should still render when postponed is corrupted" "required server files app router should still render when postponed is corrupted",
"required server files app router should still render when postponed is corrupted with Next-Resume",
"required server files app router should still render when postponed is corrupted without Next-Resume"
], ],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
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