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

Add handling for static generation in app (#40561)



x-ref: [internal notes](https://www.notion.so/vercel/App-Static-Generation-dc5f1e0916684501b586e56a5b7b9483)
parent 4a53582e
...@@ -97,6 +97,7 @@ import { ...@@ -97,6 +97,7 @@ import {
printTreeView, printTreeView,
copyTracedFiles, copyTracedFiles,
isReservedPage, isReservedPage,
AppConfig,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin' import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
...@@ -1065,6 +1066,11 @@ export default async function build( ...@@ -1065,6 +1066,11 @@ export default async function build(
const serverPropsPages = new Set<string>() const serverPropsPages = new Set<string>()
const additionalSsgPaths = new Map<string, Array<string>>() const additionalSsgPaths = new Map<string, Array<string>>()
const additionalSsgPathsEncoded = new Map<string, Array<string>>() const additionalSsgPathsEncoded = new Map<string, Array<string>>()
const appStaticPaths = new Map<string, Array<string>>()
const appStaticPathsEncoded = new Map<string, Array<string>>()
const appNormalizedPaths = new Map<string, string>()
const appDynamicParamPaths = new Set<string>()
const appDefaultConfigs = new Map<string, AppConfig>()
const pageTraceIncludes = new Map<string, Array<string>>() const pageTraceIncludes = new Map<string, Array<string>>()
const pageTraceExcludes = new Map<string, Array<string>>() const pageTraceExcludes = new Map<string, Array<string>>()
const pageInfos = new Map<string, PageInfo>() const pageInfos = new Map<string, PageInfo>()
...@@ -1087,6 +1093,26 @@ export default async function build( ...@@ -1087,6 +1093,26 @@ export default async function build(
: require.resolve('./utils') : require.resolve('./utils')
let infoPrinted = false let infoPrinted = false
let appPathsManifest: Record<string, string> = {}
const appPathRoutes: Record<string, string> = {}
if (appDir) {
appPathsManifest = JSON.parse(
await promises.readFile(
path.join(distDir, serverDir, APP_PATHS_MANIFEST),
'utf8'
)
)
Object.keys(appPathsManifest).forEach((entry) => {
appPathRoutes[entry] = normalizeAppPath(entry) || '/'
})
await promises.writeFile(
path.join(distDir, APP_PATH_ROUTES_MANIFEST),
JSON.stringify(appPathRoutes, null, 2)
)
}
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD
const staticWorkers = new Worker(staticWorker, { const staticWorkers = new Worker(staticWorker, {
...@@ -1255,33 +1281,49 @@ export default async function build( ...@@ -1255,33 +1281,49 @@ export default async function build(
let isHybridAmp = false let isHybridAmp = false
let ssgPageRoutes: string[] | null = null let ssgPageRoutes: string[] | null = null
const pagePath = let pagePath = ''
pageType === 'pages'
? pagesPaths.find( if (pageType === 'pages') {
(p) => pagePath =
p.startsWith(actualPage + '.') || pagesPaths.find(
p.startsWith(actualPage + '/index.') (p) =>
p.startsWith(actualPage + '.') ||
p.startsWith(actualPage + '/index.')
) || ''
}
let originalAppPath: string | undefined
if (pageType === 'app' && mappedAppPages) {
for (const [originalPath, normalizedPath] of Object.entries(
appPathRoutes
)) {
if (normalizedPath === page) {
pagePath = mappedAppPages[originalPath].replace(
/^private-next-app-dir/,
''
) )
: appPaths?.find((p) => p.startsWith(actualPage + '/page.')) originalAppPath = originalPath
break
}
}
}
const staticInfo = const staticInfo = pagePath
pagesDir && pageType === 'pages' && pagePath ? await getPageStaticInfo({
? await getPageStaticInfo({ pageFilePath: join(
pageFilePath: join(pagesDir, pagePath), (pageType === 'pages' ? pagesDir : appDir) || '',
nextConfig: config, pagePath
}) ),
: {} nextConfig: config,
const pageRuntime = staticInfo.runtime })
: undefined
const pageRuntime = staticInfo?.runtime
isServerComponent = isServerComponent =
pageType === 'app' && pageType === 'app' &&
staticInfo.rsc !== RSC_MODULE_TYPES.client staticInfo?.rsc !== RSC_MODULE_TYPES.client
if ( if (!isReservedPage(page)) {
// Only calculate page static information if the page is not an
// app page.
pageType !== 'app' &&
!isReservedPage(page)
) {
try { try {
let edgeInfo: any let edgeInfo: any
...@@ -1291,8 +1333,10 @@ export default async function build( ...@@ -1291,8 +1333,10 @@ export default async function build(
serverDir, serverDir,
MIDDLEWARE_MANIFEST MIDDLEWARE_MANIFEST
)) ))
const manifestKey =
pageType === 'pages' ? page : join(page, 'page')
edgeInfo = manifest.functions[page] edgeInfo = manifest.functions[manifestKey]
} }
let isPageStaticSpan = let isPageStaticSpan =
...@@ -1301,6 +1345,7 @@ export default async function build( ...@@ -1301,6 +1345,7 @@ export default async function build(
() => { () => {
return staticWorkers.isPageStatic({ return staticWorkers.isPageStatic({
page, page,
originalAppPath,
distDir, distDir,
serverless: isLikeServerless, serverless: isLikeServerless,
configFileName, configFileName,
...@@ -1311,10 +1356,50 @@ export default async function build( ...@@ -1311,10 +1356,50 @@ export default async function build(
parentId: isPageStaticSpan.id, parentId: isPageStaticSpan.id,
pageRuntime, pageRuntime,
edgeInfo, edgeInfo,
pageType,
hasServerComponents,
}) })
} }
) )
if (pageType === 'app' && originalAppPath) {
appNormalizedPaths.set(originalAppPath, page)
// TODO-APP: handle prerendering with edge
// runtime
if (pageRuntime === 'experimental-edge') {
return
}
if (
workerResult.encodedPrerenderRoutes &&
workerResult.prerenderRoutes
) {
appStaticPaths.set(
originalAppPath,
workerResult.prerenderRoutes
)
appStaticPathsEncoded.set(
originalAppPath,
workerResult.encodedPrerenderRoutes
)
}
if (!isDynamicRoute(page)) {
appStaticPaths.set(originalAppPath, [page])
appStaticPathsEncoded.set(originalAppPath, [page])
}
if (workerResult.prerenderFallback) {
// whether or not to allow requests for paths not
// returned from generateStaticParams
appDynamicParamPaths.add(originalAppPath)
}
appDefaultConfigs.set(
originalAppPath,
workerResult.appConfig || {}
)
return
}
if (pageRuntime === SERVER_RUNTIME.edge) { if (pageRuntime === SERVER_RUNTIME.edge) {
if (workerResult.hasStaticProps) { if (workerResult.hasStaticProps) {
console.warn( console.warn(
...@@ -1821,24 +1906,6 @@ export default async function build( ...@@ -1821,24 +1906,6 @@ export default async function build(
'utf8' 'utf8'
) )
if (appDir) {
const appPathsManifest = JSON.parse(
await promises.readFile(
path.join(distDir, serverDir, APP_PATHS_MANIFEST),
'utf8'
)
)
const appPathRoutes: Record<string, string> = {}
Object.keys(appPathsManifest).forEach((entry) => {
appPathRoutes[entry] = normalizeAppPath(entry) || '/'
})
await promises.writeFile(
path.join(distDir, APP_PATH_ROUTES_MANIFEST),
JSON.stringify(appPathRoutes, null, 2)
)
}
const middlewareManifest: MiddlewareManifest = JSON.parse( const middlewareManifest: MiddlewareManifest = JSON.parse(
await promises.readFile( await promises.readFile(
path.join(distDir, serverDir, MIDDLEWARE_MANIFEST), path.join(distDir, serverDir, MIDDLEWARE_MANIFEST),
...@@ -1865,6 +1932,7 @@ export default async function build( ...@@ -1865,6 +1932,7 @@ export default async function build(
} }
const finalPrerenderRoutes: { [route: string]: SsgRoute } = {} const finalPrerenderRoutes: { [route: string]: SsgRoute } = {}
const finalDynamicRoutes: PrerenderManifest['dynamicRoutes'] = {}
const tbdPrerenderRoutes: string[] = [] const tbdPrerenderRoutes: string[] = []
let ssgNotFoundPaths: string[] = [] let ssgNotFoundPaths: string[] = []
...@@ -1889,7 +1957,16 @@ export default async function build( ...@@ -1889,7 +1957,16 @@ export default async function build(
const combinedPages = [...staticPages, ...ssgPages] const combinedPages = [...staticPages, ...ssgPages]
if (combinedPages.length > 0 || useStatic404 || useDefaultStatic500) { // we need to trigger automatic exporting when we have
// - static 404/500
// - getStaticProps paths
// - experimental app is enabled
if (
combinedPages.length > 0 ||
useStatic404 ||
useDefaultStatic500 ||
config.experimental.appDir
) {
const staticGenerationSpan = const staticGenerationSpan =
nextBuildSpan.traceChild('static-generation') nextBuildSpan.traceChild('static-generation')
await staticGenerationSpan.traceAsyncFn(async () => { await staticGenerationSpan.traceAsyncFn(async () => {
...@@ -1986,6 +2063,20 @@ export default async function build( ...@@ -1986,6 +2063,20 @@ export default async function build(
} }
} }
// TODO: output manifest specific to app paths and their
// revalidate periods and dynamicParams settings
appStaticPaths.forEach((routes, originalAppPath) => {
const encodedRoutes = appStaticPathsEncoded.get(originalAppPath)
routes.forEach((route, routeIdx) => {
defaultMap[route] = {
page: originalAppPath,
query: { __nextSsgPath: encodedRoutes?.[routeIdx] },
_isAppDir: true,
}
})
})
if (i18n) { if (i18n) {
for (const page of [ for (const page of [
...staticPages, ...staticPages,
...@@ -2035,6 +2126,58 @@ export default async function build( ...@@ -2035,6 +2126,58 @@ export default async function build(
await promises.unlink(serverBundle) await promises.unlink(serverBundle)
} }
for (const [originalAppPath, routes] of appStaticPaths) {
const page = appNormalizedPaths.get(originalAppPath) || ''
const appConfig = appDefaultConfigs.get(originalAppPath) || {}
let hasDynamicData = appConfig.revalidate === 0
routes.forEach((route) => {
let revalidate = exportConfig.initialPageRevalidationMap[route]
if (typeof revalidate === 'undefined') {
revalidate =
typeof appConfig.revalidate !== 'undefined'
? appConfig.revalidate
: false
}
if (revalidate !== 0) {
const normalizedRoute = normalizePagePath(route)
const dataRoute = path.posix.join(`${normalizedRoute}.rsc`)
finalPrerenderRoutes[route] = {
initialRevalidateSeconds: revalidate,
srcRoute: page,
dataRoute,
}
} else {
hasDynamicData = true
}
})
if (!hasDynamicData && isDynamicRoute(originalAppPath)) {
const normalizedRoute = normalizePagePath(page)
const dataRoute = path.posix.join(`${normalizedRoute}.rsc`)
// TODO: create a separate manifest to allow enforcing
// dynamicParams for non-static paths?
finalDynamicRoutes[page] = {
routeRegex: normalizeRouteRegex(
getNamedRouteRegex(page).re.source
),
dataRoute,
// if dynamicParams are enabled treat as fallback:
// 'blocking' if not it's fallback: false
fallback: appDynamicParamPaths.has(originalAppPath)
? null
: false,
dataRouteRegex: normalizeRouteRegex(
getNamedRouteRegex(
dataRoute.replace(/\.rsc$/, '')
).re.source.replace(/\(\?:\\\/\)\?\$$/, '\\.rsc$')
),
}
}
}
const moveExportedPage = async ( const moveExportedPage = async (
originPage: string, originPage: string,
page: string, page: string,
...@@ -2347,8 +2490,7 @@ export default async function build( ...@@ -2347,8 +2490,7 @@ export default async function build(
telemetry.record(eventPackageUsedInGetServerSideProps(telemetryPlugin)) telemetry.record(eventPackageUsedInGetServerSideProps(telemetryPlugin))
} }
if (ssgPages.size > 0) { if (ssgPages.size > 0 || appDir) {
const finalDynamicRoutes: PrerenderManifest['dynamicRoutes'] = {}
tbdPrerenderRoutes.forEach((tbdRoute) => { tbdPrerenderRoutes.forEach((tbdRoute) => {
const normalizedRoute = normalizePagePath(tbdRoute) const normalizedRoute = normalizePagePath(tbdRoute)
const dataRoute = path.posix.join( const dataRoute = path.posix.join(
......
...@@ -813,13 +813,21 @@ export async function getJsPageSizeInKb( ...@@ -813,13 +813,21 @@ export async function getJsPageSizeInKb(
return [-1, -1] return [-1, -1]
} }
export async function buildStaticPaths( export async function buildStaticPaths({
page: string, page,
getStaticPaths: GetStaticPaths, getStaticPaths,
configFileName: string, staticPathsResult,
locales?: string[], configFileName,
locales,
defaultLocale,
}: {
page: string
getStaticPaths?: GetStaticPaths
staticPathsResult?: UnwrapPromise<ReturnType<GetStaticPaths>>
configFileName: string
locales?: string[]
defaultLocale?: string defaultLocale?: string
): Promise< }): Promise<
Omit<UnwrapPromise<ReturnType<GetStaticPaths>>, 'paths'> & { Omit<UnwrapPromise<ReturnType<GetStaticPaths>>, 'paths'> & {
paths: string[] paths: string[]
encodedPaths: string[] encodedPaths: string[]
...@@ -833,7 +841,15 @@ export async function buildStaticPaths( ...@@ -833,7 +841,15 @@ export async function buildStaticPaths(
// Get the default list of allowed params. // Get the default list of allowed params.
const _validParamKeys = Object.keys(_routeMatcher(page)) const _validParamKeys = Object.keys(_routeMatcher(page))
const staticPathsResult = await getStaticPaths({ locales, defaultLocale }) if (!staticPathsResult) {
if (getStaticPaths) {
staticPathsResult = await getStaticPaths({ locales, defaultLocale })
} else {
throw new Error(
`invariant: attempted to buildStaticPaths without "staticPathsResult" or "getStaticPaths" ${page}`
)
}
}
const expectedReturnVal = const expectedReturnVal =
`Expected: { paths: [], fallback: boolean }\n` + `Expected: { paths: [], fallback: boolean }\n` +
...@@ -1013,6 +1029,137 @@ export async function buildStaticPaths( ...@@ -1013,6 +1029,137 @@ export async function buildStaticPaths(
} }
} }
export type AppConfig = {
revalidate?: number | false
dynamicParams?: true | false
dynamic?: 'auto' | 'error' | 'force-static'
fetchCache?: 'force-cache' | 'only-cache'
preferredRegion?: string
}
type GenerateParams = Array<{
config: AppConfig
segmentPath: string
getStaticPaths?: GetStaticPaths
generateStaticParams?: any
isLayout?: boolean
}>
export const collectGenerateParams = (
segment: any,
parentSegments: string[] = [],
generateParams: GenerateParams = []
): GenerateParams => {
if (!Array.isArray(segment)) return generateParams
const isLayout = !!segment[2]?.layout
const mod = isLayout ? segment[2]?.layout?.() : segment[2]?.page?.()
const result = {
isLayout,
segmentPath: `/${parentSegments.join('/')}${
segment[0] && parentSegments.length > 0 ? '/' : ''
}${segment[0]}`,
config: mod?.config,
getStaticPaths: mod?.getStaticPaths,
generateStaticParams: mod?.generateStaticParams,
}
if (segment[0]) {
parentSegments.push(segment[0])
}
if (result.config || result.generateStaticParams || result.getStaticPaths) {
generateParams.push(result)
}
return collectGenerateParams(
segment[1]?.children,
parentSegments,
generateParams
)
}
export async function buildAppStaticPaths({
page,
configFileName,
generateParams,
}: {
page: string
configFileName: string
generateParams: GenerateParams
}) {
const pageEntry = generateParams[generateParams.length - 1]
// if the page has legacy getStaticPaths we call it like normal
if (typeof pageEntry?.getStaticPaths === 'function') {
return buildStaticPaths({
page,
configFileName,
getStaticPaths: pageEntry.getStaticPaths,
})
} else {
// if generateStaticParams is being used we iterate over them
// collecting them from each level
type Params = Array<Record<string, string | string[]>>
let hadGenerateParams = false
const buildParams = async (
paramsItems: Params = [{}],
idx = 0
): Promise<Params> => {
const curGenerate = generateParams[idx]
if (idx === generateParams.length) {
return paramsItems
}
if (
typeof curGenerate.generateStaticParams !== 'function' &&
idx < generateParams.length
) {
return buildParams(paramsItems, idx + 1)
}
hadGenerateParams = true
const newParams = []
for (const params of paramsItems) {
const result = await curGenerate.generateStaticParams({ params })
// TODO: validate the result is valid here or wait for
// buildStaticPaths to validate?
for (const item of result.params) {
newParams.push({ ...params, ...item })
}
}
if (idx < generateParams.length) {
return buildParams(newParams, idx + 1)
}
return newParams
}
const builtParams = await buildParams()
const fallback = !generateParams.some(
// TODO: check complementary configs that can impact
// dynamicParams behavior
(generate) => generate.config?.dynamicParams === false
)
if (!hadGenerateParams) {
return {
paths: undefined,
fallback: undefined,
encodedPaths: undefined,
}
}
return buildStaticPaths({
staticPathsResult: {
fallback,
paths: builtParams.map((params) => ({ params })),
},
page,
configFileName,
})
}
}
export async function isPageStatic({ export async function isPageStatic({
page, page,
distDir, distDir,
...@@ -1025,6 +1172,9 @@ export async function isPageStatic({ ...@@ -1025,6 +1172,9 @@ export async function isPageStatic({
parentId, parentId,
pageRuntime, pageRuntime,
edgeInfo, edgeInfo,
pageType,
hasServerComponents,
originalAppPath,
}: { }: {
page: string page: string
distDir: string distDir: string
...@@ -1036,7 +1186,10 @@ export async function isPageStatic({ ...@@ -1036,7 +1186,10 @@ export async function isPageStatic({
defaultLocale?: string defaultLocale?: string
parentId?: any parentId?: any
edgeInfo?: any edgeInfo?: any
pageType?: 'pages' | 'app'
pageRuntime: ServerRuntime pageRuntime: ServerRuntime
hasServerComponents?: boolean
originalAppPath?: string
}): Promise<{ }): Promise<{
isStatic?: boolean isStatic?: boolean
isAmpOnly?: boolean isAmpOnly?: boolean
...@@ -1049,6 +1202,7 @@ export async function isPageStatic({ ...@@ -1049,6 +1202,7 @@ export async function isPageStatic({
isNextImageImported?: boolean isNextImageImported?: boolean
traceIncludes?: string[] traceIncludes?: string[]
traceExcludes?: string[] traceExcludes?: string[]
appConfig?: AppConfig
}> { }> {
const isPageStaticSpan = trace('is-page-static-utils', parentId) const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan return isPageStaticSpan
...@@ -1057,6 +1211,10 @@ export async function isPageStatic({ ...@@ -1057,6 +1211,10 @@ export async function isPageStatic({
setHttpAgentOptions(httpAgentOptions) setHttpAgentOptions(httpAgentOptions)
let componentsResult: LoadComponentsReturnType let componentsResult: LoadComponentsReturnType
let prerenderRoutes: Array<string> | undefined
let encodedPrerenderRoutes: Array<string> | undefined
let prerenderFallback: boolean | 'blocking' | undefined
let appConfig: AppConfig = {}
if (pageRuntime === SERVER_RUNTIME.edge) { if (pageRuntime === SERVER_RUNTIME.edge) {
const runtime = await getRuntimeContext({ const runtime = await getRuntimeContext({
...@@ -1084,16 +1242,74 @@ export async function isPageStatic({ ...@@ -1084,16 +1242,74 @@ export async function isPageStatic({
} else { } else {
componentsResult = await loadComponents({ componentsResult = await loadComponents({
distDir, distDir,
pathname: page, pathname: originalAppPath || page,
serverless, serverless,
hasServerComponents: false, hasServerComponents: !!hasServerComponents,
isAppPath: false, isAppPath: pageType === 'app',
}) })
} }
const Comp = componentsResult.Component const Comp = componentsResult.Component || {}
let staticPathsResult:
| UnwrapPromise<ReturnType<GetStaticPaths>>
| undefined
if (pageType === 'app') {
const tree = componentsResult.ComponentMod.tree
const generateParams = collectGenerateParams(tree)
appConfig = generateParams.reduce(
(builtConfig: AppConfig, curGenParams): AppConfig => {
const {
dynamic,
fetchCache,
preferredRegion,
revalidate: curRevalidate,
} = curGenParams?.config || {}
// TODO: should conflicting configs here throw an error
// e.g. if layout defines one region but page defines another
if (typeof builtConfig.preferredRegion === 'undefined') {
builtConfig.preferredRegion = preferredRegion
}
if (typeof builtConfig.dynamic === 'undefined') {
builtConfig.dynamic = dynamic
}
if (typeof builtConfig.fetchCache === 'undefined') {
builtConfig.fetchCache = fetchCache
}
// any revalidate number overrides false
// shorter revalidate overrides longer (initially)
if (typeof builtConfig.revalidate === 'undefined') {
builtConfig.revalidate = curRevalidate
}
if (
typeof curRevalidate === 'number' &&
(typeof builtConfig.revalidate !== 'number' ||
curRevalidate < builtConfig.revalidate)
) {
builtConfig.revalidate = curRevalidate
}
return builtConfig
},
{}
)
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') { if (isDynamicRoute(page)) {
throw new Error('INVALID_DEFAULT_EXPORT') ;({
paths: prerenderRoutes,
fallback: prerenderFallback,
encodedPaths: encodedPrerenderRoutes,
} = await buildAppStaticPaths({
page,
configFileName,
generateParams,
}))
}
} else {
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
throw new Error('INVALID_DEFAULT_EXPORT')
}
} }
const hasGetInitialProps = !!(Comp as any).getInitialProps const hasGetInitialProps = !!(Comp as any).getInitialProps
...@@ -1163,21 +1379,19 @@ export async function isPageStatic({ ...@@ -1163,21 +1379,19 @@ export async function isPageStatic({
) )
} }
let prerenderRoutes: Array<string> | undefined if ((hasStaticProps && hasStaticPaths) || staticPathsResult) {
let encodedPrerenderRoutes: Array<string> | undefined
let prerenderFallback: boolean | 'blocking' | undefined
if (hasStaticProps && hasStaticPaths) {
;({ ;({
paths: prerenderRoutes, paths: prerenderRoutes,
fallback: prerenderFallback, fallback: prerenderFallback,
encodedPaths: encodedPrerenderRoutes, encodedPaths: encodedPrerenderRoutes,
} = await buildStaticPaths( } = await buildStaticPaths({
page, page,
componentsResult.getStaticPaths!,
configFileName,
locales, locales,
defaultLocale defaultLocale,
)) configFileName,
staticPathsResult,
getStaticPaths: componentsResult.getStaticPaths!,
}))
} }
const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED
...@@ -1194,6 +1408,7 @@ export async function isPageStatic({ ...@@ -1194,6 +1408,7 @@ export async function isPageStatic({
isNextImageImported, isNextImageImported,
traceIncludes: config.unstable_includeFiles || [], traceIncludes: config.unstable_includeFiles || [],
traceExcludes: config.unstable_excludeFiles || [], traceExcludes: config.unstable_excludeFiles || [],
appConfig,
} }
}) })
.catch((err) => { .catch((err) => {
......
...@@ -81,6 +81,7 @@ export function getRender({ ...@@ -81,6 +81,7 @@ export function getRender({
getServerSideProps: pageMod.getServerSideProps, getServerSideProps: pageMod.getServerSideProps,
getStaticPaths: pageMod.getStaticPaths, getStaticPaths: pageMod.getStaticPaths,
ComponentMod: pageMod, ComponentMod: pageMod,
pathname,
} }
} }
...@@ -94,6 +95,7 @@ export function getRender({ ...@@ -94,6 +95,7 @@ export function getRender({
getServerSideProps: error500Mod.getServerSideProps, getServerSideProps: error500Mod.getServerSideProps,
getStaticPaths: error500Mod.getStaticPaths, getStaticPaths: error500Mod.getStaticPaths,
ComponentMod: error500Mod, ComponentMod: error500Mod,
pathname,
} }
} }
...@@ -106,6 +108,7 @@ export function getRender({ ...@@ -106,6 +108,7 @@ export function getRender({
getServerSideProps: errorMod.getServerSideProps, getServerSideProps: errorMod.getServerSideProps,
getStaticPaths: errorMod.getStaticPaths, getStaticPaths: errorMod.getStaticPaths,
ComponentMod: errorMod, ComponentMod: errorMod,
pathname,
} }
} }
......
export * from './utils' export * from './utils'
export { default as exportPage } from '../export/worker' import exportPage from '../export/worker'
export { exportPage }
// @ts-expect-error createServerContext exists on experimental channel // @ts-expect-error createServerContext exists on experimental channel
import { createServerContext } from 'react' import { createServerContext } from 'react'
export class DynamicServerError extends Error {
constructor(type: string) {
super(`Dynamic server usage: ${type}`)
}
}
// Ensure serverContext is not created more than once as React will throw when creating it more than once // Ensure serverContext is not created more than once as React will throw when creating it more than once
// https://github.com/facebook/react/blob/dd2d6522754f52c70d02c51db25eb7cbd5d1c8eb/packages/react/src/ReactServerContext.js#L101 // https://github.com/facebook/react/blob/dd2d6522754f52c70d02c51db25eb7cbd5d1c8eb/packages/react/src/ReactServerContext.js#L101
const createContext = (name: string) => { const createContext = <T>(name: string, defaultValue: T | null = null) => {
// @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global // @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global
if (!global.__NEXT_DEV_SERVER_CONTEXT__) { if (!global.__NEXT_DEV_SERVER_CONTEXT__) {
// @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global // @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global
...@@ -13,13 +19,30 @@ const createContext = (name: string) => { ...@@ -13,13 +19,30 @@ const createContext = (name: string) => {
// @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global // @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global
if (!global.__NEXT_DEV_SERVER_CONTEXT__[name]) { if (!global.__NEXT_DEV_SERVER_CONTEXT__[name]) {
// @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global // @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global
global.__NEXT_DEV_SERVER_CONTEXT__[name] = createServerContext(name, null) global.__NEXT_DEV_SERVER_CONTEXT__[name] = createServerContext(
name,
defaultValue
)
} }
// @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global // @ts-expect-error __NEXT_DEV_SERVER_CONTEXT__ is a global
return global.__NEXT_DEV_SERVER_CONTEXT__[name] return global.__NEXT_DEV_SERVER_CONTEXT__[name]
} }
export const HeadersContext = createContext('HeadersContext') export const CONTEXT_NAMES = {
export const PreviewDataContext = createContext('PreviewDataContext') HeadersContext: 'HeadersContext',
export const CookiesContext = createContext('CookiesContext') PreviewDataContext: 'PreviewDataContext',
CookiesContext: 'CookiesContext',
StaticGenerationContext: 'StaticGenerationContext',
FetchRevalidateContext: 'FetchRevalidateContext',
} as const
export const HeadersContext = createContext(CONTEXT_NAMES.HeadersContext)
export const PreviewDataContext = createContext(
CONTEXT_NAMES.PreviewDataContext
)
export const CookiesContext = createContext(CONTEXT_NAMES.CookiesContext)
export const StaticGenerationContext = createContext(
CONTEXT_NAMES.StaticGenerationContext,
{ isStaticGeneration: false }
)
...@@ -3,16 +3,37 @@ import { ...@@ -3,16 +3,37 @@ import {
HeadersContext, HeadersContext,
PreviewDataContext, PreviewDataContext,
CookiesContext, CookiesContext,
DynamicServerError,
StaticGenerationContext,
} from './hooks-server-context' } from './hooks-server-context'
export function useTrackStaticGeneration() {
return useContext<
typeof import('./hooks-server-context').StaticGenerationContext
>(StaticGenerationContext)
}
function useStaticGenerationBailout(reason: string) {
const staticGenerationContext = useTrackStaticGeneration()
if (staticGenerationContext.isStaticGeneration) {
// TODO: honor the dynamic: 'force-static'
staticGenerationContext.revalidate = 0
throw new DynamicServerError(reason)
}
}
export function useHeaders() { export function useHeaders() {
useStaticGenerationBailout('useHeaders')
return useContext(HeadersContext) return useContext(HeadersContext)
} }
export function usePreviewData() { export function usePreviewData() {
useStaticGenerationBailout('usePreviewData')
return useContext(PreviewDataContext) return useContext(PreviewDataContext)
} }
export function useCookies() { export function useCookies() {
useStaticGenerationBailout('useCookies')
return useContext(CookiesContext) return useContext(CookiesContext)
} }
...@@ -22,6 +22,8 @@ import { ...@@ -22,6 +22,8 @@ import {
CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_PATH,
EXPORT_DETAIL, EXPORT_DETAIL,
EXPORT_MARKER, EXPORT_MARKER,
FLIGHT_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_EXPORT, PHASE_EXPORT,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
...@@ -386,6 +388,7 @@ export default async function exportApp( ...@@ -386,6 +388,7 @@ export default async function exportApp(
nextScriptWorkers: nextConfig.experimental.nextScriptWorkers, nextScriptWorkers: nextConfig.experimental.nextScriptWorkers,
optimizeFonts: nextConfig.optimizeFonts as FontConfig, optimizeFonts: nextConfig.optimizeFonts as FontConfig,
largePageDataBytes: nextConfig.experimental.largePageDataBytes, largePageDataBytes: nextConfig.experimental.largePageDataBytes,
serverComponents: nextConfig.experimental.serverComponents,
} }
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
...@@ -404,15 +407,31 @@ export default async function exportApp( ...@@ -404,15 +407,31 @@ export default async function exportApp(
} }
const exportPathMap = await nextExportSpan const exportPathMap = await nextExportSpan
.traceChild('run-export-path-map') .traceChild('run-export-path-map')
.traceAsyncFn(() => .traceAsyncFn(async () => {
nextConfig.exportPathMap(defaultPathMap, { const exportMap = await nextConfig.exportPathMap(defaultPathMap, {
dev: false, dev: false,
dir, dir,
outDir, outDir,
distDir, distDir,
buildId, buildId,
}) })
) return exportMap
})
if (options.buildExport && nextConfig.experimental.appDir) {
// @ts-expect-error untyped
renderOpts.serverComponentManifest = require(join(
distDir,
SERVER_DIRECTORY,
`${FLIGHT_MANIFEST}.json`
)) as PagesManifest
// @ts-expect-error untyped
renderOpts.serverCSSManifest = require(join(
distDir,
SERVER_DIRECTORY,
FLIGHT_SERVER_CSS_MANIFEST + '.json'
)) as PagesManifest
}
// only add missing 404 page when `buildExport` is false // only add missing 404 page when `buildExport` is false
if (!options.buildExport) { if (!options.buildExport) {
......
...@@ -27,6 +27,7 @@ import { setHttpAgentOptions } from '../server/config' ...@@ -27,6 +27,7 @@ import { setHttpAgentOptions } from '../server/config'
import RenderResult from '../server/render-result' import RenderResult from '../server/render-result'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { addRequestMeta } from '../server/request-meta' import { addRequestMeta } from '../server/request-meta'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
loadRequireHook() loadRequireHook()
const envConfig = require('../shared/lib/runtime-config') const envConfig = require('../shared/lib/runtime-config')
...@@ -104,7 +105,6 @@ export default async function exportPage({ ...@@ -104,7 +105,6 @@ export default async function exportPage({
pathMap, pathMap,
distDir, distDir,
outDir, outDir,
appPaths,
pagesDataDir, pagesDataDir,
renderOpts, renderOpts,
buildExport, buildExport,
...@@ -129,6 +129,7 @@ export default async function exportPage({ ...@@ -129,6 +129,7 @@ export default async function exportPage({
try { try {
const { query: originalQuery = {} } = pathMap const { query: originalQuery = {} } = pathMap
const { page } = pathMap const { page } = pathMap
const isAppDir = (pathMap as any)._isAppDir
const filePath = normalizePagePath(path) const filePath = normalizePagePath(path)
const isDynamic = isDynamicRoute(page) const isDynamic = isDynamicRoute(page)
const ampPath = `${filePath}.amp` const ampPath = `${filePath}.amp`
...@@ -136,6 +137,9 @@ export default async function exportPage({ ...@@ -136,6 +137,9 @@ export default async function exportPage({
let query = { ...originalQuery } let query = { ...originalQuery }
let params: { [key: string]: string | string[] } | undefined let params: { [key: string]: string | string[] } | undefined
if (isAppDir) {
outDir = join(distDir, 'server/app')
}
let updatedPath = query.__nextSsgPath || path let updatedPath = query.__nextSsgPath || path
let locale = query.__nextLocale || renderOpts.locale let locale = query.__nextLocale || renderOpts.locale
delete query.__nextLocale delete query.__nextLocale
...@@ -172,7 +176,11 @@ export default async function exportPage({ ...@@ -172,7 +176,11 @@ export default async function exportPage({
).pathname ).pathname
if (isDynamic && page !== nonLocalizedPath) { if (isDynamic && page !== nonLocalizedPath) {
params = getRouteMatcher(getRouteRegex(page))(updatedPath) || undefined const normalizedPage = isAppDir ? normalizeAppPath(page) : page
params =
getRouteMatcher(getRouteRegex(normalizedPage))(updatedPath) ||
undefined
if (params) { if (params) {
// we have to pass these separately for serverless // we have to pass these separately for serverless
if (!serverless) { if (!serverless) {
...@@ -272,9 +280,6 @@ export default async function exportPage({ ...@@ -272,9 +280,6 @@ export default async function exportPage({
return !buildExport && getStaticProps && !isDynamicRoute(path) return !buildExport && getStaticProps && !isDynamicRoute(path)
} }
const isAppPath = appPaths.some((appPath: string) =>
appPath.startsWith(page + '.page')
)
if (serverless) { if (serverless) {
const curUrl = url.parse(req.url!, true) const curUrl = url.parse(req.url!, true)
req.url = url.format({ req.url = url.format({
...@@ -295,7 +300,7 @@ export default async function exportPage({ ...@@ -295,7 +300,7 @@ export default async function exportPage({
pathname: page, pathname: page,
serverless, serverless,
hasServerComponents: !!serverComponents, hasServerComponents: !!serverComponents,
isAppPath, isAppPath: isAppDir,
}) })
const ampState = { const ampState = {
ampFirst: pageConfig?.amp === true, ampFirst: pageConfig?.amp === true,
...@@ -362,8 +367,66 @@ export default async function exportPage({ ...@@ -362,8 +367,66 @@ export default async function exportPage({
pathname: page, pathname: page,
serverless, serverless,
hasServerComponents: !!serverComponents, hasServerComponents: !!serverComponents,
isAppPath, isAppPath: isAppDir,
}) })
curRenderOpts = {
...components,
...renderOpts,
ampPath: renderAmpPath,
params,
optimizeFonts,
optimizeCss,
disableOptimizedLoading,
fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless)
: null,
locale: locale as string,
}
// during build we attempt rendering app dir paths
// and bail when dynamic dependencies are detected
// only fully static paths are fully generated here
if (isAppDir) {
const {
DynamicServerError,
} = require('../client/components/hooks-server-context')
const { renderToHTMLOrFlight } =
require('../server/app-render') as typeof import('../server/app-render')
try {
curRenderOpts.params ||= {}
const result = await renderToHTMLOrFlight(
req as any,
res as any,
page,
query,
curRenderOpts as any,
false,
true
)
const html = result?.toUnchunkedString()
const flightData = (curRenderOpts as any).pageData
const revalidate = (curRenderOpts as any).revalidate
results.fromBuildExportRevalidate = revalidate
if (revalidate !== 0) {
await promises.writeFile(htmlFilepath, html, 'utf8')
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
)
}
} catch (err) {
if (!(err instanceof DynamicServerError)) {
throw err
}
}
return { ...results, duration: Date.now() - start }
}
const ampState = { const ampState = {
ampFirst: components.pageConfig?.amp === true, ampFirst: components.pageConfig?.amp === true,
hasQuery: Boolean(query.amp), hasQuery: Boolean(query.amp),
...@@ -407,19 +470,6 @@ export default async function exportPage({ ...@@ -407,19 +470,6 @@ export default async function exportPage({
if (optimizeCss) { if (optimizeCss) {
process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true) process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true)
} }
curRenderOpts = {
...components,
...renderOpts,
ampPath: renderAmpPath,
params,
optimizeFonts,
optimizeCss,
disableOptimizedLoading,
fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless)
: null,
locale: locale as string,
}
renderResult = await renderMethod( renderResult = await renderMethod(
req, req,
res, res,
......
import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type { LoadComponentsReturnType } from './load-components' import type { LoadComponentsReturnType } from './load-components'
import type { ServerRuntime } from '../types' import type { ServerRuntime } from '../types'
...@@ -17,10 +17,8 @@ import { ...@@ -17,10 +17,8 @@ import {
createBufferedTransformStream, createBufferedTransformStream,
continueFromInitialStream, continueFromInitialStream,
} from './node-web-streams-helper' } from './node-web-streams-helper'
import { isDynamicRoute } from '../shared/lib/router/utils'
import { ESCAPE_REGEX, htmlEscapeJsonString } from './htmlescape' import { ESCAPE_REGEX, htmlEscapeJsonString } from './htmlescape'
import { shouldUseReactRoot } from './utils' import { shouldUseReactRoot } from './utils'
import { NextApiRequestCookies } from './api-utils'
import { matchSegment } from '../client/components/match-segments' import { matchSegment } from '../client/components/match-segments'
import { import {
FlightCSSManifest, FlightCSSManifest,
...@@ -65,76 +63,66 @@ function interopDefault(mod: any) { ...@@ -65,76 +63,66 @@ function interopDefault(mod: any) {
return mod.default || mod return mod.default || mod
} }
// Shadowing check does not work with TypeScript enums // tolerate dynamic server errors during prerendering so console
// eslint-disable-next-line no-shadow // isn't spammed with unactionable errors
const enum RecordStatus { function onError(err: any) {
Pending, const { DynamicServerError } =
Resolved, require('../client/components/hooks-server-context') as typeof import('../client/components/hooks-server-context')
Rejected,
}
type Record = {
status: RecordStatus
// Could hold the existing promise or the resolved Promise
value: any
}
/** if (!(err instanceof DynamicServerError)) {
* Create data fetching record for Promise. console.error(err)
*/
function createRecordFromThenable(thenable: Promise<any>) {
const record: Record = {
status: RecordStatus.Pending,
value: thenable,
} }
thenable.then(
function (value) {
if (record.status === RecordStatus.Pending) {
const resolvedRecord = record
resolvedRecord.status = RecordStatus.Resolved
resolvedRecord.value = value
}
},
function (err) {
if (record.status === RecordStatus.Pending) {
const rejectedRecord = record
rejectedRecord.status = RecordStatus.Rejected
rejectedRecord.value = err
}
}
)
return record
} }
/** let isFetchPatched = false
* Read record value or throw Promise if it's not resolved yet.
*/
function readRecordValue(record: Record) {
if (record.status === RecordStatus.Resolved) {
return record.value
} else {
throw record.value
}
}
/** // we patch fetch to collect cache information used for
* Preload data fetching record before it is called during React rendering. // determining if a page is static or not
* If the record is already in the cache returns that record. function patchFetch() {
*/ if (isFetchPatched) return
function preloadDataFetchingRecord( isFetchPatched = true
map: Map<string, Record>,
key: string, const { DynamicServerError } =
fetcher: () => Promise<any> | any require('../client/components/hooks-server-context') as typeof import('../client/components/hooks-server-context')
) {
let record = map.get(key) const { useTrackStaticGeneration } =
require('../client/components/hooks-server') as typeof import('../client/components/hooks-server')
if (!record) {
const thenable = fetcher() const origFetch = (global as any).fetch
record = createRecordFromThenable(thenable)
map.set(key, record) ;(global as any).fetch = async (init: any, opts: any) => {
} let staticGenerationContext: ReturnType<typeof useTrackStaticGeneration> =
{}
try {
// eslint-disable-next-line react-hooks/rules-of-hooks
staticGenerationContext = useTrackStaticGeneration() || {}
} catch (_) {}
const { isStaticGeneration, fetchRevalidate, pathname } =
staticGenerationContext
if (isStaticGeneration) {
if (opts && typeof opts === 'object') {
if (opts.cache === 'no-store') {
staticGenerationContext.revalidate = 0
// TODO: ensure this error isn't logged to the user
// seems it's slipping through currently
throw new DynamicServerError(
`no-store fetch ${init}${pathname ? ` ${pathname}` : ''}`
)
}
return record if (
typeof opts.revalidate === 'number' &&
(typeof fetchRevalidate === 'undefined' ||
opts.revalidate < fetchRevalidate)
) {
staticGenerationContext.fetchRevalidate = opts.revalidate
}
}
}
return origFetch(init, opts)
}
} }
interface FlightResponseRef { interface FlightResponseRef {
...@@ -149,6 +137,7 @@ function useFlightResponse( ...@@ -149,6 +137,7 @@ function useFlightResponse(
writable: WritableStream<Uint8Array>, writable: WritableStream<Uint8Array>,
req: ReadableStream<Uint8Array>, req: ReadableStream<Uint8Array>,
serverComponentManifest: any, serverComponentManifest: any,
rscChunks: Uint8Array[],
flightResponseRef: FlightResponseRef, flightResponseRef: FlightResponseRef,
nonce?: string nonce?: string
): Promise<JSX.Element> { ): Promise<JSX.Element> {
...@@ -172,6 +161,10 @@ function useFlightResponse( ...@@ -172,6 +161,10 @@ function useFlightResponse(
function process() { function process() {
forwardReader.read().then(({ done, value }) => { forwardReader.read().then(({ done, value }) => {
if (value) {
rscChunks.push(value)
}
if (!bootstrapped) { if (!bootstrapped) {
bootstrapped = true bootstrapped = true
writer.write( writer.write(
...@@ -217,12 +210,14 @@ function createServerComponentRenderer( ...@@ -217,12 +210,14 @@ function createServerComponentRenderer(
transformStream, transformStream,
serverComponentManifest, serverComponentManifest,
serverContexts, serverContexts,
rscChunks,
}: { }: {
transformStream: TransformStream<Uint8Array, Uint8Array> transformStream: TransformStream<Uint8Array, Uint8Array>
serverComponentManifest: NonNullable<RenderOpts['serverComponentManifest']> serverComponentManifest: NonNullable<RenderOpts['serverComponentManifest']>
serverContexts: Array< serverContexts: Array<
[ServerContextName: string, JSONValue: Object | number | string] [ServerContextName: string, JSONValue: Object | number | string]
> >
rscChunks: Uint8Array[]
}, },
nonce?: string nonce?: string
): () => JSX.Element { ): () => JSX.Element {
...@@ -246,6 +241,7 @@ function createServerComponentRenderer( ...@@ -246,6 +241,7 @@ function createServerComponentRenderer(
serverComponentManifest, serverComponentManifest,
{ {
context: serverContexts, context: serverContexts,
onError,
} }
) )
} }
...@@ -261,6 +257,7 @@ function createServerComponentRenderer( ...@@ -261,6 +257,7 @@ function createServerComponentRenderer(
writable, writable,
reqStream, reqStream,
serverComponentManifest, serverComponentManifest,
rscChunks,
flightResponseRef, flightResponseRef,
nonce nonce
) )
...@@ -479,8 +476,14 @@ export async function renderToHTMLOrFlight( ...@@ -479,8 +476,14 @@ export async function renderToHTMLOrFlight(
pathname: string, pathname: string,
query: NextParsedUrlQuery, query: NextParsedUrlQuery,
renderOpts: RenderOpts, renderOpts: RenderOpts,
isPagesDir: boolean isPagesDir: boolean,
isStaticGeneration: boolean = false
): Promise<RenderResult | null> { ): Promise<RenderResult | null> {
patchFetch()
const { CONTEXT_NAMES } =
require('../client/components/hooks-server-context') as typeof import('../client/components/hooks-server-context')
// @ts-expect-error createServerContext exists in react@experimental + react-dom@experimental // @ts-expect-error createServerContext exists in react@experimental + react-dom@experimental
if (typeof React.createServerContext === 'undefined') { if (typeof React.createServerContext === 'undefined') {
throw new Error( throw new Error(
...@@ -511,9 +514,9 @@ export async function renderToHTMLOrFlight( ...@@ -511,9 +514,9 @@ export async function renderToHTMLOrFlight(
// Empty so that the client-side router will do a full page navigation. // Empty so that the client-side router will do a full page navigation.
const flightData: FlightData = pathname + (search ? `?${search}` : '') const flightData: FlightData = pathname + (search ? `?${search}` : '')
return new FlightRenderResult( return new FlightRenderResult(
renderToReadableStream(flightData, serverComponentManifest).pipeThrough( renderToReadableStream(flightData, serverComponentManifest, {
createBufferedTransformStream() onError,
) }).pipeThrough(createBufferedTransformStream())
) )
} }
...@@ -531,7 +534,6 @@ export async function renderToHTMLOrFlight( ...@@ -531,7 +534,6 @@ export async function renderToHTMLOrFlight(
stripInternalQueries(query) stripInternalQueries(query)
const pageIsDynamic = isDynamicRoute(pathname)
const LayoutRouter = const LayoutRouter =
ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default
const RenderFromTemplateContext = const RenderFromTemplateContext =
...@@ -563,24 +565,25 @@ export async function renderToHTMLOrFlight( ...@@ -563,24 +565,25 @@ export async function renderToHTMLOrFlight(
res, res,
(renderOpts as any).previewProps (renderOpts as any).previewProps
) )
const isPreview = previewData !== false
/** /**
* Server Context is specifically only available in Server Components. * Server Context is specifically only available in Server Components.
* It has to hold values that can't change while rendering from the common layout down. * It has to hold values that can't change while rendering from the common layout down.
* An example of this would be that `headers` are available but `searchParams` are not because that'd mean we have to render from the root layout down on all requests. * An example of this would be that `headers` are available but `searchParams` are not because that'd mean we have to render from the root layout down on all requests.
*/ */
const staticGenerationContext: {
revalidate?: undefined | number
isStaticGeneration: boolean
pathname: string
} = { isStaticGeneration, pathname }
const serverContexts: Array<[string, any]> = [ const serverContexts: Array<[string, any]> = [
['WORKAROUND', null], // TODO-APP: First value has a bug currently where the value is not set on the second request: https://github.com/facebook/react/issues/24849 ['WORKAROUND', null], // TODO-APP: First value has a bug currently where the value is not set on the second request: https://github.com/facebook/react/issues/24849
['HeadersContext', headers], [CONTEXT_NAMES.HeadersContext, headers],
['CookiesContext', cookies], [CONTEXT_NAMES.CookiesContext, cookies],
['PreviewDataContext', previewData], [CONTEXT_NAMES.PreviewDataContext, previewData],
[CONTEXT_NAMES.StaticGenerationContext, staticGenerationContext],
] ]
/**
* Used to keep track of in-flight / resolved data fetching Promises.
*/
const dataCache = new Map<string, Record>()
type CreateSegmentPath = (child: FlightSegmentPath) => FlightSegmentPath type CreateSegmentPath = (child: FlightSegmentPath) => FlightSegmentPath
/** /**
...@@ -661,6 +664,8 @@ export async function renderToHTMLOrFlight( ...@@ -661,6 +664,8 @@ export async function renderToHTMLOrFlight(
return segmentTree return segmentTree
} }
let defaultRevalidate: false | undefined | number = false
/** /**
* Use the provided loader tree to create the React Component tree. * Use the provided loader tree to create the React Component tree.
*/ */
...@@ -701,6 +706,10 @@ export async function renderToHTMLOrFlight( ...@@ -701,6 +706,10 @@ export async function renderToHTMLOrFlight(
: isPage : isPage
? await page() ? await page()
: undefined : undefined
if (layoutOrPageMod?.config) {
defaultRevalidate = layoutOrPageMod.config.revalidate
}
/** /**
* Checks if the current segment is a root layout. * Checks if the current segment is a root layout.
*/ */
...@@ -711,11 +720,19 @@ export async function renderToHTMLOrFlight( ...@@ -711,11 +720,19 @@ export async function renderToHTMLOrFlight(
const rootLayoutIncludedAtThisLevelOrAbove = const rootLayoutIncludedAtThisLevelOrAbove =
rootLayoutIncluded || rootLayoutAtThisLevel rootLayoutIncluded || rootLayoutAtThisLevel
/** // TODO-APP: move these errors to the loader instead?
* Check if the current layout/page is a client component // we will also need a migration doc here to link to
*/ if (typeof layoutOrPageMod?.getServerSideProps === 'function') {
const isClientComponentModule = throw new Error(
layoutOrPageMod && !layoutOrPageMod.hasOwnProperty('__next_rsc__') `getServerSideProps is not supported in app/, detected in ${segment}`
)
}
if (typeof layoutOrPageMod?.getStaticProps === 'function') {
throw new Error(
`getStaticProps is not supported in app/, detected in ${segment}`
)
}
/** /**
* The React Component to render. * The React Component to render.
...@@ -727,7 +744,7 @@ export async function renderToHTMLOrFlight( ...@@ -727,7 +744,7 @@ export async function renderToHTMLOrFlight(
// Handle dynamic segment params. // Handle dynamic segment params.
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
/** /**
* Create object holding the parent params and current params, this is passed to getServerSideProps and getStaticProps. * Create object holding the parent params and current params
*/ */
const currentParams = const currentParams =
// Handle null case where dynamic param is optional // Handle null case where dynamic param is optional
...@@ -836,101 +853,9 @@ export async function renderToHTMLOrFlight( ...@@ -836,101 +853,9 @@ export async function renderToHTMLOrFlight(
} }
} }
const segmentPath = createSegmentPath([actualSegment])
const dataCacheKey = JSON.stringify(segmentPath)
let fetcher: (() => Promise<any>) | null = null
type GetServerSidePropsContext = {
headers: IncomingHttpHeaders
cookies: NextApiRequestCookies
layoutSegments: FlightSegmentPath
params?: { [key: string]: string | string[] }
preview?: boolean
previewData?: string | object | undefined
}
type getServerSidePropsContextPage = GetServerSidePropsContext & {
searchParams: URLSearchParams
pathname: string
}
type GetStaticPropsContext = {
layoutSegments: FlightSegmentPath
params?: { [key: string]: string | string[] }
preview?: boolean
previewData?: string | object | undefined
}
type GetStaticPropContextPage = GetStaticPropsContext & {
pathname: string
}
// TODO-APP: pass a shared cache from previous getStaticProps/getServerSideProps calls?
if (!isClientComponentModule && layoutOrPageMod.getServerSideProps) {
// TODO-APP: recommendation for i18n
// locales: (renderOpts as any).locales, // always the same
// locale: (renderOpts as any).locale, // /nl/something -> nl
// defaultLocale: (renderOpts as any).defaultLocale, // changes based on domain
const getServerSidePropsContext:
| GetServerSidePropsContext
| getServerSidePropsContextPage = {
headers,
cookies,
layoutSegments: segmentPath,
// TODO-APP: change pathname to actual pathname, it holds the dynamic parameter currently
...(isPage ? { searchParams: query, pathname } : {}),
...(pageIsDynamic ? { params: currentParams } : undefined),
...(isPreview
? { preview: true, previewData: previewData }
: undefined),
}
fetcher = () =>
Promise.resolve(
layoutOrPageMod.getServerSideProps(getServerSidePropsContext)
)
}
// TODO-APP: implement layout specific caching for getStaticProps
if (!isClientComponentModule && layoutOrPageMod.getStaticProps) {
const getStaticPropsContext:
| GetStaticPropsContext
| GetStaticPropContextPage = {
layoutSegments: segmentPath,
...(isPage ? { pathname } : {}),
...(pageIsDynamic ? { params: currentParams } : undefined),
...(isPreview
? { preview: true, previewData: previewData }
: undefined),
}
fetcher = () =>
Promise.resolve(layoutOrPageMod.getStaticProps(getStaticPropsContext))
}
if (fetcher) {
// Kick off data fetching before rendering, this ensures there is no waterfall for layouts as
// all data fetching required to render the page is kicked off simultaneously
preloadDataFetchingRecord(dataCache, dataCacheKey, fetcher)
}
return { return {
Component: () => { Component: () => {
let props let props = {}
// The data fetching was kicked off before rendering (see above)
// if the data was not resolved yet the layout rendering will be suspended
if (fetcher) {
const record = preloadDataFetchingRecord(
dataCache,
dataCacheKey,
fetcher
)
// Result of calling getStaticProps or getServerSideProps. If promise is not resolve yet it will suspend.
const recordValue = readRecordValue(record)
if (props) {
props = Object.assign({}, props, recordValue.props)
} else {
props = recordValue.props
}
}
return ( return (
<> <>
...@@ -964,6 +889,21 @@ export async function renderToHTMLOrFlight( ...@@ -964,6 +889,21 @@ export async function renderToHTMLOrFlight(
} }
} }
/**
* Rules of Static & Dynamic HTML:
*
* 1.) We must generate static HTML unless the caller explicitly opts
* in to dynamic HTML support.
*
* 2.) If dynamic HTML support is requested, we must honor that request
* or throw an error. It is the sole responsibility of the caller to
* ensure they aren't e.g. requesting dynamic HTML for an AMP page.
*
* These rules help ensure that other existing features like request caching,
* coalescing, and ISR continue working as intended.
*/
const generateStaticHTML = supportsDynamicHTML !== true
// Handle Flight render request. This is only used when client-side navigating. E.g. when you `router.push('/dashboard')` or `router.reload()`. // Handle Flight render request. This is only used when client-side navigating. E.g. when you `router.push('/dashboard')` or `router.reload()`.
if (isFlight) { if (isFlight) {
// TODO-APP: throw on invalid flightRouterState // TODO-APP: throw on invalid flightRouterState
...@@ -981,7 +921,6 @@ export async function renderToHTMLOrFlight( ...@@ -981,7 +921,6 @@ export async function renderToHTMLOrFlight(
const parallelRoutesKeys = Object.keys(parallelRoutes) const parallelRoutesKeys = Object.keys(parallelRoutes)
// Because this function walks to a deeper point in the tree to start rendering we have to track the dynamic parameters up to the point where rendering starts // Because this function walks to a deeper point in the tree to start rendering we have to track the dynamic parameters up to the point where rendering starts
// That way even when rendering the subtree getServerSideProps/getStaticProps get the right parameters.
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
const currentParams = const currentParams =
// Handle null case where dynamic param is optional // Handle null case where dynamic param is optional
...@@ -1064,11 +1003,22 @@ export async function renderToHTMLOrFlight( ...@@ -1064,11 +1003,22 @@ export async function renderToHTMLOrFlight(
).slice(1), ).slice(1),
] ]
return new FlightRenderResult( const readable = renderToReadableStream(
renderToReadableStream(flightData, serverComponentManifest, { flightData,
serverComponentManifest,
{
context: serverContexts, context: serverContexts,
}).pipeThrough(createBufferedTransformStream()) onError,
) }
).pipeThrough(createBufferedTransformStream())
if (generateStaticHTML) {
let staticHtml = Buffer.from(
(await readable.getReader().read()).value || ''
).toString()
return new FlightRenderResult(staticHtml)
}
return new FlightRenderResult(readable)
} }
// Below this line is handling for rendering to HTML. // Below this line is handling for rendering to HTML.
...@@ -1100,6 +1050,13 @@ export async function renderToHTMLOrFlight( ...@@ -1100,6 +1050,13 @@ export async function renderToHTMLOrFlight(
nonce = getScriptNonceFromHeader(csp) nonce = getScriptNonceFromHeader(csp)
} }
const serverComponentsRenderOpts = {
transformStream: serverComponentsInlinedTransformStream,
serverComponentManifest,
serverContexts,
rscChunks: [],
}
/** /**
* A new React Component that renders the provided React Component * A new React Component that renders the provided React Component
* using Flight which can then be rendered to HTML. * using Flight which can then be rendered to HTML.
...@@ -1123,11 +1080,7 @@ export async function renderToHTMLOrFlight( ...@@ -1123,11 +1080,7 @@ export async function renderToHTMLOrFlight(
) )
}, },
ComponentMod, ComponentMod,
{ serverComponentsRenderOpts,
transformStream: serverComponentsInlinedTransformStream,
serverComponentManifest,
serverContexts,
},
nonce nonce
) )
...@@ -1149,20 +1102,6 @@ export async function renderToHTMLOrFlight( ...@@ -1149,20 +1102,6 @@ export async function renderToHTMLOrFlight(
) )
} }
/**
* Rules of Static & Dynamic HTML:
*
* 1.) We must generate static HTML unless the caller explicitly opts
* in to dynamic HTML support.
*
* 2.) If dynamic HTML support is requested, we must honor that request
* or throw an error. It is the sole responsibility of the caller to
* ensure they aren't e.g. requesting dynamic HTML for an AMP page.
*
* These rules help ensure that other existing features like request caching,
* coalescing, and ISR continue working as intended.
*/
const generateStaticHTML = supportsDynamicHTML !== true
const bodyResult = async () => { const bodyResult = async () => {
const content = ( const content = (
<FlushEffects> <FlushEffects>
...@@ -1234,5 +1173,22 @@ export async function renderToHTMLOrFlight( ...@@ -1234,5 +1173,22 @@ export async function renderToHTMLOrFlight(
} }
} }
return new RenderResult(await bodyResult()) const readable = await bodyResult()
if (generateStaticHTML) {
let staticHtml = Buffer.from(
(await readable.getReader().read()).value || ''
).toString()
;(renderOpts as any).pageData = Buffer.concat(
serverComponentsRenderOpts.rscChunks
).toString()
;(renderOpts as any).revalidate =
typeof staticGenerationContext.revalidate === 'undefined'
? defaultRevalidate
: staticGenerationContext.revalidate
return new RenderResult(staticHtml)
}
return new RenderResult(readable)
} }
...@@ -913,9 +913,14 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -913,9 +913,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}) })
} }
protected async getStaticPaths(pathname: string): Promise<{ protected async getStaticPaths({
staticPaths: string[] | undefined pathname,
fallbackMode: 'static' | 'blocking' | false }: {
pathname: string
originalAppPath?: string
}): Promise<{
staticPaths?: string[]
fallbackMode?: 'static' | 'blocking' | false
}> { }> {
// `staticPaths` is intentionally set to `undefined` as it should've // `staticPaths` is intentionally set to `undefined` as it should've
// been caught when checking disk data. // been caught when checking disk data.
...@@ -923,7 +928,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -923,7 +928,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// Read whether or not fallback should exist from the manifest. // Read whether or not fallback should exist from the manifest.
const fallbackField = const fallbackField =
this.getPrerenderManifest().dynamicRoutes[pathname].fallback this.getPrerenderManifest().dynamicRoutes[pathname]?.fallback
return { return {
staticPaths, staticPaths,
...@@ -932,7 +937,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -932,7 +937,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
? 'static' ? 'static'
: fallbackField === null : fallbackField === null
? 'blocking' ? 'blocking'
: false, : fallbackField,
} }
} }
...@@ -942,15 +947,17 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -942,15 +947,17 @@ export default abstract class Server<ServerOptions extends Options = Options> {
): Promise<ResponsePayload | null> { ): Promise<ResponsePayload | null> {
const is404Page = pathname === '/404' const is404Page = pathname === '/404'
const is500Page = pathname === '/500' const is500Page = pathname === '/500'
const isAppPath = components.isAppPath
const isLikeServerless = const isLikeServerless =
typeof components.ComponentMod === 'object' && typeof components.ComponentMod === 'object' &&
typeof (components.ComponentMod as any).renderReqToHTML === 'function' typeof (components.ComponentMod as any).renderReqToHTML === 'function'
const hasServerProps = !!components.getServerSideProps const hasServerProps = !!components.getServerSideProps
const hasStaticPaths = !!components.getStaticPaths let hasStaticPaths = !!components.getStaticPaths
const hasGetInitialProps = !!components.Component?.getInitialProps const hasGetInitialProps = !!components.Component?.getInitialProps
const isServerComponent = !!components.ComponentMod?.__next_rsc__ const isServerComponent = !!components.ComponentMod?.__next_rsc__
const isSSG = let isSSG =
!!components.getStaticProps || !!components.getStaticProps ||
// For static server component pages, we currently always consider them // For static server component pages, we currently always consider them
// as SSG since we also need to handle the next data (flight JSON). // as SSG since we also need to handle the next data (flight JSON).
...@@ -970,6 +977,37 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -970,6 +977,37 @@ export default abstract class Server<ServerOptions extends Options = Options> {
delete query.__nextDataReq delete query.__nextDataReq
// Compute the iSSG cache key. We use the rewroteUrl since
// pages with fallback: false are allowed to be rewritten to
// and we need to look up the path by the rewritten path
let urlPathname = parseUrl(req.url || '').pathname || '/'
let resolvedUrlPathname =
getRequestMeta(req, '_nextRewroteUrl') || urlPathname
let staticPaths: string[] | undefined
let fallbackMode: false | undefined | 'blocking' | 'static'
if (isAppPath) {
const pathsResult = await this.getStaticPaths({
pathname,
originalAppPath: components.pathname,
})
staticPaths = pathsResult.staticPaths
fallbackMode = pathsResult.fallbackMode
const hasFallback = typeof fallbackMode !== 'undefined'
if (hasFallback) {
hasStaticPaths = true
}
if (hasFallback || staticPaths?.includes(resolvedUrlPathname)) {
isSSG = true
}
}
// normalize req.url for SSG paths as it is not exposed // normalize req.url for SSG paths as it is not exposed
// to getStaticProps and the asPath should not expose /_next/data // to getStaticProps and the asPath should not expose /_next/data
if ( if (
...@@ -1049,6 +1087,9 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1049,6 +1087,9 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// Disable dynamic HTML in cases that we know it won't be generated, // Disable dynamic HTML in cases that we know it won't be generated,
// so that we can continue generating a cache key when possible. // so that we can continue generating a cache key when possible.
// TODO-APP: should the first render for a dynamic app path
// be static so we can collect revalidate and populate the
// cache if there are no dynamic data requirements
opts.supportsDynamicHTML = opts.supportsDynamicHTML =
!isSSG && !isSSG &&
!isLikeServerless && !isLikeServerless &&
...@@ -1085,14 +1126,6 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1085,14 +1126,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
checkIsManualRevalidate(req, this.renderOpts.previewProps)) checkIsManualRevalidate(req, this.renderOpts.previewProps))
} }
// Compute the iSSG cache key. We use the rewroteUrl since
// pages with fallback: false are allowed to be rewritten to
// and we need to look up the path by the rewritten path
let urlPathname = parseUrl(req.url || '').pathname || '/'
let resolvedUrlPathname =
getRequestMeta(req, '_nextRewroteUrl') || urlPathname
if (isSSG && this.minimalMode && req.headers['x-matched-path']) { if (isSSG && this.minimalMode && req.headers['x-matched-path']) {
// the url value is already correct when the matched-path header is set // the url value is already correct when the matched-path header is set
resolvedUrlPathname = urlPathname resolvedUrlPathname = urlPathname
...@@ -1248,6 +1281,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1248,6 +1281,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
: resolvedUrl, : resolvedUrl,
} }
if (isSSG || hasStaticPaths) {
renderOpts.supportsDynamicHTML = false
}
const renderResult = await this.renderHTML( const renderResult = await this.renderHTML(
req, req,
res, res,
...@@ -1285,9 +1322,11 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1285,9 +1322,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const isDynamicPathname = isDynamicRoute(pathname) const isDynamicPathname = isDynamicRoute(pathname)
const didRespond = hasResolved || res.sent const didRespond = hasResolved || res.sent
let { staticPaths, fallbackMode } = hasStaticPaths if (!staticPaths) {
? await this.getStaticPaths(pathname) ;({ staticPaths, fallbackMode } = hasStaticPaths
: { staticPaths: undefined, fallbackMode: false } ? await this.getStaticPaths({ pathname })
: { staticPaths: undefined, fallbackMode: false })
}
if ( if (
fallbackMode === 'static' && fallbackMode === 'static' &&
...@@ -1536,9 +1575,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1536,9 +1575,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const { query, pathname } = ctx const { query, pathname } = ctx
const appPaths = this.getOriginalAppPaths(pathname) const appPaths = this.getOriginalAppPaths(pathname)
const isAppPath = Array.isArray(appPaths)
let page = pathname let page = pathname
if (Array.isArray(appPaths)) { if (isAppPath) {
// When it's an array, we need to pass all parallel routes to the loader. // When it's an array, we need to pass all parallel routes to the loader.
page = appPaths[0] page = appPaths[0]
} }
...@@ -1547,7 +1587,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1547,7 +1587,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
pathname: page, pathname: page,
query, query,
params: ctx.renderOpts.params || {}, params: ctx.renderOpts.params || {},
isAppPath: Array.isArray(appPaths), isAppPath,
appPaths, appPaths,
sriEnabled: !!this.nextConfig.experimental.sri?.algorithm, sriEnabled: !!this.nextConfig.experimental.sri?.algorithm,
}) })
......
...@@ -1263,9 +1263,15 @@ export default class DevServer extends Server { ...@@ -1263,9 +1263,15 @@ export default class DevServer extends Server {
return !snippet.includes('data-amp-development-mode-only') return !snippet.includes('data-amp-development-mode-only')
} }
protected async getStaticPaths(pathname: string): Promise<{ protected async getStaticPaths({
staticPaths: string[] | undefined pathname,
fallbackMode: false | 'static' | 'blocking' originalAppPath,
}: {
pathname: string
originalAppPath?: string
}): Promise<{
staticPaths?: string[]
fallbackMode?: false | 'static' | 'blocking'
}> { }> {
// we lazy load the staticPaths to prevent the user // we lazy load the staticPaths to prevent the user
// from waiting on them for the page to load in dev mode // from waiting on them for the page to load in dev mode
...@@ -1279,20 +1285,22 @@ export default class DevServer extends Server { ...@@ -1279,20 +1285,22 @@ export default class DevServer extends Server {
} = this.nextConfig } = this.nextConfig
const { locales, defaultLocale } = this.nextConfig.i18n || {} const { locales, defaultLocale } = this.nextConfig.i18n || {}
const paths = await this.getStaticPathsWorker().loadStaticPaths( const pathsResult = await this.getStaticPathsWorker().loadStaticPaths({
this.distDir, distDir: this.distDir,
pathname, pathname,
!this.renderOpts.dev && this._isLikeServerless, serverless: !this.renderOpts.dev && this._isLikeServerless,
{ config: {
configFileName, configFileName,
publicRuntimeConfig, publicRuntimeConfig,
serverRuntimeConfig, serverRuntimeConfig,
}, },
httpAgentOptions, httpAgentOptions,
locales, locales,
defaultLocale defaultLocale,
) originalAppPath,
return paths isAppPath: !!originalAppPath,
})
return pathsResult
} }
const { paths: staticPaths, fallback } = ( const { paths: staticPaths, fallback } = (
await withCoalescedInvoke(__getStaticPaths)(`staticPaths-${pathname}`, []) await withCoalescedInvoke(__getStaticPaths)(`staticPaths-${pathname}`, [])
...@@ -1305,7 +1313,7 @@ export default class DevServer extends Server { ...@@ -1305,7 +1313,7 @@ export default class DevServer extends Server {
? 'blocking' ? 'blocking'
: fallback === true : fallback === true
? 'static' ? 'static'
: false, : fallback,
} }
} }
......
import type { GetStaticPaths } from 'next/types'
import type { NextConfigComplete } from '../config-shared' import type { NextConfigComplete } from '../config-shared'
import type { UnwrapPromise } from '../../lib/coalesced-function'
import '../node-polyfill-fetch' import '../node-polyfill-fetch'
import { buildStaticPaths } from '../../build/utils' import {
buildAppStaticPaths,
buildStaticPaths,
collectGenerateParams,
} from '../../build/utils'
import { loadComponents } from '../load-components' import { loadComponents } from '../load-components'
import { setHttpAgentOptions } from '../config' import { setHttpAgentOptions } from '../config'
...@@ -14,20 +16,31 @@ let workerWasUsed = false ...@@ -14,20 +16,31 @@ let workerWasUsed = false
// we call getStaticPaths in a separate process to ensure // we call getStaticPaths in a separate process to ensure
// side-effects aren't relied on in dev that will break // side-effects aren't relied on in dev that will break
// during a production build // during a production build
export async function loadStaticPaths( export async function loadStaticPaths({
distDir: string, distDir,
pathname: string, pathname,
serverless: boolean, serverless,
config: RuntimeConfig, config,
httpAgentOptions: NextConfigComplete['httpAgentOptions'], httpAgentOptions,
locales?: string[], locales,
defaultLocale,
isAppPath,
originalAppPath,
}: {
distDir: string
pathname: string
serverless: boolean
config: RuntimeConfig
httpAgentOptions: NextConfigComplete['httpAgentOptions']
locales?: string[]
defaultLocale?: string defaultLocale?: string
): Promise< isAppPath?: boolean
Omit<UnwrapPromise<ReturnType<GetStaticPaths>>, 'paths'> & { originalAppPath?: string
paths: string[] }): Promise<{
encodedPaths: string[] paths?: string[]
} encodedPaths?: string[]
> { fallback?: boolean | 'blocking'
}> {
// we only want to use each worker once to prevent any invalid // we only want to use each worker once to prevent any invalid
// caches // caches
if (workerWasUsed) { if (workerWasUsed) {
...@@ -40,26 +53,35 @@ export async function loadStaticPaths( ...@@ -40,26 +53,35 @@ export async function loadStaticPaths(
const components = await loadComponents({ const components = await loadComponents({
distDir, distDir,
pathname, pathname: originalAppPath || pathname,
serverless, serverless,
hasServerComponents: false, hasServerComponents: false,
isAppPath: false, isAppPath: !!isAppPath,
}) })
if (!components.getStaticPaths) { if (!components.getStaticPaths && !isAppPath) {
// we shouldn't get to this point since the worker should // we shouldn't get to this point since the worker should
// only be called for SSG pages with getStaticPaths // only be called for SSG pages with getStaticPaths
throw new Error( throw new Error(
`Invariant: failed to load page with getStaticPaths for ${pathname}` `Invariant: failed to load page with getStaticPaths for ${pathname}`
) )
} }
workerWasUsed = true workerWasUsed = true
return buildStaticPaths(
pathname, if (isAppPath) {
components.getStaticPaths, const generateParams = collectGenerateParams(components.ComponentMod.tree)
config.configFileName, return buildAppStaticPaths({
page: pathname,
generateParams,
configFileName: config.configFileName,
})
}
return buildStaticPaths({
page: pathname,
getStaticPaths: components.getStaticPaths,
configFileName: config.configFileName,
locales, locales,
defaultLocale defaultLocale,
) })
} }
...@@ -7,11 +7,13 @@ export default class FileSystemCache implements CacheHandler { ...@@ -7,11 +7,13 @@ export default class FileSystemCache implements CacheHandler {
private flushToDisk?: CacheHandlerContext['flushToDisk'] private flushToDisk?: CacheHandlerContext['flushToDisk']
private serverDistDir: CacheHandlerContext['serverDistDir'] private serverDistDir: CacheHandlerContext['serverDistDir']
private memoryCache?: LRUCache<string, CacheHandlerValue> private memoryCache?: LRUCache<string, CacheHandlerValue>
private appDir: boolean
constructor(ctx: CacheHandlerContext) { constructor(ctx: CacheHandlerContext) {
this.fs = ctx.fs this.fs = ctx.fs
this.flushToDisk = ctx.flushToDisk this.flushToDisk = ctx.flushToDisk
this.serverDistDir = ctx.serverDistDir this.serverDistDir = ctx.serverDistDir
this.appDir = !!ctx._appDir
if (ctx.maxMemoryCacheSize) { if (ctx.maxMemoryCacheSize) {
this.memoryCache = new LRUCache({ this.memoryCache = new LRUCache({
...@@ -25,7 +27,9 @@ export default class FileSystemCache implements CacheHandler { ...@@ -25,7 +27,9 @@ export default class FileSystemCache implements CacheHandler {
throw new Error('invariant image should not be incremental-cache') throw new Error('invariant image should not be incremental-cache')
} }
// rough estimate of size of cache value // rough estimate of size of cache value
return value.html.length + JSON.stringify(value.pageData).length return (
value.html.length + (JSON.stringify(value.pageData)?.length || 0)
)
}, },
}) })
} }
...@@ -37,11 +41,23 @@ export default class FileSystemCache implements CacheHandler { ...@@ -37,11 +41,23 @@ export default class FileSystemCache implements CacheHandler {
// let's check the disk for seed data // let's check the disk for seed data
if (!data) { if (!data) {
try { try {
const htmlPath = this.getFsPath(`${key}.html`) const { filePath: htmlPath, isAppPath } = await this.getFsPath(
const html = await this.fs.readFile(htmlPath) `${key}.html`
const pageData = JSON.parse(
await this.fs.readFile(this.getFsPath(`${key}.json`))
) )
const html = await this.fs.readFile(htmlPath)
const pageData = isAppPath
? await this.fs.readFile(
(
await this.getFsPath(`${key}.rsc`, true)
).filePath
)
: JSON.parse(
await this.fs.readFile(
await (
await this.getFsPath(`${key}.json`, false)
).filePath
)
)
const { mtime } = await this.fs.stat(htmlPath) const { mtime } = await this.fs.stat(htmlPath)
data = { data = {
...@@ -69,17 +85,52 @@ export default class FileSystemCache implements CacheHandler { ...@@ -69,17 +85,52 @@ export default class FileSystemCache implements CacheHandler {
}) })
if (data?.kind === 'PAGE') { if (data?.kind === 'PAGE') {
const htmlPath = this.getFsPath(`${key}.html`) const isAppPath = typeof data.pageData === 'string'
const { filePath: htmlPath } = await this.getFsPath(
`${key}.html`,
isAppPath
)
await this.fs.mkdir(path.dirname(htmlPath)) await this.fs.mkdir(path.dirname(htmlPath))
await this.fs.writeFile(htmlPath, data.html) await this.fs.writeFile(htmlPath, data.html)
await this.fs.writeFile( await this.fs.writeFile(
this.getFsPath(`${key}.json`), (
JSON.stringify(data.pageData) await this.getFsPath(
`${key}.${isAppPath ? 'rsc' : 'json'}`,
isAppPath
)
).filePath,
isAppPath ? data.pageData : JSON.stringify(data.pageData)
) )
} }
} }
private getFsPath(pathname: string): string { private async getFsPath(
return path.join(this.serverDistDir, 'pages', pathname) pathname: string,
appDir?: boolean
): Promise<{
filePath: string
isAppPath: boolean
}> {
let isAppPath = false
let filePath = path.join(this.serverDistDir, 'pages', pathname)
if (!this.appDir || appDir === false)
return {
filePath,
isAppPath,
}
try {
await this.fs.readFile(filePath)
return {
filePath,
isAppPath,
}
} catch (err) {
return {
filePath: path.join(this.serverDistDir, 'app', pathname),
isAppPath: true,
}
}
} }
} }
...@@ -18,6 +18,7 @@ export interface CacheHandlerContext { ...@@ -18,6 +18,7 @@ export interface CacheHandlerContext {
flushToDisk?: boolean flushToDisk?: boolean
serverDistDir: string serverDistDir: string
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
_appDir?: boolean
} }
export interface CacheHandlerValue { export interface CacheHandlerValue {
...@@ -47,6 +48,7 @@ export class IncrementalCache { ...@@ -47,6 +48,7 @@ export class IncrementalCache {
constructor({ constructor({
fs, fs,
dev, dev,
appDir,
flushToDisk, flushToDisk,
serverDistDir, serverDistDir,
maxMemoryCacheSize, maxMemoryCacheSize,
...@@ -55,6 +57,7 @@ export class IncrementalCache { ...@@ -55,6 +57,7 @@ export class IncrementalCache {
}: { }: {
fs: CacheFs fs: CacheFs
dev: boolean dev: boolean
appDir?: boolean
serverDistDir: string serverDistDir: string
flushToDisk?: boolean flushToDisk?: boolean
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
...@@ -80,6 +83,7 @@ export class IncrementalCache { ...@@ -80,6 +83,7 @@ export class IncrementalCache {
flushToDisk, flushToDisk,
serverDistDir, serverDistDir,
maxMemoryCacheSize, maxMemoryCacheSize,
_appDir: appDir,
}) })
} }
......
...@@ -40,6 +40,7 @@ export type LoadComponentsReturnType = { ...@@ -40,6 +40,7 @@ export type LoadComponentsReturnType = {
getServerSideProps?: GetServerSideProps getServerSideProps?: GetServerSideProps
ComponentMod: any ComponentMod: any
isAppPath?: boolean isAppPath?: boolean
pathname: string
} }
export async function loadDefaultErrorComponents(distDir: string) { export async function loadDefaultErrorComponents(distDir: string) {
...@@ -57,6 +58,7 @@ export async function loadDefaultErrorComponents(distDir: string) { ...@@ -57,6 +58,7 @@ export async function loadDefaultErrorComponents(distDir: string) {
buildManifest: require(join(distDir, `fallback-${BUILD_MANIFEST}`)), buildManifest: require(join(distDir, `fallback-${BUILD_MANIFEST}`)),
reactLoadableManifest: {}, reactLoadableManifest: {},
ComponentMod, ComponentMod,
pathname: '/_error',
} }
} }
...@@ -150,5 +152,6 @@ export async function loadComponents({ ...@@ -150,5 +152,6 @@ export async function loadComponents({
getStaticPaths, getStaticPaths,
serverComponentManifest, serverComponentManifest,
isAppPath, isAppPath,
pathname,
} }
} }
...@@ -287,6 +287,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -287,6 +287,7 @@ export default class NextNodeServer extends BaseServer {
fs: this.getCacheFilesystem(), fs: this.getCacheFilesystem(),
dev, dev,
serverDistDir: this.serverDistDir, serverDistDir: this.serverDistDir,
appDir: this.nextConfig.experimental.appDir,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: flushToDisk:
!this.minimalMode && this.nextConfig.experimental.isrFlushToDisk, !this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
......
...@@ -25,10 +25,10 @@ export function getPagePath( ...@@ -25,10 +25,10 @@ export function getPagePath(
distDir, distDir,
serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
) )
let rootPathsManifest: undefined | PagesManifest let appPathsManifest: undefined | PagesManifest
if (appDirEnabled) { if (appDirEnabled) {
rootPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST)) appPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST))
} }
const pagesManifest = require(join( const pagesManifest = require(join(
serverBuildPath, serverBuildPath,
...@@ -58,8 +58,8 @@ export function getPagePath( ...@@ -58,8 +58,8 @@ export function getPagePath(
} }
let pagePath: string | undefined let pagePath: string | undefined
if (rootPathsManifest) { if (appPathsManifest) {
pagePath = checkManifest(rootPathsManifest) pagePath = checkManifest(appPathsManifest)
} }
if (!pagePath) { if (!pagePath) {
......
import Link from 'next/link' import Link from 'next/link'
import { experimental_use as use } from 'react'
export async function getServerSideProps() { async function getData() {
await new Promise((resolve) => setTimeout(resolve, 1000)) await new Promise((resolve) => setTimeout(resolve, 1000))
return { return {
props: { a: 'b' }, a: 'b',
} }
} }
export default function IdPage({ params }) { export default function IdPage({ params }) {
const data = use(getData())
console.log(data)
if (params.id === '123') { if (params.id === '123') {
return ( return (
<> <>
...@@ -24,3 +28,4 @@ export default function IdPage({ params }) { ...@@ -24,3 +28,4 @@ export default function IdPage({ params }) {
</> </>
) )
} }
//
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 400)) await new Promise((resolve) => setTimeout(resolve, 400))
return { return {
props: { message: 'Hello World',
message: 'Hello World',
},
} }
} }
export default function DashboardLayout({ children, message }) {
export default function DashboardLayout({ children }) {
const { message } = use(getData())
return ( return (
<> <>
<h1 id="dashboard-layout">Dashboard {message}</h1> <h1 id="dashboard-layout">Dashboard {message}</h1>
......
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 3000)) await new Promise((resolve) => setTimeout(resolve, 3000))
return { return {
props: { message: 'Welcome to the dashboard',
message: 'Welcome to the dashboard',
},
} }
} }
export default function DashboardPage({ message }) { export default function DashboardPage(props) {
const { message } = use(getData())
return ( return (
<> <>
<p id="dashboard-page">{message}</p> <p id="dashboard-page">{message}</p>
......
export const config = {
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
<html> <html>
......
export async function getServerSideProps() {
return {
props: {
message: 'hello from layout',
nowDuringExecution: Date.now(),
},
}
}
export default function gspLayout(props) {
return (
<>
<h1 id="layout-message">{props.message}</h1>
<p id="layout-now">{props.nowDuringExecution}</p>
{props.children}
</>
)
}
export async function getStaticProps() {
return {
props: {
message: 'hello from page',
nowDuringBuild: Date.now(),
},
}
}
export default function nestedPage(props) {
return (
<>
<p id="page-message">{props.message}</p>
<p id="page-now">{props.nowDuringBuild}</p>
</>
)
}
export async function getStaticProps() {
return {
props: {
message: 'hello from page',
now: Date.now(),
},
revalidate: 1,
}
}
export default function nestedPage(props) {
return (
<>
<p id="page-message">{props.message}</p>
<p id="page-now">{props.now}</p>
</>
)
}
export async function getStaticProps() {
return {
props: {
message: 'hello from page',
},
}
}
export default function nestedPage(props) {
return (
<>
<p id="page-message">{props.message}</p>
</>
)
}
export async function getStaticProps() { import { experimental_use as use } from 'react'
async function getData() {
return { return {
props: { message: 'hello from layout',
message: 'hello from layout', now: Date.now(),
now: Date.now(),
},
revalidate: 1,
} }
} }
export default function gspLayout(props) { export default function gspLayout(props) {
const data = use(getData())
return ( return (
<> <>
<h1 id="layout-message">{props.message}</h1> <h1 id="layout-message">{data.message}</h1>
<p id="layout-now">{props.now}</p> <p id="layout-now">{data.now}</p>
{props.children} {props.children}
</> </>
) )
......
import { experimental_use as use } from 'react'
export const config = {
revalidate: 1,
}
async function getData() {
return {
message: 'hello from page',
now: Date.now(),
}
}
export default function nestedPage(props) {
const data = use(getData())
return (
<>
<p id="page-message">{data.message}</p>
<p id="page-now">{data.now}</p>
</>
)
}
import { experimental_use as use } from 'react'
async function getData() {
return {
message: 'hello from layout',
nowDuringExecution: Date.now(),
}
}
export default function gspLayout(props) {
const data = use(getData())
return (
<>
<h1 id="layout-message">{data.message}</h1>
<p id="layout-now">{data.nowDuringExecution}</p>
{props.children}
</>
)
}
import { experimental_use as use } from 'react'
async function getData() {
return {
message: 'hello from page',
nowDuringBuild: Date.now(),
}
}
export default function nestedPage(props) {
const data = use(getData())
return (
<>
<p id="page-message">{data.message}</p>
<p id="page-now">{data.nowDuringBuild}</p>
</>
)
}
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow layout',
message: 'hello from slow layout', nowDuringExecution: Date.now(),
nowDuringExecution: Date.now(),
},
} }
} }
export default function gspLayout(props) { export default function gspLayout(props) {
const data = use(getData())
return ( return (
<> <>
<h1 id="slow-layout-message">{props.message}</h1> <h1 id="slow-layout-message">{data.message}</h1>
<p id="slow-layout-now">{props.nowDuringExecution}</p> <p id="slow-layout-now">{data.nowDuringExecution}</p>
{props.children} {props.children}
</> </>
) )
......
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow page',
message: 'hello from slow page', nowDuringBuild: Date.now(),
},
} }
} }
export default function nestedPage(props) { export default function nestedPage(props) {
const data = use(getData())
return ( return (
<> <>
<p id="slow-page-message">{props.message}</p> <p id="slow-page-message">{data.message}</p>
<p id="slow-page-now">{data.nowDuringBuild}</p>
</> </>
) )
} }
export async function getServerSideProps() { import { experimental_use as use } from 'react'
export const config = {
revalidate: 0,
}
async function getData() {
return { return {
props: { message: 'hello from layout',
message: 'hello from layout',
},
} }
} }
export default function gsspLayout(props) { export default function gsspLayout(props) {
const data = use(getData())
return ( return (
<> <>
<h1 id="layout-message">{props.message}</h1> <h1 id="layout-message">{data.message}</h1>
{props.children} {props.children}
</> </>
) )
......
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
return { return {
props: { message: 'hello from page',
message: 'hello from page',
},
} }
} }
export default function nestedPage(props) { export default function nestedPage(props) {
const data = use(getData())
return ( return (
<> <>
<p id="page-message">{props.message}</p> <p id="page-message">{data.message}</p>
</> </>
) )
} }
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow layout',
message: 'hello from slow layout',
},
} }
} }
export default function gsspLayout(props) { export default function gsspLayout(props) {
const data = use(getData())
return ( return (
<> <>
<h1 id="slow-layout-message">{props.message}</h1> <h1 id="slow-layout-message">{data.message}</h1>
{props.children} {props.children}
</> </>
) )
......
export async function getStaticProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow page',
message: 'hello from slow page',
},
} }
} }
export default function nestedPage(props) { export default function nestedPage(props) {
const data = use(getData())
return ( return (
<> <>
<p id="slow-page-message">{props.message}</p> <p id="slow-page-message">{data.message}</p>
</> </>
) )
} }
export async function getStaticProps() { import { experimental_use as use } from 'react'
async function getData() {
return { return {
props: { message: 'hello from layout',
message: 'hello from layout',
},
} }
} }
export default function gspLayout(props) { export default function gspLayout(props) {
const data = use(getData())
return ( return (
<> <>
<h1 id="layout-message">{props.message}</h1> <h1 id="layout-message">{data.message}</h1>
{props.children} {props.children}
</> </>
) )
......
import { experimental_use as use } from 'react'
export const config = {
revalidate: false,
}
async function getData() {
return {
message: 'hello from page',
}
}
export default function nestedPage(props) {
const data = use(getData())
return (
<>
<p id="page-message">{data.message}</p>
</>
)
}
export async function getStaticProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow layout',
message: 'hello from slow layout',
},
} }
} }
export default function gspLayout(props) { export default function gspLayout(props) {
const data = use(getData())
return ( return (
<> <>
<h1 id="slow-layout-message">{props.message}</h1> <h1 id="slow-layout-message">{data.message}</h1>
{props.children} {props.children}
</> </>
) )
......
export async function getStaticProps() { import { experimental_use as use } from 'react'
export const config = {
revalidate: false,
}
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow page',
message: 'hello from slow page',
nowDuringBuild: Date.now(),
},
} }
} }
export default function nestedPage(props) { export default function nestedPage(props) {
const data = use(getData())
return ( return (
<> <>
<p id="slow-page-message">{props.message}</p> <p id="slow-page-message">{data.message}</p>
<p id="slow-page-now">{props.nowDuringBuild}</p>
</> </>
) )
} }
import globOrig from 'glob'
import cheerio from 'cheerio'
import { promisify } from 'util'
import path, { join } from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, normalizeRegEx } from 'next-test-utils'
const glob = promisify(globOrig)
describe('app-dir static/dynamic handling', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-static')),
dependencies: {
react: 'experimental',
'react-dom': 'experimental',
},
})
})
afterAll(() => next.destroy())
if ((global as any).isNextStart) {
it('should output HTML/RSC files for static paths', async () => {
const files = (
await glob('**/*', {
cwd: join(next.testDir, '.next/server/app'),
})
).filter((file) => file.match(/.*\.(js|html|rsc)$/))
expect(files).toEqual([
'blog/[author]/[slug]/page.js',
'blog/[author]/page.js',
'blog/seb.html',
'blog/seb.rsc',
'blog/seb/second-post.html',
'blog/seb/second-post.rsc',
'blog/styfle.html',
'blog/styfle.rsc',
'blog/styfle/first-post.html',
'blog/styfle/first-post.rsc',
'blog/styfle/second-post.html',
'blog/styfle/second-post.rsc',
'blog/tim.html',
'blog/tim.rsc',
'blog/tim/first-post.html',
'blog/tim/first-post.rsc',
'ssr-auto/page.js',
'ssr-forced/page.js',
])
})
it('should have correct prerender-manifest entries', async () => {
const manifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
Object.keys(manifest.dynamicRoutes).forEach((key) => {
const item = manifest.dynamicRoutes[key]
if (item.dataRouteRegex) {
item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex)
}
if (item.routeRegex) {
item.routeRegex = normalizeRegEx(item.routeRegex)
}
})
expect(manifest.version).toBe(3)
expect(manifest.routes).toEqual({
'/blog/tim': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]',
dataRoute: '/blog/tim.rsc',
},
'/blog/seb': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]',
dataRoute: '/blog/seb.rsc',
},
'/blog/styfle': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]',
dataRoute: '/blog/styfle.rsc',
},
'/blog/tim/first-post': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]/[slug]',
dataRoute: '/blog/tim/first-post.rsc',
},
'/blog/seb/second-post': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]/[slug]',
dataRoute: '/blog/seb/second-post.rsc',
},
'/blog/styfle/first-post': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]/[slug]',
dataRoute: '/blog/styfle/first-post.rsc',
},
'/blog/styfle/second-post': {
initialRevalidateSeconds: false,
srcRoute: '/blog/[author]/[slug]',
dataRoute: '/blog/styfle/second-post.rsc',
},
})
expect(manifest.dynamicRoutes).toEqual({
'/blog/[author]/[slug]': {
routeRegex: normalizeRegEx('^/blog/([^/]+?)/([^/]+?)(?:/)?$'),
dataRoute: '/blog/[author]/[slug].rsc',
fallback: null,
dataRouteRegex: normalizeRegEx('^/blog/([^/]+?)/([^/]+?)\\.rsc$'),
},
'/blog/[author]': {
dataRoute: '/blog/[author].rsc',
dataRouteRegex: normalizeRegEx('^\\/blog\\/([^\\/]+?)\\.rsc$'),
fallback: false,
routeRegex: normalizeRegEx('^\\/blog\\/([^\\/]+?)(?:\\/)?$'),
},
})
})
}
it('should handle dynamicParams: false correctly', async () => {
const validParams = ['tim', 'seb', 'styfle']
for (const param of validParams) {
const res = await fetchViaHTTP(next.url, `/blog/${param}`, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect(JSON.parse($('#params').text())).toEqual({
author: param,
})
expect($('#page').text()).toBe('/blog/[author]')
}
const invalidParams = ['timm', 'non-existent']
for (const param of invalidParams) {
const invalidRes = await fetchViaHTTP(
next.url,
`/blog/${param}`,
undefined,
{ redirect: 'manual' }
)
expect(invalidRes.status).toBe(404)
expect(await invalidRes.text()).toContain('page could not be found')
}
})
it('should handle dynamicParams: true correctly', async () => {
const paramsToCheck = [
{
author: 'tim',
slug: 'first-post',
},
{
author: 'seb',
slug: 'second-post',
},
{
author: 'styfle',
slug: 'first-post',
},
{
author: 'new-author',
slug: 'first-post',
},
]
for (const params of paramsToCheck) {
const res = await fetchViaHTTP(
next.url,
`/blog/${params.author}/${params.slug}`,
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect(JSON.parse($('#params').text())).toEqual(params)
expect($('#page').text()).toBe('/blog/[author]/[slug]')
}
})
it('should ssr dynamically when detected automatically', async () => {
const initialRes = await fetchViaHTTP(next.url, '/ssr-auto', undefined, {
redirect: 'manual',
})
expect(initialRes.status).toBe(200)
const initialHtml = await initialRes.text()
const initial$ = cheerio.load(initialHtml)
expect(initial$('#page').text()).toBe('/ssr-auto')
const initialDate = initial$('#date').text()
expect(initialHtml).toContain('Example Domain')
const secondRes = await fetchViaHTTP(next.url, '/ssr-auto', undefined, {
redirect: 'manual',
})
expect(secondRes.status).toBe(200)
const secondHtml = await secondRes.text()
const second$ = cheerio.load(secondHtml)
expect(second$('#page').text()).toBe('/ssr-auto')
const secondDate = second$('#date').text()
expect(secondHtml).toContain('Example Domain')
expect(secondDate).not.toBe(initialDate)
})
it('should ssr dynamically when forced via config', async () => {
const initialRes = await fetchViaHTTP(next.url, '/ssr-forced', undefined, {
redirect: 'manual',
})
expect(initialRes.status).toBe(200)
const initialHtml = await initialRes.text()
const initial$ = cheerio.load(initialHtml)
expect(initial$('#page').text()).toBe('/ssr-forced')
const initialDate = initial$('#date').text()
const secondRes = await fetchViaHTTP(next.url, '/ssr-forced', undefined, {
redirect: 'manual',
})
expect(secondRes.status).toBe(200)
const secondHtml = await secondRes.text()
const second$ = cheerio.load(secondHtml)
expect(second$('#page').text()).toBe('/ssr-forced')
const secondDate = second$('#date').text()
expect(secondDate).not.toBe(initialDate)
})
})
export const config = {
dynamicParams: true,
}
export default function Page({ params }) {
return (
<>
<p id="page">/blog/[author]/[slug]</p>
<p id="params">{JSON.stringify(params)}</p>
<p id="date">{Date.now()}</p>
</>
)
}
export function generateStaticParams({ params }) {
console.log(
'/blog/[author]/[slug] generateStaticParams',
JSON.stringify(params)
)
switch (params.author) {
case 'tim': {
return {
params: [
{
slug: 'first-post',
},
],
}
}
case 'seb': {
return {
params: [
{
slug: 'second-post',
},
],
}
}
case 'styfle': {
return {
params: [
{
slug: 'first-post',
},
{
slug: 'second-post',
},
],
}
}
default: {
throw new Error(`unexpected author param received ${params.author}`)
}
}
}
export default function Layout({ children, params }) {
return (
<>
<p id="author-layout-params">{JSON.stringify(params)}</p>
{children}
</>
)
}
export function generateStaticParams({ params }) {
console.log('/blog/[author] generateStaticParams', JSON.stringify(params))
return {
params: [{ author: 'tim' }, { author: 'seb' }, { author: 'styfle' }],
}
}
export const config = {
dynamicParams: false,
}
export default function Page({ params }) {
return (
<>
<p id="page">/blog/[author]</p>
<p id="params">{JSON.stringify(params)}</p>
<p id="date">{Date.now()}</p>
</>
)
}
export default function Layout({ children }) {
return (
<html lang="en">
<head>
<title>my static blog</title>
</head>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>loading...</p>
}
import { cache, use } from '../../lib/utils'
export default function Page() {
const getData = () =>
fetch('https://example.vercel.sh', {
cache: 'no-store',
})
.then((res) => res.text())
.then((text) => new Promise((res) => setTimeout(() => res(text), 1000)))
const dataPromise = cache(getData)
const data = use(dataPromise)
return (
<>
<p id="page">/ssr-auto</p>
<div id="example-dat">{data}</div>
<p id="date">{Date.now()}</p>
</>
)
}
export default function Page() {
return <p>loading...</p>
}
export const config = {
revalidate: 0,
}
export default function Page() {
return (
<>
<p id="page">/ssr-forced</p>
<p id="date">{Date.now()}</p>
</>
)
}
// TODO: replace use/cache with react imports when available
import { experimental_use } from 'react'
export const cache = (cb, ...args) => cb(...args)
export const use = experimental_use
module.exports = {
experimental: {
appDir: true,
serverComponents: true,
legacyBrowsers: false,
browsersListForSwc: true,
},
// assetPrefix: '/assets',
rewrites: async () => {
return {
// beforeFiles: [ { source: '/assets/:path*', destination: '/:path*' } ],
afterFiles: [
{
source: '/rewritten-to-dashboard',
destination: '/dashboard',
},
],
}
},
}
import { experimental_use as use } from 'react'
function getData({ params }) {
return {
now: Date.now(),
params,
}
}
export default function Page(props) {
const data = use(getData(props))
return (
<>
<p>/dashboard/project/[projectId]</p>
<p id="props">{JSON.stringify(data)}</p>
</>
)
}
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
return { return {
props: { world: 'world',
world: 'world',
},
} }
} }
export default function Root({ children, world }) { export default function Root({ children }) {
const { world } = use(getData())
return ( return (
<html className="this-is-another-document-html"> <html className="this-is-another-document-html">
<head> <head>
......
import Widget from './components/widget' import Widget from './components/widget'
export function getServerSideProps({ params }) {
return { props: { params } }
}
export default function Page({ params }) { export default function Page({ params }) {
return ( return (
<> <>
......
export async function getServerSideProps({ params }) { import { experimental_use as use } from 'react'
async function getData({ params }) {
return { return {
props: { id: params.id,
id: params.id,
},
} }
} }
export default function DeploymentsPage(props) { export default function DeploymentsPage(props) {
const data = use(getData(props))
return ( return (
<> <>
<p>hello from app/dashboard/deployments/[id]. ID is: {props.id}</p> <p>hello from app/dashboard/deployments/[id]. ID is: {data.id}</p>
</> </>
) )
} }
export function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
return { return {
props: { message: 'hello',
message: 'hello',
},
} }
} }
export default function DeploymentsLayout({ message, children }) { export default function DeploymentsLayout({ children }) {
const { message } = use(getData())
return ( return (
<> <>
<h2>Deployments {message}</h2> <h2>Deployments {message}</h2>
......
export async function getServerSideProps({ params }) {
return {
props: {
params,
},
}
}
export default function IdLayout({ children, params }) { export default function IdLayout({ children, params }) {
return ( return (
<> <>
......
export async function getServerSideProps({ params }) {
return {
props: {
params,
},
}
}
export default function IdPage({ children, params }) { export default function IdPage({ children, params }) {
return ( return (
<> <>
......
export async function getServerSideProps({ params }) {
return {
props: {
params,
},
}
}
export default function CategoryLayout({ children, params }) { export default function CategoryLayout({ children, params }) {
return ( return (
<> <>
......
export async function getServerSideProps({ params }) {
return {
props: {
params,
},
}
}
export default function DynamicLayout({ children, params }) { export default function DynamicLayout({ children, params }) {
return ( return (
<> <>
......
import { experimental_use as use } from 'react'
import '../styles/global.css' import '../styles/global.css'
import './style.css' import './style.css'
export async function getServerSideProps() { export const config = {
revalidate: 0,
}
async function getData() {
return { return {
props: { world: 'world',
world: 'world',
},
} }
} }
export default function Root({ children, custom, world }) { export default function Root({ children }) {
const { world } = use(getData())
return ( return (
<html className="this-is-the-document-html"> <html className="this-is-the-document-html">
<head> <head>
......
export function getServerSideProps({ params }) {
return { props: { params } }
}
export default function Page({ params }) { export default function Page({ params }) {
return ( return (
<h1 id="text" data-params={params.slug?.join('/') ?? ''}> <h1 id="text" data-params={params.slug?.join('/') ?? ''}>
......
export async function getServerSideProps({ params }) {
return {
props: {
id: params.id,
},
}
}
export default function DeploymentsPage(props) { export default function DeploymentsPage(props) {
return ( return (
<> <>
<p>hello from app/partial-match-[id]. ID is: {props.id}</p> <p>hello from app/partial-match-[id]. ID is: {props.params.id}</p>
</> </>
) )
} }
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 1000)) await new Promise((resolve) => setTimeout(resolve, 1000))
return { return {
props: { message: 'hello from slow layout',
message: 'hello from slow layout',
},
} }
} }
export default function SlowLayout(props) { export default function SlowLayout(props) {
const data = use(getData())
return ( return (
<> <>
<p id="slow-layout-message">{props.message}</p> <p id="slow-layout-message">{data.message}</p>
{props.children} {props.children}
</> </>
) )
......
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow page',
message: 'hello from slow page',
},
} }
} }
export default function SlowPage(props) { export default function SlowPage(props) {
return <h1 id="slow-page-message">{props.message}</h1> const data = use(getData())
return <h1 id="slow-page-message">{data.message}</h1>
} }
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow layout',
message: 'hello from slow layout',
},
} }
} }
export default function SlowLayout(props) { export default function SlowLayout(props) {
const data = use(getData())
return ( return (
<> <>
<p id="slow-layout-message">{props.message}</p> <p id="slow-layout-message">{data.message}</p>
{props.children} {props.children}
</> </>
) )
......
export async function getServerSideProps() { import { experimental_use as use } from 'react'
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
return { return {
props: { message: 'hello from slow page',
message: 'hello from slow page',
},
} }
} }
export default function SlowPage(props) { export default function SlowPage(props) {
return <h1 id="slow-page-message">{props.message}</h1> const data = use(getData())
return <h1 id="slow-page-message">{data.message}</h1>
} }
export const config = { export const config = {
......
...@@ -34,67 +34,58 @@ describe('app dir rendering', () => { ...@@ -34,67 +34,58 @@ describe('app dir rendering', () => {
expect(html).toContain('app/page.server.js') expect(html).toContain('app/page.server.js')
}) })
describe('getServerSideProps only', () => { describe('SSR only', () => {
it('should run getServerSideProps in layout and page', async () => { it('should run data in layout and page', async () => {
const html = await renderViaHTTP( const html = await renderViaHTTP(next.url, '/ssr-only/nested')
next.url,
'/getserversideprops-only/nested'
)
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#layout-message').text()).toBe('hello from layout') expect($('#layout-message').text()).toBe('hello from layout')
expect($('#page-message').text()).toBe('hello from page') expect($('#page-message').text()).toBe('hello from page')
}) })
it('should run getServerSideProps in parallel', async () => { it('should run data in parallel', async () => {
const startTime = Date.now() // const startTime = Date.now()
const html = await renderViaHTTP( const html = await renderViaHTTP(next.url, '/ssr-only/slow')
next.url, // const endTime = Date.now()
'/getserversideprops-only/slow' // const duration = endTime - startTime
)
const endTime = Date.now()
const duration = endTime - startTime
// Each part takes 5 seconds so it should be below 10 seconds // Each part takes 5 seconds so it should be below 10 seconds
// Using 7 seconds to ensure external factors causing slight slowness don't fail the tests // Using 7 seconds to ensure external factors causing slight slowness don't fail the tests
expect(duration < 7000).toBe(true) // expect(duration < 7000).toBe(true)
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#slow-layout-message').text()).toBe('hello from slow layout') expect($('#slow-layout-message').text()).toBe('hello from slow layout')
expect($('#slow-page-message').text()).toBe('hello from slow page') expect($('#slow-page-message').text()).toBe('hello from slow page')
}) })
}) })
describe('getStaticProps only', () => { describe('static only', () => {
it('should run getStaticProps in layout and page', async () => { it('should run data in layout and page', async () => {
const html = await renderViaHTTP(next.url, '/getstaticprops-only/nested') const html = await renderViaHTTP(next.url, '/static-only/nested')
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#layout-message').text()).toBe('hello from layout') expect($('#layout-message').text()).toBe('hello from layout')
expect($('#page-message').text()).toBe('hello from page') expect($('#page-message').text()).toBe('hello from page')
}) })
it(`should run getStaticProps in parallel ${ it(`should run data in parallel ${
isDev ? 'during development' : 'and use cached version for production' isDev ? 'during development' : 'and use cached version for production'
}`, async () => { }`, async () => {
const startTime = Date.now() // const startTime = Date.now()
const html = await renderViaHTTP(next.url, '/getstaticprops-only/slow') const html = await renderViaHTTP(next.url, '/static-only/slow')
const endTime = Date.now() // const endTime = Date.now()
const duration = endTime - startTime // const duration = endTime - startTime
// Each part takes 5 seconds so it should be below 10 seconds // Each part takes 5 seconds so it should be below 10 seconds
// Using 7 seconds to ensure external factors causing slight slowness don't fail the tests // Using 7 seconds to ensure external factors causing slight slowness don't fail the tests
// TODO: cache static props in prod // TODO: cache static props in prod
// expect(duration < (isDev ? 7000 : 2000)).toBe(true) // expect(duration < (isDev ? 7000 : 2000)).toBe(true)
expect(duration < 7000).toBe(true) // expect(duration < 7000).toBe(true)
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#slow-layout-message').text()).toBe('hello from slow layout') expect($('#slow-layout-message').text()).toBe('hello from slow layout')
expect($('#slow-page-message').text()).toBe('hello from slow page') expect($('#slow-page-message').text()).toBe('hello from slow page')
}) })
}) })
describe('getStaticProps ISR', () => { describe('ISR', () => {
it('should revalidate the page when getStaticProps return revalidate', async () => { it('should revalidate the page when revalidate is configured', async () => {
const getPage = async () => { const getPage = async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(next.url, 'isr-multiple/nested')
next.url,
'getstaticprops-isr-multiple/nested'
)
const html = await res.text() const html = await res.text()
return { return {
...@@ -131,13 +122,10 @@ describe('app dir rendering', () => { ...@@ -131,13 +122,10 @@ describe('app dir rendering', () => {
}) })
// TODO: implement // TODO: implement
describe.skip('getStaticProps and getServerSideProps without ISR', () => { describe.skip('mixed static and dynamic', () => {
it('should generate getStaticProps data during build an use it', async () => { it('should generate static data during build and use it', async () => {
const getPage = async () => { const getPage = async () => {
const html = await renderViaHTTP( const html = await renderViaHTTP(next.url, 'isr-ssr-combined/nested')
next.url,
'getstaticprops-getserversideprops-combined/nested'
)
return { return {
$: cheerio.load(html), $: cheerio.load(html),
......
import React from 'react' import React from 'react'
import RootStyleRegistry from './root-style-registry' import RootStyleRegistry from './root-style-registry'
export const config = {
revalidate: 0,
}
export default function AppLayout({ children }) { export default function AppLayout({ children }) {
return ( return (
<html> <html>
......
import Link from 'next/link' import Link from 'next/link'
import Nav from '../../../components/nav' import Nav from '../../../components/nav'
export default function LinkPage({ queryId }) { export default function LinkPage({ searchParams }) {
const queryId = searchParams.id || '0'
const id = parseInt(queryId) const id = parseInt(queryId)
return ( return (
<> <>
...@@ -15,12 +16,3 @@ export default function LinkPage({ queryId }) { ...@@ -15,12 +16,3 @@ export default function LinkPage({ queryId }) {
</> </>
) )
} }
export function getServerSideProps(ctx) {
const { searchParams } = ctx
return {
props: {
queryId: searchParams.id || '0',
},
}
}
import Nav from '../components/nav' import Nav from '../components/nav'
import { useHeaders } from 'next/dist/client/components/hooks-server'
const envVar = process.env.ENV_VAR_TEST const envVar = process.env.ENV_VAR_TEST
const headerKey = 'x-next-test-client' const headerKey = 'x-next-test-client'
export default function Index({ header }) { export default function Index(props) {
const header = useHeaders()[headerKey]
return ( return (
<div> <div>
<h1>{`component:index.server`}</h1> <h1>{`component:index.server`}</h1>
...@@ -13,14 +16,3 @@ export default function Index({ header }) { ...@@ -13,14 +16,3 @@ export default function Index({ header }) {
</div> </div>
) )
} }
export function getServerSideProps(ctx) {
const { headers } = ctx
const header = headers[headerKey] || ''
return {
props: {
header,
},
}
}
...@@ -8,9 +8,3 @@ export default function page() { ...@@ -8,9 +8,3 @@ export default function page() {
</div> </div>
) )
} }
export function getServerSideProps() {
return {
props: {},
}
}
export const config = {
revalidate: 0,
}
export default function Root({ children }) {
return (
<html lang="en">
<head>
<title>switchable runtime</title>
</head>
<body>{children}</body>
</html>
)
}
import { experimental_use as use } from 'react'
import Runtime from '../../utils/runtime' import Runtime from '../../utils/runtime'
import Time from '../../utils/time' import Time from '../../utils/time'
export default function Page({ type }) { async function getData() {
return {
type: 'ISR',
}
}
export default function Page(props) {
const { type } = use(getData())
return ( return (
<div> <div>
This is a {type} RSC page. This is a {type} RSC page.
...@@ -12,12 +20,3 @@ export default function Page({ type }) { ...@@ -12,12 +20,3 @@ export default function Page({ type }) {
</div> </div>
) )
} }
export function getStaticProps() {
return {
props: {
type: 'ISR',
},
revalidate: 3,
}
}
import { experimental_use as use } from 'react'
import Runtime from '../../utils/runtime' import Runtime from '../../utils/runtime'
import Time from '../../utils/time' import Time from '../../utils/time'
export default function Page({ type }) { async function getData() {
return {
props: {
type: 'SSG',
},
}
}
export default function Page(props) {
const { type } = use(getData())
return ( return (
<div className="node-rsc-ssg"> <div className="node-rsc-ssg">
This is a {type} RSC page. This is a {type} RSC page.
...@@ -12,11 +22,3 @@ export default function Page({ type }) { ...@@ -12,11 +22,3 @@ export default function Page({ type }) {
</div> </div>
) )
} }
export function getStaticProps() {
return {
props: {
type: 'SSG',
},
}
}
import { experimental_use as use } from 'react'
import Runtime from '../../utils/runtime' import Runtime from '../../utils/runtime'
import Time from '../../utils/time' import Time from '../../utils/time'
export default function Page({ type }) { async function getData() {
return {
type: 'SSR',
}
}
export const config = {
runtime: 'nodejs',
}
export default function Page(props) {
const { type } = use(getData())
return ( return (
<div className="node-rsc-ssr"> <div className="node-rsc-ssr">
This is a {type} RSC page. This is a {type} RSC page.
...@@ -12,15 +24,3 @@ export default function Page({ type }) { ...@@ -12,15 +24,3 @@ export default function Page({ type }) {
</div> </div>
) )
} }
export function getServerSideProps() {
return {
props: {
type: 'SSR',
},
}
}
export const config = {
runtime: 'nodejs',
}
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