Skip to content

Commit

Permalink
Fail on prerendering with dynamic error config (#41707)
Browse files Browse the repository at this point in the history
This PR adds the ability to fail on pre-rendering for a page configured
with `dynamic="error"`
  • Loading branch information
javivelasco committed Oct 24, 2022
1 parent ceb07ff commit 95af245
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -2094,11 +2094,13 @@ export default async function build(
// revalidate periods and dynamicParams settings
appStaticPaths.forEach((routes, originalAppPath) => {
const encodedRoutes = appStaticPathsEncoded.get(originalAppPath)
const appConfig = appDefaultConfigs.get(originalAppPath) || {}

routes.forEach((route, routeIdx) => {
defaultMap[route] = {
page: originalAppPath,
query: { __nextSsgPath: encodedRoutes?.[routeIdx] },
_isDynamicError: appConfig.dynamic === 'error',
_isAppDir: true,
}
})
Expand Down
7 changes: 7 additions & 0 deletions packages/next/export/worker.ts
Expand Up @@ -134,6 +134,7 @@ export default async function exportPage({
const { query: originalQuery = {} } = pathMap
const { page } = pathMap
const isAppDir = (pathMap as any)._isAppDir
const isDynamicError = (pathMap as any)._isDynamicError
const filePath = normalizePagePath(path)
const isDynamic = isDynamicRoute(page)
const ampPath = `${filePath}.amp`
Expand Down Expand Up @@ -323,6 +324,12 @@ export default async function exportPage({
const revalidate = (curRenderOpts as any).revalidate
results.fromBuildExportRevalidate = revalidate

if (isDynamicError) {
throw new Error(
`Page with dynamic = "error" encountered dynamic data method ${path}.`
)
}

if (revalidate !== 0) {
await promises.writeFile(htmlFilepath, html ?? '', 'utf8')
await promises.writeFile(
Expand Down
@@ -0,0 +1,3 @@
export default function Page() {
return <p>loading...</p>
}
13 changes: 13 additions & 0 deletions test/integration/app-dynamic-error/app/dynamic-error/page.js
@@ -0,0 +1,13 @@
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>
</>
)
}
10 changes: 10 additions & 0 deletions test/integration/app-dynamic-error/app/layout.js
@@ -0,0 +1,10 @@
export default function Layout({ children }) {
return (
<html lang="en">
<head>
<title>my static blog</title>
</head>
<body>{children}</body>
</html>
)
}
5 changes: 5 additions & 0 deletions test/integration/app-dynamic-error/next.config.js
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
appDir: true,
},
}
12 changes: 12 additions & 0 deletions test/integration/app-dynamic-error/test/index.test.ts
@@ -0,0 +1,12 @@
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)
})

0 comments on commit 95af245

Please sign in to comment.