Unverified Commit 95af2454 authored by Javi Velasco's avatar Javi Velasco Committed by GitHub

Fail on prerendering with dynamic error config (#41707)

This PR adds the ability to fail on pre-rendering for a page configured
with `dynamic="error"`
parent ceb07ff0
...@@ -2094,11 +2094,13 @@ export default async function build( ...@@ -2094,11 +2094,13 @@ export default async function build(
// revalidate periods and dynamicParams settings // revalidate periods and dynamicParams settings
appStaticPaths.forEach((routes, originalAppPath) => { appStaticPaths.forEach((routes, originalAppPath) => {
const encodedRoutes = appStaticPathsEncoded.get(originalAppPath) const encodedRoutes = appStaticPathsEncoded.get(originalAppPath)
const appConfig = appDefaultConfigs.get(originalAppPath) || {}
routes.forEach((route, routeIdx) => { routes.forEach((route, routeIdx) => {
defaultMap[route] = { defaultMap[route] = {
page: originalAppPath, page: originalAppPath,
query: { __nextSsgPath: encodedRoutes?.[routeIdx] }, query: { __nextSsgPath: encodedRoutes?.[routeIdx] },
_isDynamicError: appConfig.dynamic === 'error',
_isAppDir: true, _isAppDir: true,
} }
}) })
......
...@@ -134,6 +134,7 @@ export default async function exportPage({ ...@@ -134,6 +134,7 @@ export default async function exportPage({
const { query: originalQuery = {} } = pathMap const { query: originalQuery = {} } = pathMap
const { page } = pathMap const { page } = pathMap
const isAppDir = (pathMap as any)._isAppDir const isAppDir = (pathMap as any)._isAppDir
const isDynamicError = (pathMap as any)._isDynamicError
const filePath = normalizePagePath(path) const filePath = normalizePagePath(path)
const isDynamic = isDynamicRoute(page) const isDynamic = isDynamicRoute(page)
const ampPath = `${filePath}.amp` const ampPath = `${filePath}.amp`
...@@ -323,6 +324,12 @@ export default async function exportPage({ ...@@ -323,6 +324,12 @@ export default async function exportPage({
const revalidate = (curRenderOpts as any).revalidate const revalidate = (curRenderOpts as any).revalidate
results.fromBuildExportRevalidate = revalidate results.fromBuildExportRevalidate = revalidate
if (isDynamicError) {
throw new Error(
`Page with dynamic = "error" encountered dynamic data method ${path}.`
)
}
if (revalidate !== 0) { if (revalidate !== 0) {
await promises.writeFile(htmlFilepath, html ?? '', 'utf8') await promises.writeFile(htmlFilepath, html ?? '', 'utf8')
await promises.writeFile( await promises.writeFile(
......
export default function Page() {
return <p>loading...</p>
}
import { headers } from 'next/headers'
export const dynamic = 'error'
export default function Page() {
headers()
return (
<>
<p id="page">/dynamic-error</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>
)
}
module.exports = {
experimental: {
appDir: true,
},
}
import { nextBuild } from 'next-test-utils'
import { join } from 'path'
it('throws an error when prerendering a page with config dynamic error', async () => {
const appDir = join(__dirname, '../../app-dynamic-error')
const { stderr, code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(stderr).toContain('Error occurred prerendering page "/dynamic-error"')
expect(code).toBe(1)
})
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