Unverified Commit dfd3c260 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure outputs are correct with cache components in deployment adapters (#87018)

Updates our outputs to ensure we provide correct metadata and ISR pairs
when cache components are enabled.

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
parent 840643f2
......@@ -174,37 +174,50 @@ export interface AdapterOutput {
/**
* fallback is initial cache data generated during build for a prerender
*/
fallback?: {
/**
* path to the fallback file can be HTML/JSON/RSC
*/
filePath: string
/**
* initialStatus is the status code that should be applied
* when serving the fallback
*/
initialStatus?: number
/**
* initialHeaders are the headers that should be sent when
* serving the fallback
*/
initialHeaders?: Record<string, string | string[]>
/**
* initial expiration is how long until the fallback entry
* is considered expired and no longer valid to serve
*/
initialExpiration?: number
/**
* initial revalidate is how long until the fallback is
* considered stale and should be revalidated
*/
initialRevalidate?: Revalidate
/**
* postponedState is the PPR state when it postponed and is used for resuming
*/
postponedState?: string
}
fallback?:
| {
/**
* path to the fallback file can be HTML/JSON/RSC,
*/
filePath: string
/**
* initialStatus is the status code that should be applied
* when serving the fallback
*/
initialStatus?: number
/**
* initialHeaders are the headers that should be sent when
* serving the fallback
*/
initialHeaders?: Record<string, string | string[]>
/**
* initial expiration is how long until the fallback entry
* is considered expired and no longer valid to serve
*/
initialExpiration?: number
/**
* initial revalidate is how long until the fallback is
* considered stale and should be revalidated
*/
initialRevalidate?: Revalidate
/**
* postponedState is the PPR state when it postponed and is used for resuming
*/
postponedState?: string
}
| {
/*
a fallback filePath can be omitted when postponedState is
present which signals the fallback should just resume with
the postpone state but doesn't have fallback to seed cache
*/
postponedState: string
initialExpiration?: number
initialRevalidate?: Revalidate
initialHeaders?: Record<string, string | string[]>
initialStatus?: number
}
/**
* config related to the route
*/
......@@ -1164,7 +1177,6 @@ export async function handleBuildComplete({
initialRevalidateSeconds: initialRevalidate,
initialHeaders,
initialStatus,
prefetchDataRoute,
dataRoute,
renderingMode,
allowHeader,
......@@ -1273,10 +1285,11 @@ export async function handleBuildComplete({
outputs.prerenders.push(initialOutput)
if (dataRoute) {
let dataFilePath = path.join(
let dataFilePath: string | undefined = path.join(
pagesDistDir,
`${normalizePagePath(route)}.json`
)
let postponed = meta.postponed
if (isAppPage) {
// When experimental PPR is enabled, we expect that the data
......@@ -1284,32 +1297,52 @@ export async function handleBuildComplete({
// be from the prefetch data route. If this isn't enabled
// for ppr, the only way to get the data is from the data
// route.
dataFilePath = path.join(
appDistDir,
prefetchDataRoute &&
renderingMode === RenderingMode.PARTIALLY_STATIC
? prefetchDataRoute
: dataRoute
)
dataFilePath = path.join(appDistDir, dataRoute)
}
outputs.prerenders.push({
...initialOutput,
id: dataRoute,
pathname: dataRoute,
fallback: isNotFoundTrue
? undefined
: {
...initialOutput.fallback,
initialHeaders: {
...initialOutput.fallback?.initialHeaders,
'content-type': isAppPage
? rscContentTypeHeader
: JSON_CONTENT_TYPE_HEADER,
if (
renderingMode === RenderingMode.PARTIALLY_STATIC &&
!(await cachedFilePathCheck(dataFilePath))
) {
// TODO: allowQuery should diverge based on app client param
// parsing flag
outputs.prerenders.push({
...initialOutput,
id: dataRoute,
pathname: dataRoute,
fallback: !postponed
? undefined
: {
...initialOutput.fallback,
postponedState: postponed,
initialHeaders: {
...initialOutput.fallback?.initialHeaders,
'content-type': isAppPage
? rscContentTypeHeader
: JSON_CONTENT_TYPE_HEADER,
},
filePath: undefined,
},
filePath: dataFilePath,
},
})
})
} else {
outputs.prerenders.push({
...initialOutput,
id: dataRoute,
pathname: dataRoute,
fallback: isNotFoundTrue
? undefined
: {
...initialOutput.fallback,
initialHeaders: {
...initialOutput.fallback?.initialHeaders,
'content-type': isAppPage
? rscContentTypeHeader
: JSON_CONTENT_TYPE_HEADER,
},
filePath: dataFilePath,
},
})
}
}
if (isAppPage) {
......@@ -1325,13 +1358,16 @@ export async function handleBuildComplete({
fallbackRevalidate,
fallbackHeaders,
fallbackStatus,
fallbackSourceRoute,
allowHeader,
dataRoute,
renderingMode,
experimentalBypassFor,
} = prerenderManifest.dynamicRoutes[dynamicRoute]
const isAppPage = Boolean(appOutputMap[dynamicRoute])
const srcRoute = fallbackSourceRoute || dynamicRoute
const parentOutput = getParentOutput(srcRoute, dynamicRoute)
const isAppPage = Boolean(appOutputMap[srcRoute])
const allowQuery = Object.values(
routesManifest.dynamicRoutes.find(
......@@ -1344,7 +1380,7 @@ export async function handleBuildComplete({
id: dynamicRoute,
type: AdapterOutputType.PRERENDER,
pathname: dynamicRoute,
parentOutputId: getParentOutput(dynamicRoute, dynamicRoute).id,
parentOutputId: parentOutput.id,
groupId: prerenderGroupId,
config: {
allowQuery,
......@@ -1380,7 +1416,25 @@ export async function handleBuildComplete({
await handleAppMeta(dynamicRoute, initialOutput, meta)
}
if (dataRoute) {
if (renderingMode === RenderingMode.PARTIALLY_STATIC) {
outputs.prerenders.push({
...initialOutput,
id: `${dynamicRoute}.rsc`,
pathname: `${dynamicRoute}.rsc`,
fallback: meta.postponed
? {
...initialOutput.fallback,
postponedState: meta.postponed,
initialHeaders: {
...initialOutput.fallback?.initialHeaders,
'content-type': isAppPage
? rscContentTypeHeader
: JSON_CONTENT_TYPE_HEADER,
},
}
: undefined,
})
} else if (dataRoute) {
outputs.prerenders.push({
...initialOutput,
id: dataRoute,
......@@ -1521,7 +1575,7 @@ export async function handleBuildComplete({
route.page
) + getDestinationQuery(route.routeKeys)
if (appPageKeys && appPageKeys.length > 0 && config.cacheComponents) {
if (appPageKeys && appPageKeys.length > 0) {
// If we have fallback root params (implying we've already
// emitted a rewrite for the /_tree request), or if the route
// has PPR enabled and client param parsing is enabled, then
......
......@@ -147,8 +147,6 @@ export function getDefineEnv({
: process.env.NEXT_RSPACK
? 'Rspack'
: 'Webpack',
// minimal mode is enforced when an adapter is configured
'process.env.MINIMAL_MODE': Boolean(config.experimental.adapterPath),
// TODO: enforce `NODE_ENV` on `process.env`, and add a test:
'process.env.NODE_ENV':
dev || config.experimental.allowDevelopmentBuild
......
......@@ -121,9 +121,7 @@ export async function handler(
if (routeModule.isDev) {
addRequestMeta(req, 'devRequestTimingInternalsEnd', process.hrtime.bigint())
}
const isMinimalMode = Boolean(
process.env.MINIMAL_MODE || getRequestMeta(req, 'minimalMode')
)
const isMinimalMode = Boolean(getRequestMeta(req, 'minimalMode'))
let srcPage = 'VAR_DEFINITION_PAGE'
......
......@@ -280,9 +280,7 @@ export async function handler(
}
})
}
const isMinimalMode = Boolean(
process.env.MINIMAL_MODE || getRequestMeta(req, 'minimalMode')
)
const isMinimalMode = Boolean(getRequestMeta(req, 'minimalMode'))
const handleResponse = async (currentSpan?: Span) => {
const responseGenerator: ResponseGenerator = async ({
......
......@@ -102,9 +102,7 @@ export const getHandler = ({
return
}
const isMinimalMode = Boolean(
process.env.MINIMAL_MODE || getRequestMeta(req, 'minimalMode')
)
const isMinimalMode = Boolean(getRequestMeta(req, 'minimalMode'))
const render404 = async () => {
// TODO: should route-module itself handle rendering the 404
......
......@@ -965,10 +965,7 @@ export abstract class RouteModule<
public getResponseCache(req: IncomingMessage | BaseNextRequest) {
if (!this.responseCache) {
const minimalMode =
(Boolean(process.env.MINIMAL_MODE) ||
getRequestMeta(req, 'minimalMode')) ??
false
const minimalMode = getRequestMeta(req, 'minimalMode') ?? false
this.responseCache = new ResponseCache(minimalMode)
}
return this.responseCache
......
/**
* This adapter is not modifying outputs or the test
* it is just adding additional assertions ensuring
* we provide the expected outputs and file paths are valid
*/
import fs from 'fs'
// @ts-check
/** @type {import('next').NextAdapter } */
const myAdapter = {
name: 'my-custom-adapter',
modifyConfig: (config, { phase }) => {
if (process.env.NODE_ENV !== 'production') return config
if (typeof phase !== 'string') {
throw new Error(`invalid phase value provided to modifyConfig ${phase}`)
}
console.log('called modify config in adapter with phase', phase)
return config
},
onBuildComplete: async (ctx) => {
console.log('onBuildComplete called')
// Validate all output file paths exist on the filesystem
const allOutputs = [
...ctx.outputs.pages,
...ctx.outputs.pagesApi,
...ctx.outputs.appPages,
...ctx.outputs.appRoutes,
...ctx.outputs.prerenders,
...ctx.outputs.staticFiles,
]
if (ctx.outputs.middleware) {
allOutputs.push(ctx.outputs.middleware)
}
const validationErrors = []
// Check that all filePaths in outputs exist
for (const output of allOutputs) {
if (output.filePath) {
try {
await fs.promises.access(output.filePath, fs.constants.F_OK)
} catch (err) {
validationErrors.push(
`Missing file for output ${output.id}: ${output.filePath}`
)
}
}
// Check fallback filePath for prerenders
if (output.type === 'PRERENDER' && output.fallback) {
if (output.fallback.filePath) {
try {
await fs.promises.access(
output.fallback.filePath,
fs.constants.F_OK
)
} catch (err) {
validationErrors.push(
`Missing fallback file for prerender ${output.id}: ${JSON.stringify(output, null, 2)}`
)
}
} else if (!output.fallback.postponedState) {
throw new Error(
`Missing postponed state or filePath for prerender ${output.id} ${JSON.stringify(output, null, 2)}`
)
}
}
// Check assets
if (output.assets) {
for (const [key, assetPath] of Object.entries(output.assets)) {
try {
await fs.promises.access(assetPath, fs.constants.F_OK)
} catch (err) {
validationErrors.push(
`Missing asset file for output ${output.id} (${key}): ${assetPath}`
)
}
}
}
// Check wasmAssets
if (output.wasmAssets) {
for (const [key, wasmPath] of Object.entries(output.wasmAssets)) {
try {
await fs.promises.access(wasmPath, fs.constants.F_OK)
} catch (err) {
validationErrors.push(
`Missing wasm file for output ${output.id} (${key}): ${wasmPath}`
)
}
}
}
}
// Validate that segment routes are present in routing.dynamicRoutes
// Segment routes match the pattern: .segments/.+.segment.rsc
const segmentRoutes = ctx.routing.dynamicRoutes.filter((route) => {
// Check if the source or destination contains segment routes
return (
route.sourceRegex.includes('.segments/') ||
route.sourceRegex.includes('.segment.rsc')
)
})
// Ensure we have segment routes when we have app pages
if (ctx.outputs.appPages.length > 0) {
if (segmentRoutes.length === 0) {
validationErrors.push(
'Expected segment routes in routing.dynamicRoutes when app pages exist'
)
} else {
console.log(
`Found ${segmentRoutes.length} segment routes in routing.dynamicRoutes`
)
}
}
// Validate that all appPages have matching .rsc and non .rsc pathnames
const appPagePathnames = new Map()
for (const appPage of ctx.outputs.appPages) {
const pathname = appPage.pathname
if (pathname.endsWith('.rsc')) {
const basePathname = pathname.slice(0, -4) // Remove .rsc extension
if (!appPagePathnames.has(basePathname)) {
appPagePathnames.set(basePathname, { rsc: false, nonRsc: false })
}
appPagePathnames.get(basePathname).rsc = true
} else {
if (!appPagePathnames.has(pathname)) {
appPagePathnames.set(pathname, { rsc: false, nonRsc: false })
}
appPagePathnames.get(pathname).nonRsc = true
}
}
// Check that each pathname has both .rsc and non .rsc versions
for (const [pathname, versions] of appPagePathnames.entries()) {
if (!versions.rsc) {
validationErrors.push(
`App page ${pathname} is missing corresponding .rsc pathname`
)
}
if (!versions.nonRsc) {
validationErrors.push(
`App page ${pathname}.rsc is missing corresponding non .rsc pathname`
)
}
}
if (appPagePathnames.size > 0) {
console.log(
`Validated ${appPagePathnames.size} app page pathname(s) have matching .rsc and non .rsc versions`
)
}
if (validationErrors.length > 0) {
console.error('Validation errors:')
for (const error of validationErrors) {
console.error(` - ${error}`)
}
throw new Error(
`Adapter validation failed with ${validationErrors.length} error(s)`
)
}
console.log('Validation passed: All output files exist on filesystem')
console.log(
`Segment routes validated: ${segmentRoutes.length} routes found`
)
await fs.promises.writeFile(
'build-complete.json',
JSON.stringify(ctx, null, 2)
)
},
}
export default myAdapter
......@@ -3,6 +3,9 @@
*/
const nextConfig = {
cacheComponents: true,
experimental: {
adapterPath: require.resolve('./my-adapter.mjs'),
},
}
module.exports = nextConfig
......@@ -110,10 +110,15 @@ describe('adapter-config', () => {
try {
expect(prerenderOutput.parentOutputId).toBeTruthy()
if (prerenderOutput.fallback) {
const stats = await fs.promises.stat(
if (
'filePath' in prerenderOutput.fallback &&
prerenderOutput.fallback.filePath
)
expect(stats.isFile()).toBe(true)
) {
const stats = await fs.promises.stat(
prerenderOutput.fallback.filePath
)
expect(stats.isFile()).toBe(true)
}
expect(prerenderOutput.fallback.initialRevalidate).toBeDefined()
}
......
/**
* This adapter is not modifying outputs or the test
* it is just adding additional assertions ensuring
* we provide the expected outputs and file paths are valid
*/
import fs from 'fs'
// @ts-check
......@@ -16,6 +21,170 @@ const myAdapter = {
onBuildComplete: async (ctx) => {
console.log('onBuildComplete called')
// Validate all output file paths exist on the filesystem
const allOutputs = [
...ctx.outputs.pages,
...ctx.outputs.pagesApi,
...ctx.outputs.appPages,
...ctx.outputs.appRoutes,
...ctx.outputs.prerenders,
...ctx.outputs.staticFiles,
]
if (ctx.outputs.middleware) {
allOutputs.push(ctx.outputs.middleware)
}
const validationErrors = []
// Check that all filePaths in outputs exist
for (const output of allOutputs) {
if (output.filePath) {
try {
await fs.promises.access(output.filePath, fs.constants.F_OK)
} catch (err) {
validationErrors.push(
`Missing file for output ${output.id}: ${output.filePath}`
)
}
}
// Check fallback filePath for prerenders
if (output.type === 'PRERENDER' && output.fallback) {
if (output.fallback.filePath) {
try {
await fs.promises.access(
output.fallback.filePath,
fs.constants.F_OK
)
} catch (err) {
validationErrors.push(
`Missing fallback file for prerender ${output.id}: ${JSON.stringify(output, null, 2)}`
)
}
} else if (!output.fallback.postponedState) {
throw new Error(
`Missing postponed state or filePath for prerender ${output.id} ${JSON.stringify(output, null, 2)}`
)
}
}
// Check assets
if (output.assets) {
for (const [key, assetPath] of Object.entries(output.assets)) {
try {
await fs.promises.access(assetPath, fs.constants.F_OK)
} catch (err) {
validationErrors.push(
`Missing asset file for output ${output.id} (${key}): ${assetPath}`
)
}
}
}
// Check wasmAssets
if (output.wasmAssets) {
for (const [key, wasmPath] of Object.entries(output.wasmAssets)) {
try {
await fs.promises.access(wasmPath, fs.constants.F_OK)
} catch (err) {
validationErrors.push(
`Missing wasm file for output ${output.id} (${key}): ${wasmPath}`
)
}
}
}
}
// Validate that segment routes are present in routing.dynamicRoutes
// Segment routes match the pattern: .segments/.+.segment.rsc
const segmentRoutes = ctx.routing.dynamicRoutes.filter((route) => {
// Check if the source or destination contains segment routes
return (
route.sourceRegex.includes('.segments/') ||
route.sourceRegex.includes('.segment.rsc')
)
})
// Ensure we have segment routes when we have app pages
if (ctx.outputs.appPages.length > 0) {
if (segmentRoutes.length === 0) {
validationErrors.push(
'Expected segment routes in routing.dynamicRoutes when app pages exist'
)
} else {
console.log(
`Found ${segmentRoutes.length} segment routes in routing.dynamicRoutes`
)
}
}
// Validate that all appPages have matching .rsc and non .rsc pathnames
const appPagePathnames = new Map()
// Helper function to normalize /index to / for matching
const normalizePathname = (pathname) => {
if (pathname.endsWith('/index')) {
return pathname.slice(0, -6) || '/'
}
return pathname
}
for (const appPage of ctx.outputs.appPages) {
const pathname = appPage.pathname
if (pathname.endsWith('.rsc')) {
const basePathname = normalizePathname(pathname.slice(0, -4)) // Remove .rsc extension and normalize
if (!appPagePathnames.has(basePathname)) {
appPagePathnames.set(basePathname, { rsc: false, nonRsc: false })
}
appPagePathnames.get(basePathname).rsc = true
} else {
const normalizedPathname = normalizePathname(pathname)
if (!appPagePathnames.has(normalizedPathname)) {
appPagePathnames.set(normalizedPathname, {
rsc: false,
nonRsc: false,
})
}
appPagePathnames.get(normalizedPathname).nonRsc = true
}
}
// Check that each pathname has both .rsc and non .rsc versions
for (const [pathname, versions] of appPagePathnames.entries()) {
if (!versions.rsc) {
validationErrors.push(
`App page ${pathname} is missing corresponding .rsc pathname`
)
}
if (!versions.nonRsc) {
validationErrors.push(
`App page ${pathname}.rsc is missing corresponding non .rsc pathname`
)
}
}
if (appPagePathnames.size > 0) {
console.log(
`Validated ${appPagePathnames.size} app page pathname(s) have matching .rsc and non .rsc versions`
)
}
if (validationErrors.length > 0) {
console.error('Validation errors:')
for (const error of validationErrors) {
console.error(` - ${error}`)
}
throw new Error(
`Adapter validation failed with ${validationErrors.length} error(s)`
)
}
console.log('Validation passed: All output files exist on filesystem')
console.log(
`Segment routes validated: ${segmentRoutes.length} routes found`
)
await fs.promises.writeFile(
'build-complete.json',
JSON.stringify(ctx, null, 2)
......
......@@ -5,8 +5,8 @@ const require = Module.createRequire(import.meta.url)
const nextConfig = {
experimental: {
adapterPath: require.resolve('./my-adapter.mjs'),
cacheComponents: process.env.TEST_CACHE_COMPONENTS === '1',
},
cacheComponents: process.env.TEST_CACHE_COMPONENTS === '1',
rewrites() {
return [
{
......
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