Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on prerendering with dynamic error config #41707

Merged
merged 2 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -2071,11 +2071,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
12 changes: 12 additions & 0 deletions test/e2e/app-dir/app-dynamic-error.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,0 +1,3 @@
export default function Page() {
return <p>loading...</p>
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/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/e2e/app-dir/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/e2e/app-dir/app-dynamic-error/next.config.js
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
appDir: true,
},
}