Unverified Commit 86cb8ec8 authored by Steven's avatar Steven Committed by GitHub

fix: handle error case for `output: export` in `next dev` (#47768)

fix NEXT-929
parent 4fbbb626
...@@ -1330,7 +1330,6 @@ export async function isPageStatic({ ...@@ -1330,7 +1330,6 @@ export async function isPageStatic({
isrFlushToDisk, isrFlushToDisk,
maxMemoryCacheSize, maxMemoryCacheSize,
incrementalCacheHandlerPath, incrementalCacheHandlerPath,
nextConfigOutput,
}: { }: {
page: string page: string
distDir: string distDir: string
...@@ -1498,16 +1497,6 @@ export async function isPageStatic({ ...@@ -1498,16 +1497,6 @@ export async function isPageStatic({
{} {}
) )
if (nextConfigOutput === 'export') {
if (!appConfig.dynamic || appConfig.dynamic === 'auto') {
appConfig.dynamic = 'error'
} else if (appConfig.dynamic === 'force-dynamic') {
throw new Error(
`export const dynamic = "force-dynamic" on page "${page}" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export`
)
}
}
if (appConfig.dynamic === 'force-dynamic') { if (appConfig.dynamic === 'force-dynamic') {
appConfig.revalidate = 0 appConfig.revalidate = 0
} }
......
...@@ -86,6 +86,8 @@ const nextBuild: CliCommand = (argv) => { ...@@ -86,6 +86,8 @@ const nextBuild: CliCommand = (argv) => {
(err.code === 'INVALID_RESOLVE_ALIAS' || (err.code === 'INVALID_RESOLVE_ALIAS' ||
err.code === 'WEBPACK_ERRORS' || err.code === 'WEBPACK_ERRORS' ||
err.code === 'BUILD_OPTIMIZATION_FAILED' || err.code === 'BUILD_OPTIMIZATION_FAILED' ||
err.code === 'NEXT_EXPORT_ERROR' ||
err.code === 'NEXT_STATIC_GEN_BAILOUT' ||
err.code === 'EDGE_RUNTIME_UNSUPPORTED_API') err.code === 'EDGE_RUNTIME_UNSUPPORTED_API')
) { ) {
printAndExit(`> ${err.message}`) printAndExit(`> ${err.message}`)
......
...@@ -72,9 +72,9 @@ const nextExport: CliCommand = (argv) => { ...@@ -72,9 +72,9 @@ const nextExport: CliCommand = (argv) => {
nextExportCliSpan.stop() nextExportCliSpan.stop()
printAndExit(`Export successful. Files written to ${options.outdir}`, 0) printAndExit(`Export successful. Files written to ${options.outdir}`, 0)
}) })
.catch((err: unknown) => { .catch((err: any) => {
nextExportCliSpan.stop() nextExportCliSpan.stop()
if (err instanceof ExportError) { if (err instanceof ExportError || err.code === 'NEXT_EXPORT_ERROR') {
Log.error(err.message) Log.error(err.message)
} else { } else {
console.error(err) console.error(err)
......
import { DynamicServerError } from './hooks-server-context' import { DynamicServerError } from './hooks-server-context'
import { staticGenerationAsyncStorage } from './static-generation-async-storage' import { staticGenerationAsyncStorage } from './static-generation-async-storage'
export function staticGenerationBailout(reason: string): boolean | never { class StaticGenBailoutError extends Error {
code = 'NEXT_STATIC_GEN_BAILOUT'
}
export type StaticGenerationBailout = (
reason: string,
opts?: { dynamic?: string; link?: string }
) => boolean | never
export const staticGenerationBailout: StaticGenerationBailout = (
reason,
opts
) => {
const staticGenerationStore = staticGenerationAsyncStorage.getStore() const staticGenerationStore = staticGenerationAsyncStorage.getStore()
if (staticGenerationStore?.forceStatic) { if (staticGenerationStore?.forceStatic) {
...@@ -9,8 +21,10 @@ export function staticGenerationBailout(reason: string): boolean | never { ...@@ -9,8 +21,10 @@ export function staticGenerationBailout(reason: string): boolean | never {
} }
if (staticGenerationStore?.dynamicShouldError) { if (staticGenerationStore?.dynamicShouldError) {
throw new Error( const { dynamic = 'error', link } = opts || {}
`Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`${reason}\`` const suffix = link ? ` See more info here: ${link}` : ''
throw new StaticGenBailoutError(
`Page with \`dynamic = "${dynamic}"\` couldn't be rendered statically because it used \`${reason}\`.${suffix}`
) )
} }
......
...@@ -144,7 +144,7 @@ const createProgress = (total: number, label: string) => { ...@@ -144,7 +144,7 @@ const createProgress = (total: number, label: string) => {
} }
export class ExportError extends Error { export class ExportError extends Error {
type = 'ExportError' code = 'NEXT_EXPORT_ERROR'
} }
export interface ExportOptions { export interface ExportOptions {
......
...@@ -10,6 +10,7 @@ import type { ...@@ -10,6 +10,7 @@ import type {
Segment, Segment,
} from './types' } from './types'
import type { StaticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage' import type { StaticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage'
import type { StaticGenerationBailout } from '../../client/components/static-generation-bailout'
import type { RequestAsyncStorage } from '../../client/components/request-async-storage' import type { RequestAsyncStorage } from '../../client/components/request-async-storage'
import type { MetadataItems } from '../../lib/metadata/resolve-metadata' import type { MetadataItems } from '../../lib/metadata/resolve-metadata'
...@@ -166,6 +167,7 @@ export async function renderToHTMLOrFlight( ...@@ -166,6 +167,7 @@ export async function renderToHTMLOrFlight(
dev, dev,
nextFontManifest, nextFontManifest,
supportsDynamicHTML, supportsDynamicHTML,
nextConfigOutput,
} = renderOpts } = renderOpts
const clientReferenceManifest = renderOpts.clientReferenceManifest! const clientReferenceManifest = renderOpts.clientReferenceManifest!
...@@ -217,6 +219,8 @@ export async function renderToHTMLOrFlight( ...@@ -217,6 +219,8 @@ export async function renderToHTMLOrFlight(
ComponentMod.staticGenerationAsyncStorage ComponentMod.staticGenerationAsyncStorage
const requestAsyncStorage: RequestAsyncStorage = const requestAsyncStorage: RequestAsyncStorage =
ComponentMod.requestAsyncStorage ComponentMod.requestAsyncStorage
const staticGenerationBailout: StaticGenerationBailout =
ComponentMod.staticGenerationBailout
// we wrap the render in an AsyncLocalStorage context // we wrap the render in an AsyncLocalStorage context
const wrappedRender = async () => { const wrappedRender = async () => {
...@@ -645,15 +649,33 @@ export async function renderToHTMLOrFlight( ...@@ -645,15 +649,33 @@ export async function renderToHTMLOrFlight(
? [DefaultNotFound] ? [DefaultNotFound]
: [] : []
if (typeof layoutOrPageMod?.dynamic === 'string') { let dynamic = layoutOrPageMod?.dynamic
if (nextConfigOutput === 'export') {
if (!dynamic || dynamic === 'auto') {
dynamic = 'error'
} else if (dynamic === 'force-dynamic') {
staticGenerationStore.forceDynamic = true
staticGenerationStore.dynamicShouldError = true
staticGenerationBailout(`output: export`, {
dynamic,
link: 'https://nextjs.org/docs/advanced-features/static-html-export',
})
}
}
if (typeof dynamic === 'string') {
// the nested most config wins so we only force-static // the nested most config wins so we only force-static
// if it's configured above any parent that configured // if it's configured above any parent that configured
// otherwise // otherwise
if (layoutOrPageMod.dynamic === 'error') { if (dynamic === 'error') {
staticGenerationStore.dynamicShouldError = true staticGenerationStore.dynamicShouldError = true
} else if (dynamic === 'force-dynamic') {
staticGenerationStore.forceDynamic = true
staticGenerationBailout(`force-dynamic`, { dynamic })
} else { } else {
staticGenerationStore.dynamicShouldError = false staticGenerationStore.dynamicShouldError = false
if (layoutOrPageMod.dynamic === 'force-static') { if (dynamic === 'force-static') {
staticGenerationStore.forceStatic = true staticGenerationStore.forceStatic = true
} else { } else {
staticGenerationStore.forceStatic = false staticGenerationStore.forceStatic = false
...@@ -1460,6 +1482,15 @@ export async function renderToHTMLOrFlight( ...@@ -1460,6 +1482,15 @@ export async function renderToHTMLOrFlight(
return result return result
} catch (err: any) { } catch (err: any) {
if (
err.code === 'NEXT_STATIC_GEN_BAILOUT' ||
err.message?.includes(
'https://nextjs.org/docs/advanced-features/static-html-export'
)
) {
// Ensure that "next dev" prints the red error overlay
throw err
}
if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) { if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) {
warn( warn(
`Entire page ${pathname} deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`, `Entire page ${pathname} deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`,
......
...@@ -278,7 +278,9 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -278,7 +278,9 @@ export class AppRouteRouteModule extends RouteModule<
// The dynamic property is set to force-dynamic, so we should // The dynamic property is set to force-dynamic, so we should
// force the page to be dynamic. // force the page to be dynamic.
staticGenerationStore.forceDynamic = true staticGenerationStore.forceDynamic = true
this.staticGenerationBailout(`dynamic = 'force-dynamic'`) this.staticGenerationBailout(`force-dynamic`, {
dynamic: this.dynamic,
})
break break
case 'force-static': case 'force-static':
// The dynamic property is set to force-static, so we should // The dynamic property is set to force-static, so we should
......
...@@ -32,8 +32,8 @@ createNextDescribe( ...@@ -32,8 +32,8 @@ createNextDescribe(
await session.hasRedbox(true) await session.hasRedbox(true)
console.log(await session.getRedboxDescription()) console.log(await session.getRedboxDescription())
expect(await session.getRedboxDescription()).toMatchInlineSnapshot( expect(await session.getRedboxDescription()).toBe(
`"Error: Page with \`dynamic = \\"error\\"\` couldn't be rendered statically because it used \`cookies\`"` `Error: Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`.`
) )
await cleanup() await cleanup()
......
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