Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
86cb8ec8
Unverified
Commit
86cb8ec8
authored
Apr 06, 2023
by
Steven
Committed by
GitHub
Apr 06, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: handle error case for `output: export` in `next dev` (#47768)
fix NEXT-929
parent
4fbbb626
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
201 additions
and
139 deletions
+201
-139
utils.ts
packages/next/src/build/utils.ts
+0
-11
next-build.ts
packages/next/src/cli/next-build.ts
+2
-0
next-export.ts
packages/next/src/cli/next-export.ts
+2
-2
static-generation-bailout.ts
...s/next/src/client/components/static-generation-bailout.ts
+17
-3
index.ts
packages/next/src/export/index.ts
+1
-1
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+34
-3
module.ts
.../next/src/server/future/route-modules/app-route/module.ts
+3
-1
dynamic-error.test.ts
test/development/acceptance-app/dynamic-error.test.ts
+2
-2
index.test.ts
test/integration/app-dir-export/test/index.test.ts
+140
-116
No files found.
packages/next/src/build/utils.ts
View file @
86cb8ec8
...
...
@@ -1330,7 +1330,6 @@ export async function isPageStatic({
isrFlushToDisk
,
maxMemoryCacheSize
,
incrementalCacheHandlerPath
,
nextConfigOutput
,
}:
{
page
:
string
distDir
:
string
...
...
@@ -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'
)
{
appConfig
.
revalidate
=
0
}
...
...
packages/next/src/cli/next-build.ts
View file @
86cb8ec8
...
...
@@ -86,6 +86,8 @@ const nextBuild: CliCommand = (argv) => {
(
err
.
code
===
'INVALID_RESOLVE_ALIAS'
||
err
.
code
===
'WEBPACK_ERRORS'
||
err
.
code
===
'BUILD_OPTIMIZATION_FAILED'
||
err
.
code
===
'NEXT_EXPORT_ERROR'
||
err
.
code
===
'NEXT_STATIC_GEN_BAILOUT'
||
err
.
code
===
'EDGE_RUNTIME_UNSUPPORTED_API'
)
)
{
printAndExit
(
`>
${
err
.
message
}
`
)
...
...
packages/next/src/cli/next-export.ts
View file @
86cb8ec8
...
...
@@ -72,9 +72,9 @@ const nextExport: CliCommand = (argv) => {
nextExportCliSpan
.
stop
()
printAndExit
(
`Export successful. Files written to
${
options
.
outdir
}
`
,
0
)
})
.
catch
((
err
:
unknown
)
=>
{
.
catch
((
err
:
any
)
=>
{
nextExportCliSpan
.
stop
()
if
(
err
instanceof
ExportError
)
{
if
(
err
instanceof
ExportError
||
err
.
code
===
'NEXT_EXPORT_ERROR'
)
{
Log
.
error
(
err
.
message
)
}
else
{
console
.
error
(
err
)
...
...
packages/next/src/client/components/static-generation-bailout.ts
View file @
86cb8ec8
import
{
DynamicServerError
}
from
'./hooks-server-context'
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
()
if
(
staticGenerationStore
?.
forceStatic
)
{
...
...
@@ -9,8 +21,10 @@ export function staticGenerationBailout(reason: string): boolean | never {
}
if
(
staticGenerationStore
?.
dynamicShouldError
)
{
throw
new
Error
(
`Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`
${
reason
}
\``
const
{
dynamic
=
'error'
,
link
}
=
opts
||
{}
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
}
`
)
}
...
...
packages/next/src/export/index.ts
View file @
86cb8ec8
...
...
@@ -144,7 +144,7 @@ const createProgress = (total: number, label: string) => {
}
export
class
ExportError
extends
Error
{
type
=
'ExportError
'
code
=
'NEXT_EXPORT_ERROR
'
}
export
interface
ExportOptions
{
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
86cb8ec8
...
...
@@ -10,6 +10,7 @@ import type {
Segment
,
}
from
'./types'
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
{
MetadataItems
}
from
'../../lib/metadata/resolve-metadata'
...
...
@@ -166,6 +167,7 @@ export async function renderToHTMLOrFlight(
dev
,
nextFontManifest
,
supportsDynamicHTML
,
nextConfigOutput
,
}
=
renderOpts
const
clientReferenceManifest
=
renderOpts
.
clientReferenceManifest
!
...
...
@@ -217,6 +219,8 @@ export async function renderToHTMLOrFlight(
ComponentMod
.
staticGenerationAsyncStorage
const
requestAsyncStorage
:
RequestAsyncStorage
=
ComponentMod
.
requestAsyncStorage
const
staticGenerationBailout
:
StaticGenerationBailout
=
ComponentMod
.
staticGenerationBailout
// we wrap the render in an AsyncLocalStorage context
const
wrappedRender
=
async
()
=>
{
...
...
@@ -645,15 +649,33 @@ export async function renderToHTMLOrFlight(
?
[
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
// if it's configured above any parent that configured
// otherwise
if
(
layoutOrPageMod
.
dynamic
===
'error'
)
{
if
(
dynamic
===
'error'
)
{
staticGenerationStore
.
dynamicShouldError
=
true
}
else
if
(
dynamic
===
'force-dynamic'
)
{
staticGenerationStore
.
forceDynamic
=
true
staticGenerationBailout
(
`force-dynamic`
,
{
dynamic
})
}
else
{
staticGenerationStore
.
dynamicShouldError
=
false
if
(
layoutOrPageMod
.
dynamic
===
'force-static'
)
{
if
(
dynamic
===
'force-static'
)
{
staticGenerationStore
.
forceStatic
=
true
}
else
{
staticGenerationStore
.
forceStatic
=
false
...
...
@@ -1460,6 +1482,15 @@ export async function renderToHTMLOrFlight(
return
result
}
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
)
{
warn
(
`Entire page
${
pathname
}
deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`
,
...
...
packages/next/src/server/future/route-modules/app-route/module.ts
View file @
86cb8ec8
...
...
@@ -278,7 +278,9 @@ export class AppRouteRouteModule extends RouteModule<
// The dynamic property is set to force-dynamic, so we should
// force the page to be dynamic.
staticGenerationStore
.
forceDynamic
=
true
this
.
staticGenerationBailout
(
`dynamic = 'force-dynamic'`
)
this
.
staticGenerationBailout
(
`force-dynamic`
,
{
dynamic
:
this
.
dynamic
,
})
break
case
'force-static'
:
// The dynamic property is set to force-static, so we should
...
...
test/development/acceptance-app/dynamic-error.test.ts
View file @
86cb8ec8
...
...
@@ -32,8 +32,8 @@ createNextDescribe(
await
session
.
hasRedbox
(
true
)
console
.
log
(
await
session
.
getRedboxDescription
())
expect
(
await
session
.
getRedboxDescription
()).
to
MatchInlineSnapshot
(
`
"Error: Page with \`dynamic = \\"error\\"\` couldn't be rendered statically because it used \`cookies\`"
`
expect
(
await
session
.
getRedboxDescription
()).
to
Be
(
`
Error: Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`.
`
)
await
cleanup
()
...
...
test/integration/app-dir-export/test/index.test.ts
View file @
86cb8ec8
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment