Unverified Commit afebda3f authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

[dio] Prospective Fallback Render (#71450)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
When Dynamic I/O is enabled and Partial Prerendering (PPR) is not, we
perform a prospective render of the fallback page in order to detect the
presence of a Suspense Boundary. When PPR is enabled and there is a
missing Suspense Boundary, this would trigger a build error.

The result of this render is discarded if the render completes without
error.
parent 29343e26
...@@ -3,7 +3,6 @@ import type { PagesManifest } from './webpack/plugins/pages-manifest-plugin' ...@@ -3,7 +3,6 @@ import type { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared' import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import type { MiddlewareManifest } from './webpack/plugins/middleware-plugin' import type { MiddlewareManifest } from './webpack/plugins/middleware-plugin'
import type { ActionManifest } from './webpack/plugins/flight-client-entry-plugin' import type { ActionManifest } from './webpack/plugins/flight-client-entry-plugin'
import type { ExportAppOptions } from '../export/types'
import type { Revalidate } from '../server/lib/revalidate' import type { Revalidate } from '../server/lib/revalidate'
import '../lib/setup-exception-listeners' import '../lib/setup-exception-listeners'
...@@ -128,7 +127,6 @@ import { ...@@ -128,7 +127,6 @@ import {
isAppBuiltinNotFoundPage, isAppBuiltinNotFoundPage,
collectRoutesUsingEdgeRuntime, collectRoutesUsingEdgeRuntime,
collectMeta, collectMeta,
// getSupportedBrowsers,
} from './utils' } from './utils'
import type { PageInfo, PageInfos, PrerenderedRoute } from './utils' import type { PageInfo, PageInfos, PrerenderedRoute } from './utils'
import type { AppSegmentConfig } from './segment-config/app/app-segment-config' import type { AppSegmentConfig } from './segment-config/app/app-segment-config'
...@@ -212,6 +210,7 @@ import { stitchBuilds } from './flying-shuttle/stitch-builds' ...@@ -212,6 +210,7 @@ import { stitchBuilds } from './flying-shuttle/stitch-builds'
import { inlineStaticEnv } from './flying-shuttle/inline-static-env' import { inlineStaticEnv } from './flying-shuttle/inline-static-env'
import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback' import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback'
import { RenderingMode } from './rendering-mode' import { RenderingMode } from './rendering-mode'
import { getParamKeys } from '../server/request/fallback-params'
type Fallback = null | boolean | string type Fallback = null | boolean | string
...@@ -1222,6 +1221,7 @@ export default async function build( ...@@ -1222,6 +1221,7 @@ export default async function build(
config.basePath ? `${config.basePath}${p}` : p config.basePath ? `${config.basePath}${p}` : p
) )
const isAppDynamicIOEnabled = Boolean(config.experimental.dynamicIO)
const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr) const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr)
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST) const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
...@@ -1862,6 +1862,10 @@ export default async function build( ...@@ -1862,6 +1862,10 @@ export default async function build(
const serverPropsPages = new Set<string>() const serverPropsPages = new Set<string>()
const additionalPaths = new Map<string, PrerenderedRoute[]>() const additionalPaths = new Map<string, PrerenderedRoute[]>()
const staticPaths = new Map<string, PrerenderedRoute[]>() const staticPaths = new Map<string, PrerenderedRoute[]>()
const prospectiveRenders = new Map<
string,
{ page: string; originalAppPath: string }
>()
const appNormalizedPaths = new Map<string, string>() const appNormalizedPaths = new Map<string, string>()
const fallbackModes = new Map<string, FallbackMode>() const fallbackModes = new Map<string, FallbackMode>()
const appDefaultConfigs = new Map<string, AppSegmentConfig>() const appDefaultConfigs = new Map<string, AppSegmentConfig>()
...@@ -1946,7 +1950,7 @@ export default async function build( ...@@ -1946,7 +1950,7 @@ export default async function build(
distDir, distDir,
configFileName, configFileName,
runtimeEnvConfig, runtimeEnvConfig,
dynamicIO: Boolean(config.experimental.dynamicIO), dynamicIO: isAppDynamicIOEnabled,
httpAgentOptions: config.httpAgentOptions, httpAgentOptions: config.httpAgentOptions,
locales: config.i18n?.locales, locales: config.i18n?.locales,
defaultLocale: config.i18n?.defaultLocale, defaultLocale: config.i18n?.defaultLocale,
...@@ -2169,7 +2173,7 @@ export default async function build( ...@@ -2169,7 +2173,7 @@ export default async function build(
pageRuntime, pageRuntime,
edgeInfo, edgeInfo,
pageType, pageType,
dynamicIO: Boolean(config.experimental.dynamicIO), dynamicIO: isAppDynamicIOEnabled,
cacheHandler: config.cacheHandler, cacheHandler: config.cacheHandler,
isrFlushToDisk: ciEnvironment.hasNextSupport isrFlushToDisk: ciEnvironment.hasNextSupport
? false ? false
...@@ -2194,6 +2198,8 @@ export default async function build( ...@@ -2194,6 +2198,8 @@ export default async function build(
`Using edge runtime on a page currently disables static generation for that page` `Using edge runtime on a page currently disables static generation for that page`
) )
} else { } else {
const isDynamic = isDynamicRoute(page)
// If this route can be partially pre-rendered, then // If this route can be partially pre-rendered, then
// mark it as such and mark that it can be // mark it as such and mark that it can be
// generated server-side. // generated server-side.
...@@ -2204,6 +2210,18 @@ export default async function build( ...@@ -2204,6 +2210,18 @@ export default async function build(
staticPaths.set(originalAppPath, []) staticPaths.set(originalAppPath, [])
} }
// As PPR isn't enabled for this route, if dynamic IO
// is enabled, and this is a dynamic route, we should
// complete a prospective render for the route so that
// we can use the fallback behavior. This lets us
// check that dynamic pages won't error when they
// enable PPR.
else if (config.experimental.dynamicIO && isDynamic) {
prospectiveRenders.set(originalAppPath, {
page,
originalAppPath,
})
}
if ( if (
workerResult.prerenderedRoutes && workerResult.prerenderedRoutes &&
...@@ -2221,7 +2239,6 @@ export default async function build( ...@@ -2221,7 +2239,6 @@ export default async function build(
const appConfig = workerResult.appConfig || {} const appConfig = workerResult.appConfig || {}
if (appConfig.revalidate !== 0) { if (appConfig.revalidate !== 0) {
const isDynamic = isDynamicRoute(page)
const hasGenerateStaticParams = const hasGenerateStaticParams =
workerResult.prerenderedRoutes && workerResult.prerenderedRoutes &&
workerResult.prerenderedRoutes.length > 0 workerResult.prerenderedRoutes.length > 0
...@@ -2245,7 +2262,7 @@ export default async function build( ...@@ -2245,7 +2262,7 @@ export default async function build(
{ {
path: page, path: page,
encoded: page, encoded: page,
fallbackRouteParams: [], fallbackRouteParams: undefined,
}, },
]) ])
isStatic = true isStatic = true
...@@ -2893,6 +2910,28 @@ export default async function build( ...@@ -2893,6 +2910,28 @@ export default async function build(
}) })
}) })
// If the app does have dynamic IO enabled but does not have PPR
// enabled, then we need to perform a prospective render for all
// the dynamic pages to ensure that they won't error during
// rendering (due to a missing prelude).
for (const {
page,
originalAppPath,
} of prospectiveRenders.values()) {
defaultMap[page] = {
page: originalAppPath,
query: { __nextSsgPath: page },
_fallbackRouteParams: getParamKeys(page),
// Prospective renders are only enabled for app pages.
_isAppDir: true,
// Prospective renders are only enabled when PPR is disabled.
_isRoutePPREnabled: false,
_isProspectiveRender: true,
// Dynamic IO does not currently support `dynamic === 'error'`.
_isDynamicError: false,
}
}
if (i18n) { if (i18n) {
for (const page of [ for (const page of [
...staticPages, ...staticPages,
...@@ -2929,21 +2968,20 @@ export default async function build( ...@@ -2929,21 +2968,20 @@ export default async function build(
}, },
} }
const exportOptions: ExportAppOptions = { const outdir = path.join(distDir, 'export')
const exportResult = await exportApp(
dir,
{
nextConfig: exportConfig, nextConfig: exportConfig,
enabledDirectories, enabledDirectories,
silent: true, silent: true,
buildExport: true, buildExport: true,
debugOutput, debugOutput,
pages: combinedPages, pages: combinedPages,
outdir: path.join(distDir, 'export'), outdir,
statusMessage: 'Generating static pages', statusMessage: 'Generating static pages',
numWorkers: getNumberOfWorkers(exportConfig), numWorkers: getNumberOfWorkers(exportConfig),
} },
const exportResult = await exportApp(
dir,
exportOptions,
nextBuildSpan nextBuildSpan
) )
...@@ -3258,7 +3296,7 @@ export default async function build( ...@@ -3258,7 +3296,7 @@ export default async function build(
.traceChild('move-exported-page') .traceChild('move-exported-page')
.traceAsyncFn(async () => { .traceAsyncFn(async () => {
file = `${file}.${ext}` file = `${file}.${ext}`
const orig = path.join(exportOptions.outdir, file) const orig = path.join(outdir, file)
const pagePath = getPagePath( const pagePath = getPagePath(
originPage, originPage,
distDir, distDir,
...@@ -3344,7 +3382,7 @@ export default async function build( ...@@ -3344,7 +3382,7 @@ export default async function build(
.replace(/\\/g, '/') .replace(/\\/g, '/')
const updatedOrig = path.join( const updatedOrig = path.join(
exportOptions.outdir, outdir,
locale + localeExt, locale + localeExt,
page === '/' ? '' : file page === '/' ? '' : file
) )
...@@ -3570,7 +3608,7 @@ export default async function build( ...@@ -3570,7 +3608,7 @@ export default async function build(
} }
// remove temporary export folder // remove temporary export folder
await fs.rm(exportOptions.outdir, { recursive: true, force: true }) await fs.rm(outdir, { recursive: true, force: true })
await writeManifest(pagesManifestPath, pagesManifest) await writeManifest(pagesManifestPath, pagesManifest)
}) })
} }
......
...@@ -37,6 +37,54 @@ export const enum ExportedAppPageFiles { ...@@ -37,6 +37,54 @@ export const enum ExportedAppPageFiles {
POSTPONED = 'POSTPONED', POSTPONED = 'POSTPONED',
} }
export async function prospectiveRenderAppPage(
req: MockedRequest,
res: MockedResponse,
page: string,
pathname: string,
query: NextParsedUrlQuery,
fallbackRouteParams: FallbackRouteParams | null,
partialRenderOpts: Omit<RenderOpts, keyof RequestLifecycleOpts>
): Promise<undefined> {
const afterRunner = new AfterRunner()
// If the page is `/_not-found`, then we should update the page to be `/404`.
// UNDERSCORE_NOT_FOUND_ROUTE value used here, however we don't want to import it here as it causes constants to be inlined which we don't want here.
if (page === '/_not-found/page') {
pathname = '/404'
}
try {
await lazyRenderAppPage(
new NodeNextRequest(req),
new NodeNextResponse(res),
pathname,
query,
fallbackRouteParams,
{
...partialRenderOpts,
waitUntil: afterRunner.context.waitUntil,
onClose: afterRunner.context.onClose,
onAfterTaskError: afterRunner.context.onTaskError,
}
)
// TODO(after): if we abort a prerender because of an error in an after-callback
// we should probably communicate that better (and not log the error twice)
await afterRunner.executeAfter()
} catch (err) {
if (!isDynamicUsageError(err)) {
throw err
}
// We should fail rendering if a client side rendering bailout
// occurred at the page level.
if (isBailoutToCSRError(err)) {
throw err
}
}
}
/** /**
* Renders & exports a page associated with the /app directory * Renders & exports a page associated with the /app directory
*/ */
......
...@@ -29,7 +29,7 @@ import { createRequestResponseMocks } from '../server/lib/mock-request' ...@@ -29,7 +29,7 @@ import { createRequestResponseMocks } from '../server/lib/mock-request'
import { isAppRouteRoute } from '../lib/is-app-route-route' import { isAppRouteRoute } from '../lib/is-app-route-route'
import { hasNextSupport } from '../server/ci-info' import { hasNextSupport } from '../server/ci-info'
import { exportAppRoute } from './routes/app-route' import { exportAppRoute } from './routes/app-route'
import { exportAppPage } from './routes/app-page' import { exportAppPage, prospectiveRenderAppPage } from './routes/app-page'
import { exportPagesPage } from './routes/pages' import { exportPagesPage } from './routes/pages'
import { getParams } from './helpers/get-params' import { getParams } from './helpers/get-params'
import { createIncrementalCache } from './helpers/create-incremental-cache' import { createIncrementalCache } from './helpers/create-incremental-cache'
...@@ -103,11 +103,14 @@ async function exportPageImpl( ...@@ -103,11 +103,14 @@ async function exportPageImpl(
// the renderOpts. // the renderOpts.
_isRoutePPREnabled: isRoutePPREnabled, _isRoutePPREnabled: isRoutePPREnabled,
// If this is a prospective render, we don't actually want to persist the
// result, we just want to use it to error the build if there's a problem.
_isProspectiveRender: isProspectiveRender = false,
// Pull the original query out. // Pull the original query out.
query: originalQuery = {}, query: originalQuery = {},
} = pathMap } = pathMap
try {
const fallbackRouteParams: FallbackRouteParams | null = const fallbackRouteParams: FallbackRouteParams | null =
getFallbackRouteParams(_fallbackRouteParams) getFallbackRouteParams(_fallbackRouteParams)
...@@ -127,10 +130,7 @@ async function exportPageImpl( ...@@ -127,10 +130,7 @@ async function exportPageImpl(
delete query.__nextLocale delete query.__nextLocale
if (input.renderOpts.locale) { if (input.renderOpts.locale) {
const localePathResult = normalizeLocalePath( const localePathResult = normalizeLocalePath(path, input.renderOpts.locales)
path,
input.renderOpts.locales
)
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
updatedPath = localePathResult.pathname updatedPath = localePathResult.pathname
...@@ -191,8 +191,7 @@ async function exportPageImpl( ...@@ -191,8 +191,7 @@ async function exportPageImpl(
buildExport && buildExport &&
input.renderOpts.domainLocales && input.renderOpts.domainLocales &&
input.renderOpts.domainLocales.some( input.renderOpts.domainLocales.some(
(dl) => (dl) => dl.defaultLocale === locale || dl.locales?.includes(locale || '')
dl.defaultLocale === locale || dl.locales?.includes(locale || '')
) )
) { ) {
addRequestMeta(req, 'isLocaleDomain', true) addRequestMeta(req, 'isLocaleDomain', true)
...@@ -244,7 +243,7 @@ async function exportPageImpl( ...@@ -244,7 +243,7 @@ async function exportPageImpl(
// Handle App Routes. // Handle App Routes.
if (isAppDir && isAppRouteRoute(page)) { if (isAppDir && isAppRouteRoute(page)) {
return await exportAppRoute( return exportAppRoute(
req, req,
res, res,
params, params,
...@@ -280,7 +279,21 @@ async function exportPageImpl( ...@@ -280,7 +279,21 @@ async function exportPageImpl(
// Handle App Pages // Handle App Pages
if (isAppDir) { if (isAppDir) {
return await exportAppPage( // If this is a prospective render, don't return any metrics or revalidate
// timings as we aren't persisting this render (it was only to error).
if (isProspectiveRender) {
return prospectiveRenderAppPage(
req,
res,
page,
pathname,
query,
fallbackRouteParams,
renderOpts
)
}
return exportAppPage(
req, req,
res, res,
page, page,
...@@ -296,7 +309,7 @@ async function exportPageImpl( ...@@ -296,7 +309,7 @@ async function exportPageImpl(
) )
} }
return await exportPagesPage( return exportPagesPage(
req, req,
res, res,
path, path,
...@@ -316,16 +329,6 @@ async function exportPageImpl( ...@@ -316,16 +329,6 @@ async function exportPageImpl(
components, components,
fileWriter fileWriter
) )
} catch (err) {
console.error(
`\nError occurred prerendering page "${path}". Read more: https://nextjs.org/docs/messages/prerender-error\n`
)
if (!isBailoutToCSRError(err)) {
console.error(isError(err) && err.stack ? err.stack : err)
}
return { error: true }
}
} }
export async function exportPages( export async function exportPages(
...@@ -513,8 +516,11 @@ async function exportPage( ...@@ -513,8 +516,11 @@ async function exportPage(
const start = Date.now() const start = Date.now()
const turborepoAccessTraceResult = new TurborepoAccessTraceResult() const turborepoAccessTraceResult = new TurborepoAccessTraceResult()
// Export the page. // Export the page.
const result = await exportPageSpan.traceAsyncFn(() => let result: ExportRouteResult | undefined
try {
result = await exportPageSpan.traceAsyncFn(() =>
turborepoTraceAccess( turborepoTraceAccess(
() => exportPageImpl(input, baseFileWriter), () => exportPageImpl(input, baseFileWriter),
turborepoAccessTraceResult turborepoAccessTraceResult
...@@ -528,6 +534,17 @@ async function exportPage( ...@@ -528,6 +534,17 @@ async function exportPage(
if ('error' in result) { if ('error' in result) {
return { error: result.error, duration: Date.now() - start, files: [] } return { error: result.error, duration: Date.now() - start, files: [] }
} }
} catch (err) {
console.error(
`\nError occurred prerendering page "${input.path}". Read more: https://nextjs.org/docs/messages/prerender-error\n`
)
if (!isBailoutToCSRError(err)) {
console.error(isError(err) && err.stack ? err.stack : err)
}
return { error: true, duration: Date.now() - start, files: [] }
}
// Notify the parent process that we processed a page (used by the progress activity indicator) // Notify the parent process that we processed a page (used by the progress activity indicator)
process.send?.([3, { type: 'activity' }]) process.send?.([3, { type: 'activity' }])
......
...@@ -38,6 +38,7 @@ const zExportMap: zod.ZodType<ExportPathMap> = z.record( ...@@ -38,6 +38,7 @@ const zExportMap: zod.ZodType<ExportPathMap> = z.record(
_isAppDir: z.boolean().optional(), _isAppDir: z.boolean().optional(),
_isDynamicError: z.boolean().optional(), _isDynamicError: z.boolean().optional(),
_isRoutePPREnabled: z.boolean().optional(), _isRoutePPREnabled: z.boolean().optional(),
_isProspectiveRender: z.boolean().optional(),
}) })
) )
......
...@@ -592,6 +592,16 @@ export type ExportPathMap = { ...@@ -592,6 +592,16 @@ export type ExportPathMap = {
* @internal * @internal
*/ */
_isRoutePPREnabled?: boolean _isRoutePPREnabled?: boolean
/**
* When true, it indicates that this page is being rendered in an attempt to
* discover if the page will be safe to generate with PPR. This is only
* enabled when the app has `experimental.dynamicIO` enabled but does not
* have `experimental.ppr` enabled.
*
* @internal
*/
_isProspectiveRender?: boolean
} }
} }
......
import { nextTestSetup } from 'e2e-utils'
describe(`Dynamic IO Prospective Fallback`, () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname + '/fixtures/prospective-fallback',
skipStart: true,
skipDeployment: true,
})
if (skipped) {
return
}
if (isNextDev) {
it('should not error when visiting the page', async () => {
// Start the server, we expect this to succeed.
await next.start()
const res = await next.fetch('/blog/123')
expect(res.status).toBe(200)
})
} else {
it('should error on the build due to a missing suspense boundary', async () => {
try {
await next.start()
} catch {
// we expect the build to fail
}
expect(next.cliOutput).toContain(
'In Route "/blog/[slug]" this component accessed data without a fallback UI available somewhere above it using Suspense.'
)
})
it('should not error when we add the missing suspense boundary', async () => {
await next.patchFile(
'app/blog/[slug]/loading.jsx',
`
export default function Loading() {
return <div>Loading...</div>
}
`
)
// We expect this to succeed.
await next.start()
expect(next.cliOutput).not.toContain(
'In Route "/blog/[slug]" this component accessed data without a fallback UI available somewhere above it using Suspense.'
)
})
}
})
export default async function BlogPage({ params }) {
const { slug } = await params
return <div>Blog: {slug}</div>
}
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
module.exports = {
experimental: {
dynamicIO: true,
},
}
...@@ -31,7 +31,7 @@ export default async function Layout({ children, params }) { ...@@ -31,7 +31,7 @@ export default async function Layout({ children, params }) {
async function Inner({ children, params }) { async function Inner({ children, params }) {
return ( return (
<> <>
<h1>{await params.slug}</h1> <h1>{(await params).slug}</h1>
<section>{children}</section> <section>{children}</section>
</> </>
) )
......
...@@ -93,7 +93,8 @@ ...@@ -93,7 +93,8 @@
"test/e2e/app-dir/app-client-cache/client-cache.defaults.test.ts", "test/e2e/app-dir/app-client-cache/client-cache.defaults.test.ts",
"test/e2e/app-dir/react-max-headers-length/react-max-headers-length.test.ts", "test/e2e/app-dir/react-max-headers-length/react-max-headers-length.test.ts",
"test/e2e/app-dir/front-redirect-issue/front-redirect-issue.test.ts", "test/e2e/app-dir/front-redirect-issue/front-redirect-issue.test.ts",
"test/e2e/app-dir/static-shell-debugging/static-shell-debugging.test.ts" "test/e2e/app-dir/static-shell-debugging/static-shell-debugging.test.ts",
"test/e2e/app-dir/dynamic-io-errors/dynamic-io-errors.prospective-fallback.test.ts"
] ]
} }
} }
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