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
8f6cd5e8
Unverified
Commit
8f6cd5e8
authored
Aug 21, 2025
by
Jiachi Liu
Committed by
GitHub
Aug 21, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Skip emitting pages router entries when only app router present (#82444)
parent
332e73e8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
51 changed files
with
1023 additions
and
211 deletions
+1023
-211
app.rs
crates/next-api/src/app.rs
+1
-0
pages.rs
crates/next-api/src/pages.rs
+102
-38
app_structure.rs
crates/next-core/src/app_structure.rs
+49
-0
pages_structure.rs
crates/next-core/src/pages_structure.rs
+15
-0
entries.ts
packages/next/src/build/entries.ts
+52
-18
index.ts
packages/next/src/build/index.ts
+120
-31
impl.ts
packages/next/src/build/turbopack-build/impl.ts
+13
-8
utils.ts
packages/next/src/build/utils.ts
+32
-5
index.ts
...s/next/src/build/webpack/loaders/next-app-loader/index.ts
+35
-5
index.ts
...t/src/build/webpack/loaders/next-edge-ssr-loader/index.ts
+18
-12
flight-client-entry-plugin.ts
...t/src/build/webpack/plugins/flight-client-entry-plugin.ts
+27
-7
next-typegen.ts
packages/next/src/cli/next-typegen.ts
+1
-0
app-error.tsx
packages/next/src/client/components/builtin/app-error.tsx
+80
-0
handle-isr-error.tsx
packages/next/src/client/components/handle-isr-error.tsx
+3
-1
index.ts
packages/next/src/export/index.ts
+89
-35
app-page.ts
packages/next/src/export/routes/app-page.ts
+14
-2
types.ts
packages/next/src/export/types.ts
+1
-0
base-server.ts
packages/next/src/server/base-server.ts
+17
-1
hot-reloader-webpack.ts
packages/next/src/server/dev/hot-reloader-webpack.ts
+1
-0
route-types-utils.ts
...ges/next/src/server/lib/router-utils/route-types-utils.ts
+16
-3
require.ts
packages/next/src/server/require.ts
+7
-0
constants.ts
packages/next/src/shared/lib/constants.ts
+8
-2
entry-constants.ts
packages/next/src/shared/lib/entry-constants.ts
+4
-0
actions-unrecognized.test.ts
...app-dir/actions-unrecognized/actions-unrecognized.test.ts
+12
-1
cache-components-dynamic-imports.test.ts
...-dynamic-imports/cache-components-dynamic-imports.test.ts
+1
-0
metadata-static-generation.test.ts
...data-static-generation/metadata-static-generation.test.ts
+1
-0
metadata-streaming-static-generation.test.ts
...c-generation/metadata-streaming-static-generation.test.ts
+1
-0
next.config.js
test/e2e/i18n-ignore-rewrite-source-locale/next.config.js
+25
-0
hello.js
.../e2e/i18n-ignore-rewrite-source-locale/pages/api/hello.js
+3
-0
file.txt
test/e2e/i18n-ignore-rewrite-source-locale/public/file.txt
+1
-0
rewrites-with-basepath.test.ts
...nore-rewrite-source-locale/rewrites-with-basepath.test.ts
+8
-41
utils.ts
test/integration/app-dir-export/test/utils.ts
+4
-0
tsconfig.json
test/integration/next-dynamic-css/tsconfig.json
+1
-1
app-router-only.test.ts
...oduction/500-page/app-router-only/app-router-only.test.ts
+64
-0
actions.ts
test/production/500-page/app-router-only/app/actions.ts
+5
-0
global-error.tsx
.../production/500-page/app-router-only/app/global-error.tsx
+11
-0
layout.tsx
test/production/500-page/app-router-only/app/layout.tsx
+11
-0
page.tsx
test/production/500-page/app-router-only/app/page.tsx
+3
-0
route.tsx
...uction/500-page/app-router-only/app/route-error/route.tsx
+6
-0
middleware.ts
test/production/500-page/app-router-only/middleware.ts
+8
-0
page.tsx
.../mixed-router-no-custom-pages-error/app/app-page/page.tsx
+3
-0
global-error.tsx
...e/mixed-router-no-custom-pages-error/app/global-error.tsx
+11
-0
layout.tsx
...00-page/mixed-router-no-custom-pages-error/app/layout.tsx
+11
-0
mixed-router-no-error.test.ts
...outer-no-custom-pages-error/mixed-router-no-error.test.ts
+47
-0
pages-error.tsx
.../mixed-router-no-custom-pages-error/pages/pages-error.tsx
+10
-0
page.tsx
...ixed-router-with-custom-pages-error/app/app-page/page.tsx
+3
-0
global-error.tsx
...mixed-router-with-custom-pages-error/app/global-error.tsx
+17
-0
layout.tsx
...-page/mixed-router-with-custom-pages-error/app/layout.tsx
+11
-0
mixed-router-with-error.test.ts
...r-with-custom-pages-error/mixed-router-with-error.test.ts
+27
-0
_error.tsx
...age/mixed-router-with-custom-pages-error/pages/_error.tsx
+3
-0
pages-error.tsx
...ixed-router-with-custom-pages-error/pages/pages-error.tsx
+10
-0
No files found.
crates/next-api/src/app.rs
View file @
8f6cd5e8
...
...
@@ -171,6 +171,7 @@ impl AppProject {
self
.app_dir
.clone
(),
conf
.page_extensions
(),
conf
.is_global_not_found_enabled
(),
self
.project
.next_mode
(),
)
}
...
...
crates/next-api/src/pages.rs
View file @
8f6cd5e8
This diff is collapsed.
Click to expand it.
crates/next-core/src/app_structure.rs
View file @
8f6cd5e8
...
...
@@ -16,6 +16,7 @@ use turbopack_core::issue::{
};
use
crate
::{
mode
::
NextMode
,
next_app
::{
AppPage
,
AppPath
,
PageSegment
,
PageType
,
metadata
::{
...
...
@@ -753,12 +754,14 @@ pub fn get_entrypoints(
app_dir
:
FileSystemPath
,
page_extensions
:
Vc
<
Vec
<
RcStr
>>
,
is_global_not_found_enabled
:
Vc
<
bool
>
,
next_mode
:
Vc
<
NextMode
>
,
)
->
Vc
<
Entrypoints
>
{
directory_tree_to_entrypoints
(
app_dir
.clone
(),
get_directory_tree
(
app_dir
.clone
(),
page_extensions
),
get_global_metadata
(
app_dir
,
page_extensions
),
is_global_not_found_enabled
,
next_mode
,
Default
::
default
(),
Default
::
default
(),
)
...
...
@@ -786,6 +789,7 @@ fn directory_tree_to_entrypoints(
directory_tree
:
Vc
<
DirectoryTree
>
,
global_metadata
:
Vc
<
GlobalMetadata
>
,
is_global_not_found_enabled
:
Vc
<
bool
>
,
next_mode
:
Vc
<
NextMode
>
,
root_layouts
:
Vc
<
FileSystemPathVec
>
,
root_params
:
Vc
<
RootParamVecOption
>
,
)
->
Vc
<
Entrypoints
>
{
...
...
@@ -793,6 +797,7 @@ fn directory_tree_to_entrypoints(
app_dir
,
global_metadata
,
is_global_not_found_enabled
,
next_mode
,
rcstr!
(
""
),
directory_tree
,
AppPage
::
new
(),
...
...
@@ -1247,6 +1252,7 @@ async fn directory_tree_to_entrypoints_internal(
app_dir
:
FileSystemPath
,
global_metadata
:
ResolvedVc
<
GlobalMetadata
>
,
is_global_not_found_enabled
:
Vc
<
bool
>
,
next_mode
:
Vc
<
NextMode
>
,
directory_name
:
RcStr
,
directory_tree
:
Vc
<
DirectoryTree
>
,
app_page
:
AppPage
,
...
...
@@ -1258,6 +1264,7 @@ async fn directory_tree_to_entrypoints_internal(
app_dir
,
global_metadata
,
is_global_not_found_enabled
,
next_mode
,
directory_name
,
directory_tree
,
app_page
,
...
...
@@ -1272,6 +1279,7 @@ async fn directory_tree_to_entrypoints_internal_untraced(
app_dir
:
FileSystemPath
,
global_metadata
:
ResolvedVc
<
GlobalMetadata
>
,
is_global_not_found_enabled
:
Vc
<
bool
>
,
next_mode
:
Vc
<
NextMode
>
,
directory_name
:
RcStr
,
directory_tree
:
Vc
<
DirectoryTree
>
,
app_page
:
AppPage
,
...
...
@@ -1518,6 +1526,46 @@ async fn directory_tree_to_entrypoints_internal_untraced(
root_params
,
);
}
// Create production global error page only in build mode
// This aligns with webpack: default Pages entries (including /_error) are only added when
// the build isn't app-only. If the build is app-only (no user pages/api), we should still
// expose the app global error so runtime errors render, but we shouldn't emit it otherwise.
if
matches!
(
*
next_mode
.await
?
,
NextMode
::
Build
)
{
// Use built-in global-error.js to create a `_global-error/page` route.
let
global_error_tree
=
AppPageLoaderTree
{
page
:
app_page
.clone
(),
segment
:
directory_name
.clone
(),
parallel_routes
:
fxindexmap!
{
rcstr!
(
"children"
)
=>
AppPageLoaderTree
{
page
:
app_page
.clone
(),
segment
:
rcstr!
(
"__PAGE__"
),
parallel_routes
:
FxIndexMap
::
default
(),
modules
:
AppDirModules
{
page
:
Some
(
get_next_package
(
app_dir
.clone
())
.await
?
.join
(
"dist/client/components/builtin/app-error.js"
)
?
),
..
Default
::
default
()
},
global_metadata
,
}
},
modules
:
AppDirModules
::
default
(),
global_metadata
,
}
.resolved_cell
();
let
app_global_error_page
=
app_page
.clone_push_str
(
"_global-error"
)
?
.complete
(
PageType
::
Page
)
?
;
add_app_page
(
app_dir
.clone
(),
&
mut
result
,
app_global_error_page
,
global_error_tree
,
root_params
,
);
}
}
let
app_page
=
&
app_page
;
...
...
@@ -1542,6 +1590,7 @@ async fn directory_tree_to_entrypoints_internal_untraced(
app_dir
.clone
(),
*
global_metadata
,
is_global_not_found_enabled
,
next_mode
,
subdir_name
.clone
(),
*
subdirectory
,
child_app_page
.clone
(),
...
...
crates/next-core/src/pages_structure.rs
View file @
8f6cd5e8
...
...
@@ -76,6 +76,8 @@ pub struct PagesStructure {
pub
error_500
:
Option
<
ResolvedVc
<
PagesStructureItem
>>
,
pub
api
:
Option
<
ResolvedVc
<
PagesDirectoryStructure
>>
,
pub
pages
:
Option
<
ResolvedVc
<
PagesDirectoryStructure
>>
,
pub
has_user_pages
:
bool
,
pub
should_create_pages_entries
:
bool
,
}
#[turbo_tasks
::
value]
...
...
@@ -102,6 +104,7 @@ pub async fn find_pages_structure(
project_root
:
FileSystemPath
,
next_router_root
:
FileSystemPath
,
page_extensions
:
Vc
<
Vec
<
RcStr
>>
,
next_mode
:
Vc
<
crate
::
mode
::
NextMode
>
,
)
->
Result
<
Vc
<
PagesStructure
>>
{
let
pages_root
=
project_root
.join
(
"pages"
)
?
.realpath
()
.owned
()
.await
?
;
let
pages_root
=
if
*
pages_root
.get_type
()
.await
?
==
FileSystemEntryType
::
Directory
{
...
...
@@ -123,6 +126,7 @@ pub async fn find_pages_structure(
Vc
::
cell
(
pages_root
),
next_router_root
,
page_extensions
,
next_mode
,
))
}
...
...
@@ -133,6 +137,7 @@ async fn get_pages_structure_for_root_directory(
project_path
:
Vc
<
FileSystemPathOption
>
,
next_router_path
:
FileSystemPath
,
page_extensions
:
Vc
<
Vec
<
RcStr
>>
,
next_mode
:
Vc
<
crate
::
mode
::
NextMode
>
,
)
->
Result
<
Vc
<
PagesStructure
>>
{
let
page_extensions_raw
=
&*
page_extensions
.await
?
;
...
...
@@ -259,6 +264,14 @@ async fn get_pages_structure_for_root_directory(
project_root
.join
(
"pages"
)
?
};
// Check if there are any actual user pages (not just _app, _document, _error)
// error_500_item can be auto-generated for app router, so only count it if there are other user
// pages
let
has_user_pages
=
pages_directory
.is_some
()
||
api_directory
.is_some
();
// Only skip user pages routes during build mode when there are no user pages
let
should_create_pages_entries
=
has_user_pages
||
next_mode
.await
?
.is_development
();
let
app_item
=
{
let
app_router_path
=
next_router_path
.join
(
"_app"
)
?
;
PagesStructureItem
::
new
(
...
...
@@ -311,6 +324,8 @@ async fn get_pages_structure_for_root_directory(
error_500
:
error_500_item
.to_resolved
()
.await
?
,
api
:
api_directory
,
pages
:
pages_directory
,
has_user_pages
,
should_create_pages_entries
,
}
.cell
())
}
...
...
packages/next/src/build/entries.ts
View file @
8f6cd5e8
...
...
@@ -27,7 +27,7 @@ import { isEdgeRuntime } from '../lib/is-edge-runtime'
import
{
APP_CLIENT_INTERNALS
,
RSC_MODULE_TYPES
,
UNDERSCORE_NOT_FOUND_ROUTE
_ENTRY
,
UNDERSCORE_NOT_FOUND_ROUTE
,
}
from
'../shared/lib/constants'
import
{
CLIENT_STATIC_FILES_RUNTIME_AMP
,
...
...
@@ -46,6 +46,7 @@ import {
isInstrumentationHookFile
,
isInstrumentationHookFilename
,
reduceAppConfig
,
isAppBuiltinPage
,
}
from
'./utils'
import
{
getAppPageStaticInfo
,
...
...
@@ -80,6 +81,11 @@ import type { createValidFileMatcher } from '../server/lib/find-page-file'
import
{
isReservedPage
}
from
'./utils'
import
{
isParallelRouteSegment
}
from
'../shared/lib/segment'
import
{
ensureLeadingSlash
}
from
'../shared/lib/page-path/ensure-leading-slash'
import
{
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
UNDERSCORE_GLOBAL_ERROR_ROUTE
,
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
,
}
from
'../shared/lib/entry-constants'
/**
* Collect app pages, layouts, and default files from the app directory
...
...
@@ -225,10 +231,15 @@ export function extractSlotsFromAppRoutes(mappedAppPages: {
}):
SlotInfo
[]
{
const
slots
:
SlotInfo
[]
=
[]
for
(
const
[
route
]
of
Object
.
entries
(
mappedAppPages
))
{
if
(
route
===
'/_not-found/page'
)
continue
for
(
const
[
page
]
of
Object
.
entries
(
mappedAppPages
))
{
if
(
page
===
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
||
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
)
{
continue
}
const
segments
=
rout
e
.
split
(
'/'
)
const
segments
=
pag
e
.
split
(
'/'
)
for
(
let
i
=
segments
.
length
-
1
;
i
>=
0
;
i
--
)
{
const
segment
=
segments
[
i
]
if
(
isParallelRouteSegment
(
segment
))
{
...
...
@@ -327,8 +338,13 @@ export function processAppRoutes(
const
appRoutes
:
RouteInfo
[]
=
[]
const
appRouteHandlers
:
RouteInfo
[]
=
[]
for
(
const
[
route
,
filePath
]
of
Object
.
entries
(
mappedAppPages
))
{
if
(
route
===
'/_not-found/page'
)
continue
for
(
const
[
page
,
filePath
]
of
Object
.
entries
(
mappedAppPages
))
{
if
(
page
===
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
||
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
)
{
continue
}
const
relativeFilePath
=
createRelativeFilePath
(
baseDir
,
...
...
@@ -339,12 +355,12 @@ export function processAppRoutes(
if
(
validFileMatcher
.
isAppRouterRoute
(
filePath
))
{
appRouteHandlers
.
push
({
route
:
normalizeAppPath
(
normalizePathSep
(
rout
e
)),
route
:
normalizeAppPath
(
normalizePathSep
(
pag
e
)),
filePath
:
relativeFilePath
,
})
}
else
{
appRoutes
.
push
({
route
:
normalizeAppPath
(
normalizePathSep
(
rout
e
)),
route
:
normalizeAppPath
(
normalizePathSep
(
pag
e
)),
filePath
:
relativeFilePath
,
})
}
...
...
@@ -439,10 +455,15 @@ export async function getStaticInfoIncludingLayouts({
return
pageStaticInfo
}
// Skip inheritance for global-error pages - always use default config
if
(
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
)
{
return
pageStaticInfo
}
const
segments
=
[
pageStaticInfo
]
// inherit from layout files only if it's a page route
if
(
isAppPageRoute
(
page
))
{
// inherit from layout files only if it's a page route
and not a builtin page
if
(
isAppPageRoute
(
page
)
&&
!
isAppBuiltinPage
(
pageFilePath
)
)
{
const
layoutFiles
=
[]
const
potentialLayoutFiles
=
pageExtensions
.
map
((
ext
)
=>
'layout.'
+
ext
)
let
dir
=
dirname
(
pageFilePath
)
...
...
@@ -539,6 +560,7 @@ export async function createPagesMapping({
pagesType
,
pagesDir
,
appDir
,
appDirOnly
,
}:
{
isDev
:
boolean
pageExtensions
:
PageExtensions
...
...
@@ -546,6 +568,7 @@ export async function createPagesMapping({
pagesType
:
PAGE_TYPES
pagesDir
:
string
|
undefined
appDir
:
string
|
undefined
appDirOnly
:
boolean
}):
Promise
<
MappedPages
>
{
const
isAppRoute
=
pagesType
===
'app'
const
pages
:
MappedPages
=
{}
...
...
@@ -558,9 +581,12 @@ export async function createPagesMapping({
let
pageKey
=
getPageFromPath
(
pagePath
,
pageExtensions
)
if
(
isAppRoute
)
{
pageKey
=
pageKey
.
replace
(
/%5F/g
,
'_'
)
if
(
pageKey
===
'/not-found'
)
{
if
(
pageKey
===
UNDERSCORE_NOT_FOUND_ROUTE
)
{
pageKey
=
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
}
if
(
pageKey
===
UNDERSCORE_GLOBAL_ERROR_ROUTE
)
{
pageKey
=
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
}
}
const
normalizedPath
=
normalizePathSep
(
...
...
@@ -605,9 +631,9 @@ export async function createPagesMapping({
return
pages
}
case
PAGE_TYPES
.
APP
:
{
const
hasAppPages
=
Object
.
keys
(
pages
).
some
((
page
)
=>
page
.
endsWith
(
'/page'
)
)
const
hasAppPages
=
Object
.
keys
(
pages
).
length
>
0
// Whether to emit App router 500.html entry, which only presents in production and only app router presents
const
hasAppGlobalError
=
!
isDev
&&
appDirOnly
return
{
// If there's any app pages existed, add a default /_not-found route as 404.
// If there's any custom /_not-found page, it will override the default one.
...
...
@@ -616,6 +642,11 @@ export async function createPagesMapping({
'next/dist/client/components/builtin/global-not-found'
),
}),
...(
hasAppGlobalError
&&
{
[
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
]:
require
.
resolve
(
'next/dist/client/components/builtin/app-error'
),
}),
...
pages
,
}
}
...
...
@@ -632,10 +663,13 @@ export async function createPagesMapping({
const
root
=
isDev
&&
pagesDir
?
PAGES_DIR_ALIAS
:
'next/dist/pages'
return
{
'/_app'
:
`
${
root
}
/_app`
,
'/_error'
:
`
${
root
}
/_error`
,
'/_document'
:
`
${
root
}
/_document`
,
...
pages
,
// Don't add default pages entries if this is an app-router-only build
...((
isDev
||
!
appDirOnly
)
&&
{
'/_app'
:
`
${
root
}
/_app`
,
'/_error'
:
`
${
root
}
/_error`
,
'/_document'
:
`
${
root
}
/_document`
,
...
pages
,
}),
}
}
default
:
{
...
...
packages/next/src/build/index.ts
View file @
8f6cd5e8
This diff is collapsed.
Click to expand it.
packages/next/src/build/turbopack-build/impl.ts
View file @
8f6cd5e8
...
...
@@ -179,14 +179,19 @@ export async function turbopackBuild(): Promise<{
await
Promise
.
all
(
promises
)
await
Promise
.
all
([
manifestLoader
.
loadBuildManifest
(
'_app'
),
manifestLoader
.
loadPagesManifest
(
'_app'
),
manifestLoader
.
loadFontManifest
(
'_app'
),
manifestLoader
.
loadPagesManifest
(
'_document'
),
manifestLoader
.
loadClientBuildManifest
(
'_error'
),
manifestLoader
.
loadBuildManifest
(
'_error'
),
manifestLoader
.
loadPagesManifest
(
'_error'
),
manifestLoader
.
loadFontManifest
(
'_error'
),
// Only load pages router manifests if not app-only
...(
!
appDirOnly
?
[
manifestLoader
.
loadBuildManifest
(
'_app'
),
manifestLoader
.
loadPagesManifest
(
'_app'
),
manifestLoader
.
loadFontManifest
(
'_app'
),
manifestLoader
.
loadPagesManifest
(
'_document'
),
manifestLoader
.
loadClientBuildManifest
(
'_error'
),
manifestLoader
.
loadBuildManifest
(
'_error'
),
manifestLoader
.
loadPagesManifest
(
'_error'
),
manifestLoader
.
loadFontManifest
(
'_error'
),
]
:
[]),
entrypoints
.
instrumentation
&&
manifestLoader
.
loadMiddlewareManifest
(
'instrumentation'
,
...
...
packages/next/src/build/utils.ts
View file @
8f6cd5e8
...
...
@@ -49,6 +49,8 @@ import {
}
from
'../lib/constants'
import
{
MODERN_BROWSERSLIST_TARGET
,
UNDERSCORE_GLOBAL_ERROR_ROUTE
,
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
,
UNDERSCORE_NOT_FOUND_ROUTE
,
}
from
'../shared/lib/constants'
import
prettyBytes
from
'../lib/pretty-bytes'
...
...
@@ -318,8 +320,13 @@ const filterAndSortList = (
)
=>
{
let
pages
:
string
[]
if
(
routeType
===
'app'
)
{
// filter out static app route of /favicon.ico
pages
=
list
.
filter
((
e
)
=>
e
!==
'/favicon.ico'
)
// filter out static app route of /favicon.ico and /_global-error
pages
=
list
.
filter
((
e
)
=>
{
if
(
e
===
'/favicon.ico'
)
return
false
// Hide static /_global-error from build output
if
(
e
===
'/_global-error'
)
return
false
return
true
})
}
else
{
// filter built-in pages
pages
=
list
...
...
@@ -1067,6 +1074,23 @@ export async function isPageStatic({
buildId
:
string
sriEnabled
:
boolean
}):
Promise
<
PageIsStaticResult
>
{
// Skip page data collection for synthetic _global-error routes
if
(
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE
)
{
return
{
isStatic
:
true
,
isRoutePPREnabled
:
false
,
isHybridAmp
:
false
,
isAmpOnly
:
false
,
prerenderFallbackMode
:
undefined
,
prerenderedRoutes
:
undefined
,
rootParamKeys
:
undefined
,
hasStaticProps
:
false
,
hasServerProps
:
false
,
isNextImageImported
:
false
,
appConfig
:
{},
}
}
await
createIncrementalCache
({
cacheHandler
,
cacheHandlers
,
...
...
@@ -1159,7 +1183,10 @@ export async function isPageStatic({
})
}
appConfig
=
reduceAppConfig
(
segments
)
appConfig
=
originalAppPath
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
?
{}
:
reduceAppConfig
(
segments
)
if
(
appConfig
.
dynamic
===
'force-static'
&&
pathIsEdgeRuntime
)
{
Log
.
warn
(
...
...
@@ -1755,8 +1782,8 @@ export function isReservedPage(page: string) {
return
RESERVED_PAGE
.
test
(
page
)
}
export
function
isAppBuiltin
NotFound
Page
(
page
:
string
)
{
return
/next
[\\/]
dist
[\\/]
client
[\\/]
components
[\\/]
builtin
[\\/](
not-found|global-not-found
)
/
.
test
(
export
function
isAppBuiltinPage
(
page
:
string
)
{
return
/next
[\\/]
dist
[\\/]
(
esm
[\\/])?
client
[\\/]
components
[\\/]
builtin
[\\/]
/
.
test
(
page
)
}
...
...
packages/next/src/build/webpack/loaders/next-app-loader/index.ts
View file @
8f6cd5e8
import
type
webpack
from
'next/dist/compiled/webpack/webpack'
import
{
UNDERSCORE_GLOBAL_ERROR_ROUTE
,
UNDERSCORE_NOT_FOUND_ROUTE
,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
type
ValueOf
,
}
from
'../../../../shared/lib/constants'
import
{
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
}
from
'../../../../shared/lib/entry-constants'
import
type
{
ModuleTuple
,
CollectedMetadata
}
from
'../metadata/types'
import
path
from
'path'
...
...
@@ -21,7 +25,7 @@ import { isAppRouteRoute } from '../../../../lib/is-app-route-route'
import
type
{
NextConfig
}
from
'../../../../server/config-shared'
import
{
AppPathnameNormalizer
}
from
'../../../../server/normalizers/built/app/app-pathname-normalizer'
import
type
{
MiddlewareConfig
}
from
'../../../analysis/get-page-static-info'
import
{
isAppBuiltin
NotFound
Page
}
from
'../../../utils'
import
{
isAppBuiltinPage
}
from
'../../../utils'
import
{
loadEntrypoint
}
from
'../../../load-entrypoint'
import
{
isGroupSegment
,
...
...
@@ -85,6 +89,7 @@ const defaultNotFoundPath = 'next/dist/client/components/builtin/not-found.js'
const
defaultLayoutPath
=
'next/dist/client/components/builtin/layout.js'
const
defaultGlobalNotFoundPath
=
'next/dist/client/components/builtin/global-not-found.js'
const
appErrorPath
=
'next/dist/client/components/builtin/app-error.js'
type
DirResolver
=
(
pathToResolve
:
string
)
=>
string
type
PathResolver
=
(
...
...
@@ -153,7 +158,8 @@ async function createTreeCodeFromPath(
}
>
{
const
splittedPath
=
pagePath
.
split
(
/
[\\/]
/
,
1
)
const
isNotFoundRoute
=
page
===
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
const
isDefaultNotFound
=
isAppBuiltinNotFoundPage
(
pagePath
)
const
isAppErrorRoute
=
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
const
isDefaultNotFound
=
isAppBuiltinPage
(
pagePath
)
const
appDirPrefix
=
isDefaultNotFound
?
APP_DIR_ALIAS
:
splittedPath
[
0
]
const
pages
:
string
[]
=
[]
...
...
@@ -439,7 +445,7 @@ async function createTreeCodeFromPath(
const
varName
=
`notFound
${
nestedCollectedDeclarations
.
length
}
`
nestedCollectedDeclarations
.
push
([
varName
,
notFoundPath
])
subtreeCode
=
`{
children: [
${
JSON
.
stringify
(
UNDERSCORE_NOT_FOUND_ROUTE
)}
, {
children: [
${
JSON
.
stringify
(
UNDERSCORE_NOT_FOUND_ROUTE
.
slice
(
1
)
)}
, {
children: ['
${
PAGE_SEGMENT_KEY
}
', {}, {
page: [
${
varName
}
,
...
...
@@ -452,6 +458,22 @@ async function createTreeCodeFromPath(
}
}
// If it's app-error route, set app-error as children page
if
(
isAppErrorRoute
)
{
const
varName
=
`appError
${
nestedCollectedDeclarations
.
length
}
`
nestedCollectedDeclarations
.
push
([
varName
,
appErrorPath
])
subtreeCode
=
`{
children: [
${
JSON
.
stringify
(
UNDERSCORE_GLOBAL_ERROR_ROUTE
.
slice
(
1
))}
, {
children: ['
${
PAGE_SEGMENT_KEY
}
', {}, {
page: [
${
varName
}
,
${
JSON
.
stringify
(
appErrorPath
)}
]
}]
}, {}]
}`
}
// For 404 route
// if global-not-found is in definedFilePaths, remove root layout for /_not-found
// TODO: remove this once global-not-found is stable.
...
...
@@ -461,6 +483,12 @@ async function createTreeCodeFromPath(
)
}
if
(
isAppErrorRoute
)
{
definedFilePaths
=
definedFilePaths
.
filter
(
([
type
])
=>
type
!==
'layout'
)
}
const
modulesCode
=
`{
${
definedFilePaths
.
map
(([
file
,
filePath
])
=>
{
...
...
@@ -780,7 +808,9 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
!!
treeCodeResult
.
globalNotFound
&&
isGlobalNotFoundEnabled
if
(
!
treeCodeResult
.
rootLayout
&&
!
isGlobalNotFoundPath
)
{
const
isAppErrorRoute
=
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
if
(
!
treeCodeResult
.
rootLayout
&&
!
isGlobalNotFoundPath
&&
!
isAppErrorRoute
)
{
if
(
!
isDev
)
{
// If we're building and missing a root layout, exit the build
Log
.
error
(
...
...
packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts
View file @
8f6cd5e8
...
...
@@ -121,18 +121,24 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
this
.
context
||
this
.
rootContext
,
absolutePagePath
)
const
appPath
=
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
swapDistFolderWithEsmDistFolder
(
absoluteAppPath
)
)
const
errorPath
=
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
swapDistFolderWithEsmDistFolder
(
absoluteErrorPath
)
)
const
documentPath
=
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
swapDistFolderWithEsmDistFolder
(
absoluteDocumentPath
)
)
const
appPath
=
absoluteAppPath
?
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
swapDistFolderWithEsmDistFolder
(
absoluteAppPath
)
)
:
''
const
errorPath
=
absoluteErrorPath
?
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
swapDistFolderWithEsmDistFolder
(
absoluteErrorPath
)
)
:
''
const
documentPath
=
absoluteDocumentPath
?
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
swapDistFolderWithEsmDistFolder
(
absoluteDocumentPath
)
)
:
''
const
userland500Path
=
absolute500Path
?
this
.
utils
.
contextify
(
this
.
context
||
this
.
rootContext
,
...
...
packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts
View file @
8f6cd5e8
...
...
@@ -24,8 +24,11 @@ import {
DEFAULT_RUNTIME_WEBPACK
,
EDGE_RUNTIME_WEBPACK
,
SERVER_REFERENCE_MANIFEST
,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
}
from
'../../../shared/lib/constants'
import
{
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
,
}
from
'../../../shared/lib/entry-constants'
import
{
isClientComponentEntryModule
,
isCSSMod
,
...
...
@@ -50,6 +53,7 @@ import {
import
type
{
MetadataRouteLoaderOptions
}
from
'../loaders/next-metadata-route-loader'
import
type
{
FlightActionEntryLoaderActions
}
from
'../loaders/next-flight-action-entry-loader'
import
getWebpackBundler
from
'../../../shared/lib/get-webpack-bundler'
import
{
isAppBuiltinPage
}
from
'../../utils'
interface
Options
{
dev
:
boolean
...
...
@@ -325,10 +329,11 @@ export class FlightClientEntryPlugin {
const
clientEntriesToInject
=
[]
const
mergedCSSimports
:
CssImports
=
{}
for
(
const
connection
of
getModuleReferencesInOrder
(
const
moduleReferences
=
getModuleReferencesInOrder
(
entryModule
,
compilation
.
moduleGraph
))
{
)
for
(
const
connection
of
moduleReferences
)
{
// Entry can be any user defined entry files such as layout, page, error, loading, etc.
let
entryRequest
=
(
connection
.
dependency
as
unknown
as
webpack
.
NormalModule
...
...
@@ -355,13 +360,16 @@ export class FlightClientEntryPlugin {
)
const
isAbsoluteRequest
=
path
.
isAbsolute
(
entryRequest
)
const
isAppRouterBuiltinPage
=
isAppBuiltinPage
(
entryRequest
)
// Next.js internals are put into a separate entry.
if
(
!
isAbsoluteRequest
)
{
Object
.
keys
(
clientComponentImports
).
forEach
(
(
value
)
=>
(
internalClientComponentEntryImports
[
value
]
=
new
Set
())
)
continue
if
(
!
isAppRouterBuiltinPage
)
{
continue
}
}
// TODO-APP: Enable these lines. This ensures no entrypoint is created for layout/page when there are no client components.
...
...
@@ -370,9 +378,10 @@ export class FlightClientEntryPlugin {
// continue
// }
const
relativeRequest
=
isAbsoluteRequest
?
path
.
relative
(
compilation
.
options
.
context
!
,
entryRequest
)
:
entryRequest
const
relativeRequest
=
isAbsoluteRequest
&&
!
isAppRouterBuiltinPage
?
path
.
relative
(
compilation
.
options
.
context
!
,
entryRequest
)
:
entryRequest
// Replace file suffix as `.js` will be added.
// bundlePath will have app/ prefix but not src/.
...
...
@@ -439,6 +448,17 @@ export class FlightClientEntryPlugin {
absolutePagePath
:
entryRequest
,
})
}
if
(
name
===
`app
${
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
}
`
)
{
clientEntriesToInject
.
push
({
compiler
,
compilation
,
entryName
:
name
,
clientComponentImports
,
bundlePath
:
`app
${
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
}
`
,
absolutePagePath
:
entryRequest
,
})
}
}
// Make sure CSS imports are deduplicated before injecting the client entry
...
...
packages/next/src/cli/next-typegen.ts
View file @
8f6cd5e8
...
...
@@ -89,6 +89,7 @@ const nextTypegen = async (
pageExtensions
:
nextConfig
.
pageExtensions
,
pagesDir
,
appDir
,
appDirOnly
:
!!
appDir
&&
!
pagesDir
,
})
const
validFileMatcher
=
createValidFileMatcher
(
...
...
packages/next/src/client/components/builtin/app-error.tsx
0 → 100644
View file @
8f6cd5e8
import
React
from
'react'
const
styles
:
Record
<
string
,
React
.
CSSProperties
>
=
{
error
:
{
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52
fontFamily
:
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"'
,
height
:
'100vh'
,
textAlign
:
'center'
,
display
:
'flex'
,
flexDirection
:
'column'
,
alignItems
:
'center'
,
justifyContent
:
'center'
,
},
desc
:
{
lineHeight
:
'48px'
,
},
h1
:
{
display
:
'inline-block'
,
margin
:
'0 20px 0 0'
,
paddingRight
:
23
,
fontSize
:
24
,
fontWeight
:
500
,
verticalAlign
:
'top'
,
},
h2
:
{
fontSize
:
14
,
fontWeight
:
400
,
lineHeight
:
'28px'
,
},
wrap
:
{
display
:
'inline-block'
,
},
}
as
const
/* CSS minified from
body { margin: 0; color: #000; background: #fff; }
.next-error-h1 {
border-right: 1px solid rgba(0, 0, 0, .3);
}
@media (prefers-color-scheme: dark) {
body { color: #fff; background: #000; }
.next-error-h1 {
border-right: 1px solid rgba(255, 255, 255, .3);
}
}
*/
const
themeCss
=
`body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}
@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}`
function
AppError
()
{
const
errorMessage
=
'Internal Server Error.'
const
title
=
`500:
${
errorMessage
}
`
return
(
<
html
id=
"__next_error__"
>
<
head
>
<
title
>
{
title
}
</
title
>
</
head
>
<
body
>
<
div
style=
{
styles
.
error
}
>
<
div
style=
{
styles
.
desc
}
>
<
style
dangerouslySetInnerHTML=
{
{
__html
:
themeCss
,
}
}
/>
<
h1
className=
"next-error-h1"
style=
{
styles
.
h1
}
>
500
</
h1
>
<
div
style=
{
styles
.
wrap
}
>
<
h2
style=
{
styles
.
h2
}
>
{
errorMessage
}
</
h2
>
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
)
}
export
default
AppError
packages/next/src/client/components/handle-isr-error.tsx
View file @
8f6cd5e8
...
...
@@ -12,7 +12,9 @@ export function HandleISRError({ error }: { error: any }) {
if
(
workAsyncStorage
)
{
const
store
=
workAsyncStorage
.
getStore
()
if
(
store
?.
isRevalidate
||
store
?.
isStaticGeneration
)
{
console
.
error
(
error
)
if
(
error
)
{
console
.
error
(
error
)
}
throw
error
}
}
...
...
packages/next/src/export/index.ts
View file @
8f6cd5e8
...
...
@@ -444,8 +444,15 @@ async function exportAppImpl(
return
exportMap
})
// During static export, remove export 404/500 of pages router
// when only app router presents
if
(
!
options
.
buildExport
&&
options
.
appDirOnly
)
{
delete
exportPathMap
[
'/404'
]
delete
exportPathMap
[
'/500'
]
}
// only add missing 404 page when `buildExport` is false
if
(
!
options
.
buildExport
)
{
if
(
!
options
.
buildExport
&&
!
options
.
appDirOnly
)
{
// only add missing /404 if not specified in `exportPathMap`
if
(
!
exportPathMap
[
'/404'
])
{
exportPathMap
[
'/404'
]
=
{
page
:
'/_error'
}
...
...
@@ -455,7 +462,7 @@ async function exportAppImpl(
* exports 404.html for backwards compat
* E.g. GitHub Pages, GitLab Pages, Cloudflare Pages, Netlify
*/
if
(
!
exportPathMap
[
'/404.html'
])
{
if
(
!
exportPathMap
[
'/404.html'
]
&&
exportPathMap
[
'/404'
]
)
{
// alias /404.html to /404 to be compatible with custom 404 / _error page
exportPathMap
[
'/404.html'
]
=
exportPathMap
[
'/404'
]
}
...
...
@@ -492,7 +499,9 @@ async function exportAppImpl(
}
if
(
allExportPaths
.
length
===
0
)
{
return
null
if
(
!
prerenderManifest
)
{
return
null
}
}
if
(
fallbackEnabledPages
.
size
>
0
)
{
...
...
@@ -640,44 +649,53 @@ async function exportAppImpl(
initialPhaseExportPaths
=
allExportPaths
}
const
progress
=
createProgress
(
initialPhaseExportPaths
.
length
+
finalPhaseExportPaths
.
length
,
options
.
statusMessage
||
'Exporting'
)
const
totalExportPaths
=
initialPhaseExportPaths
.
length
+
finalPhaseExportPaths
.
length
let
worker
:
StaticWorker
|
null
=
null
let
results
:
ExportPagesResult
=
[]
const
worker
=
createStaticWorker
(
nextConfig
,
{
debuggerPortOffset
:
getNextBuildDebuggerPortOffset
({
kind
:
'export-page'
}),
progress
,
})
if
(
totalExportPaths
>
0
)
{
const
progress
=
createProgress
(
totalExportPaths
,
options
.
statusMessage
||
'Exporting'
)
const
results
=
await
exportPagesInBatches
(
worker
,
initialPhaseExportPaths
)
worker
=
createStaticWorker
(
nextConfig
,
{
debuggerPortOffset
:
getNextBuildDebuggerPortOffset
({
kind
:
'export-page'
,
}),
progress
,
})
if
(
finalPhaseExportPaths
.
length
>
0
)
{
const
renderResumeDataCachesByPage
:
Record
<
string
,
string
>
=
{}
results
=
await
exportPagesInBatches
(
worker
,
initialPhaseExportPaths
)
for
(
const
{
page
,
result
}
of
results
)
{
if
(
!
result
)
{
continue
}
if
(
finalPhaseExportPaths
.
length
>
0
)
{
const
renderResumeDataCachesByPage
:
Record
<
string
,
string
>
=
{}
if
(
'renderResumeDataCache'
in
result
&&
result
.
renderResumeDataCache
)
{
// The last RDC for each page is used. We only need one. It should have
// all the entries that the fallback shell also needs. We don't need to
// merge them per page.
renderResumeDataCachesByPage
[
page
]
=
result
.
renderResumeDataCache
// Remove the RDC string from the result so that it can be garbage
// collected, when there are more results for the same page.
result
.
renderResumeDataCache
=
undefined
for
(
const
{
page
,
result
}
of
results
)
{
if
(
!
result
)
{
continue
}
if
(
'renderResumeDataCache'
in
result
&&
result
.
renderResumeDataCache
)
{
// The last RDC for each page is used. We only need one. It should have
// all the entries that the fallback shell also needs. We don't need to
// merge them per page.
renderResumeDataCachesByPage
[
page
]
=
result
.
renderResumeDataCache
// Remove the RDC string from the result so that it can be garbage
// collected, when there are more results for the same page.
result
.
renderResumeDataCache
=
undefined
}
}
}
const
finalPhaseResults
=
await
exportPagesInBatches
(
worker
,
finalPhaseExportPaths
,
renderResumeDataCachesByPage
)
const
finalPhaseResults
=
await
exportPagesInBatches
(
worker
,
finalPhaseExportPaths
,
renderResumeDataCachesByPage
)
results
.
push
(...
finalPhaseResults
)
results
.
push
(...
finalPhaseResults
)
}
}
let
hadValidationError
=
false
...
...
@@ -763,8 +781,41 @@ async function exportAppImpl(
if
(
!
options
.
buildExport
&&
prerenderManifest
)
{
await
Promise
.
all
(
Object
.
keys
(
prerenderManifest
.
routes
).
map
(
async
(
unnormalizedRoute
)
=>
{
// S
kip handling /_not-found route, it will copy the 404.html file later
// S
pecial handling: map app /_not-found to 404.html (and 404/index.html when trailingSlash)
if
(
unnormalizedRoute
===
'/_not-found'
)
{
const
{
srcRoute
}
=
prerenderManifest
!
.
routes
[
unnormalizedRoute
]
const
appPageName
=
mapAppRouteToPage
.
get
(
srcRoute
||
''
)
const
pageName
=
appPageName
||
srcRoute
||
unnormalizedRoute
const
isAppPath
=
Boolean
(
appPageName
)
const
route
=
normalizePagePath
(
unnormalizedRoute
)
const
pagePath
=
getPagePath
(
pageName
,
distDir
,
undefined
,
isAppPath
)
const
distPagesDir
=
join
(
pagePath
,
pageName
.
slice
(
1
)
.
split
(
'/'
)
.
map
(()
=>
'..'
)
.
join
(
'/'
)
)
const
orig
=
join
(
distPagesDir
,
route
)
const
htmlSrc
=
`
${
orig
}
.html`
// write 404.html at root
const
htmlDest404
=
join
(
outDir
,
'404.html'
)
await
fs
.
mkdir
(
dirname
(
htmlDest404
),
{
recursive
:
true
})
await
fs
.
copyFile
(
htmlSrc
,
htmlDest404
)
// When trailingSlash, also write 404/index.html
if
(
subFolders
)
{
const
htmlDest404Index
=
join
(
outDir
,
'404'
,
'index.html'
)
await
fs
.
mkdir
(
dirname
(
htmlDest404Index
),
{
recursive
:
true
})
await
fs
.
copyFile
(
htmlSrc
,
htmlDest404Index
)
}
}
// Skip 500.html in static export
if
(
unnormalizedRoute
===
'/_global-error'
)
{
return
}
const
{
srcRoute
}
=
prerenderManifest
!
.
routes
[
unnormalizedRoute
]
...
...
@@ -840,6 +891,7 @@ async function exportAppImpl(
}
const
segmentsDir
=
`
${
orig
}${
RSC_SEGMENTS_DIR_SUFFIX
}
`
if
(
isAppPath
&&
existsSync
(
segmentsDir
))
{
// Output a data file for each of this page's segments
//
...
...
@@ -903,7 +955,9 @@ async function exportAppImpl(
await
telemetry
.
flush
()
}
await
worker
.
end
()
if
(
worker
)
{
await
worker
.
end
()
}
return
collector
}
...
...
packages/next/src/export/routes/app-page.ts
View file @
8f6cd5e8
...
...
@@ -30,6 +30,10 @@ import type { RequestLifecycleOpts } from '../../server/base-server'
import
type
{
AppSharedContext
}
from
'../../server/app-render/app-render'
import
type
{
MultiFileWriter
}
from
'../../lib/multi-file-writer'
import
{
stringifyResumeDataCache
}
from
'../../server/resume-data-cache/resume-data-cache'
import
{
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
}
from
'../../shared/lib/entry-constants'
/**
* Renders & exports a page associated with the /app directory
...
...
@@ -59,12 +63,17 @@ export async function exportAppPage(
}
let
isDefaultNotFound
=
false
let
isDefaultGlobalError
=
false
// If the page is `/_not-found`, then we should update the page to be `/404`.
// UNDERSCORE_NOT_FOUND_ROUTE value used here, however we don't want to import it here as it causes constants to be inlined which we don't want here.
if
(
page
===
'/_not-found/page'
)
{
if
(
page
===
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
)
{
isDefaultNotFound
=
true
pathname
=
'/404'
}
// If the page is `/_global-error`, then we should update the page to be `/500`.
if
(
page
===
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
)
{
isDefaultGlobalError
=
true
pathname
=
'/500'
}
try
{
const
result
=
await
lazyRenderAppPage
(
...
...
@@ -197,6 +206,9 @@ export async function exportAppPage(
if
(
isDefaultNotFound
)
{
// Override the default /_not-found page status code to 404
status
=
404
}
else
if
(
isDefaultGlobalError
)
{
// Override the default /_global-error page status code to 500
status
=
500
}
else
if
(
isNonSuccessfulStatusCode
&&
!
isParallelRoute
)
{
// If it's parallel route the status from mock response is 404
status
=
res
.
statusCode
...
...
packages/next/src/export/types.ts
View file @
8f6cd5e8
...
...
@@ -112,6 +112,7 @@ export interface ExportAppOptions {
nextConfig
?:
NextConfigComplete
hasOutdirFromCli
?:
boolean
numWorkers
:
number
appDirOnly
:
boolean
}
export
type
ExportPageMetadata
=
{
...
...
packages/next/src/server/base-server.ts
View file @
8f6cd5e8
...
...
@@ -2749,9 +2749,10 @@ export default abstract class Server<
const
is404
=
res
.
statusCode
===
404
let
using404Page
=
false
const
hasAppDir
=
this
.
enabledDirectories
.
app
if
(
is404
)
{
if
(
this
.
enabledDirectories
.
app
)
{
if
(
hasAppDir
)
{
// Use the not-found entry in app directory
result
=
await
this
.
findPageComponents
({
locale
:
getRequestMeta
(
ctx
.
req
,
'locale'
),
...
...
@@ -2789,6 +2790,21 @@ export default abstract class Server<
// skip ensuring /500 in dev mode as it isn't used and the
// dev overlay is used instead
if
(
statusPage
!==
'/500'
||
!
this
.
renderOpts
.
dev
)
{
if
(
!
result
&&
hasAppDir
)
{
// Otherwise if app router present, load app router built-in 500 page
result
=
await
this
.
findPageComponents
({
locale
:
getRequestMeta
(
ctx
.
req
,
'locale'
),
page
:
statusPage
,
query
,
params
:
{},
isAppPath
:
true
,
// Ensuring can't be done here because you never "match" a 500
// route.
shouldEnsure
:
true
,
url
:
ctx
.
req
.
url
,
})
}
// If the above App Router result is empty, fallback to pages router 500 page
result
=
await
this
.
findPageComponents
({
locale
:
getRequestMeta
(
ctx
.
req
,
'locale'
),
page
:
statusPage
,
...
...
packages/next/src/server/dev/hot-reloader-webpack.ts
View file @
8f6cd5e8
...
...
@@ -650,6 +650,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
),
pagesDir
:
this
.
pagesDir
,
appDir
:
this
.
appDir
,
appDirOnly
:
Boolean
(
this
.
appDir
&&
!
this
.
pagesDir
),
})
)
...
...
packages/next/src/server/lib/router-utils/route-types-utils.ts
View file @
8f6cd5e8
...
...
@@ -16,6 +16,10 @@ import {
extractInterceptionRouteInformation
,
isInterceptionRouteAppPath
,
}
from
'../../../shared/lib/router/utils/interception-routes'
import
{
UNDERSCORE_GLOBAL_ERROR_ROUTE
,
UNDERSCORE_NOT_FOUND_ROUTE
,
}
from
'../../../shared/lib/entry-constants'
interface
RouteInfo
{
path
:
string
...
...
@@ -235,11 +239,15 @@ export async function createRouteTypesManifest({
}
}
// Process layout routes
// Process layout routes
(exclude internal app error/not-found layouts)
for
(
const
{
route
,
filePath
}
of
layoutRoutes
)
{
if
(
route
===
UNDERSCORE_GLOBAL_ERROR_ROUTE
||
route
===
UNDERSCORE_NOT_FOUND_ROUTE
)
continue
// Use the resolved route (for interception routes, this gives us the canonical route)
const
resolvedRoute
=
resolveInterceptingRoute
(
route
)
if
(
!
manifest
.
layoutRoutes
[
resolvedRoute
])
{
manifest
.
layoutRoutes
[
resolvedRoute
]
=
{
path
:
getRelativePath
(
filePath
),
...
...
@@ -256,8 +264,13 @@ export async function createRouteTypesManifest({
}
}
// Process app routes
// Process app routes
(exclude internal app routes)
for
(
const
{
route
,
filePath
}
of
appRoutes
)
{
if
(
route
===
UNDERSCORE_GLOBAL_ERROR_ROUTE
||
route
===
UNDERSCORE_NOT_FOUND_ROUTE
)
continue
// Don't include metadata routes or pages
if
(
!
filePath
.
endsWith
(
'page.ts'
)
&&
...
...
packages/next/src/server/require.ts
View file @
8f6cd5e8
...
...
@@ -78,6 +78,13 @@ export function getMaybePagePath(
return
null
}
// Handle absolute paths (e.g., built-in components)
if
(
path
.
isAbsolute
(
pagePath
))
{
// Use the absolute path as-is
pagePathCache
?.
set
(
cacheKey
,
pagePath
)
return
pagePath
}
pagePath
=
path
.
join
(
serverBuildPath
,
pagePath
)
pagePathCache
?.
set
(
cacheKey
,
pagePath
)
...
...
packages/next/src/shared/lib/constants.ts
View file @
8f6cd5e8
...
...
@@ -59,8 +59,14 @@ export const COMPILER_INDEXES: {
[
COMPILER_NAMES
.
edgeServer
]:
2
,
}
as
const
export
const
UNDERSCORE_NOT_FOUND_ROUTE
=
'/_not-found'
export
const
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
=
`
${
UNDERSCORE_NOT_FOUND_ROUTE
}
/page`
// Re-export entry constants for backward compatibility
export
{
UNDERSCORE_NOT_FOUND_ROUTE
,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
UNDERSCORE_GLOBAL_ERROR_ROUTE
,
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
,
}
from
'./entry-constants'
export
const
PHASE_EXPORT
=
'phase-export'
export
const
PHASE_PRODUCTION_BUILD
=
'phase-production-build'
export
const
PHASE_PRODUCTION_SERVER
=
'phase-production-server'
...
...
packages/next/src/shared/lib/entry-constants.ts
0 → 100644
View file @
8f6cd5e8
export
const
UNDERSCORE_NOT_FOUND_ROUTE
=
'/_not-found'
export
const
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
=
`
${
UNDERSCORE_NOT_FOUND_ROUTE
}
/page`
export
const
UNDERSCORE_GLOBAL_ERROR_ROUTE
=
'/_global-error'
export
const
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY
=
`
${
UNDERSCORE_GLOBAL_ERROR_ROUTE
}
/page`
test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts
View file @
8f6cd5e8
...
...
@@ -159,7 +159,18 @@ describe('unrecognized server actions', () => {
// FIXME: Currently, an unrecognized id in an MPA action results in a 500.
// This is not ideal, and ignores all nested `error.js` files, only showing the topmost one.
expect
(
response
.
status
()).
toBe
(
500
)
expect
(
response
.
headers
()[
'content-type'
]).
toStartWith
(
'text/html'
)
if
(
isNextDev
)
{
expect
(
response
.
headers
()[
'content-type'
]).
toStartWith
(
'text/html'
)
}
else
{
const
responseText
=
await
response
.
text
()
expect
(
responseText
).
toBe
(
'Internal Server Error'
)
expect
(
response
.
headers
()[
'content-type'
]).
toStartWith
(
'text/plain'
)
}
// In dev, the 500 page doesn't have any SSR'd html, so it won't show anything without JS.
if
(
!
isNextDev
)
{
expect
(
await
browser
.
elementByCss
(
'body'
).
text
()).
toContain
(
...
...
test/e2e/app-dir/cache-components-dynamic-imports/cache-components-dynamic-imports.test.ts
View file @
8f6cd5e8
...
...
@@ -39,6 +39,7 @@ describe('async imports in cacheComponents', () => {
expect
(
prerenderedRoutes
).
toMatchInlineSnapshot
(
`
[
"/_global-error",
"/_not-found",
"/inside-render/client/async-module",
"/inside-render/client/sync-module",
...
...
test/e2e/app-dir/metadata-static-generation/metadata-static-generation.test.ts
View file @
8f6cd5e8
...
...
@@ -24,6 +24,7 @@ const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
const
staticRoutes
=
prerenderManifest
.
routes
expect
(
Object
.
keys
(
staticRoutes
).
sort
()).
toEqual
([
'/'
,
'/_global-error'
,
'/_not-found'
,
'/suspenseful/static'
,
])
...
...
test/e2e/app-dir/metadata-streaming-static-generation/metadata-streaming-static-generation.test.ts
View file @
8f6cd5e8
...
...
@@ -22,6 +22,7 @@ const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
const
staticRoutes
=
prerenderManifest
.
routes
expect
(
Object
.
keys
(
staticRoutes
).
sort
()).
toEqual
([
'/'
,
'/_global-error'
,
'/_not-found'
,
'/slow/static'
,
'/suspenseful/static'
,
...
...
test/e2e/i18n-ignore-rewrite-source-locale/next.config.js
0 → 100644
View file @
8f6cd5e8
module
.
exports
=
{
basePath
:
'/basepath'
,
i18n
:
{
locales
:
[
'en'
,
'sv'
,
'nl'
],
defaultLocale
:
'en'
,
},
async
rewrites
()
{
return
{
beforeFiles
:
[
{
source
:
'/:locale/rewrite-files/:path*'
,
destination
:
'/:path*'
,
locale
:
false
,
},
{
source
:
'/:locale/rewrite-api/:path*'
,
destination
:
'/api/:path*'
,
locale
:
false
,
},
],
afterFiles
:
[],
fallback
:
[],
}
},
}
test/e2e/i18n-ignore-rewrite-source-locale/pages/api/hello.js
0 → 100644
View file @
8f6cd5e8
export
default
function
handler
(
req
,
res
)
{
res
.
send
(
'hello from api'
)
}
test/e2e/i18n-ignore-rewrite-source-locale/public/file.txt
0 → 100644
View file @
8f6cd5e8
hello from file.txt
\ No newline at end of file
test/e2e/i18n-ignore-rewrite-source-locale/rewrites-with-basepath.test.ts
View file @
8f6cd5e8
import
{
createNext
}
from
'e2e-utils'
import
{
NextInstance
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
fetchViaHTTP
,
renderViaHTTP
}
from
'next-test-utils'
import
path
from
'path'
import
fs
from
'fs-extra'
...
...
@@ -7,46 +6,14 @@ import fs from 'fs-extra'
const
locales
=
[
''
,
'/en'
,
'/sv'
,
'/nl'
]
describe
(
'i18n-ignore-rewrite-source-locale with basepath'
,
()
=>
{
let
next
:
NextInstance
beforeAll
(
async
()
=>
{
next
=
await
createNext
({
files
:
{
'pages/api/hello.js'
:
`
export default function handler(req, res) {
res.send('hello from api')
}`
,
'public/file.txt'
:
'hello from file.txt'
,
},
dependencies
:
{},
nextConfig
:
{
basePath
:
'/basepath'
,
i18n
:
{
locales
:
[
'en'
,
'sv'
,
'nl'
],
defaultLocale
:
'en'
,
},
async
rewrites
()
{
return
{
beforeFiles
:
[
{
source
:
'/:locale/rewrite-files/:path*'
,
destination
:
'/:path*'
,
locale
:
false
,
},
{
source
:
'/:locale/rewrite-api/:path*'
,
destination
:
'/api/:path*'
,
locale
:
false
,
},
],
afterFiles
:
[],
fallback
:
[],
}
},
},
})
const
{
next
,
skipped
}
=
nextTestSetup
({
files
:
__dirname
,
skipDeployment
:
true
,
})
afterAll
(()
=>
next
.
destroy
())
if
(
skipped
)
{
return
}
test
.
each
(
locales
)(
'get public file by skipping locale in rewrite, locale: %s'
,
...
...
test/integration/app-dir-export/test/utils.ts
View file @
8f6cd5e8
...
...
@@ -42,6 +42,8 @@ export const expectedWhenTrailingSlashTrue = [
?
[
'_next/static/test-build-id/_clientMiddlewareManifest.json'
]
:
[]),
'_next/static/test-build-id/_ssgManifest.js'
,
'_not-found/index.html'
,
'_not-found/index.txt'
,
'another/first/index.html'
,
'another/first/index.txt'
,
'another/index.html'
,
...
...
@@ -72,6 +74,8 @@ const expectedWhenTrailingSlashFalse = [
?
[
'_next/static/test-build-id/_clientMiddlewareManifest.json'
]
:
[]),
'_next/static/test-build-id/_ssgManifest.js'
,
'_not-found.html'
,
'_not-found.txt'
,
'another.html'
,
'another.txt'
,
'another/first.html'
,
...
...
test/integration/next-dynamic-css/tsconfig.json
View file @
8f6cd5e8
...
...
@@ -21,5 +21,5 @@
"strictNullChecks"
:
true
},
"include"
:
[
"next-env.d.ts"
,
".next/types/**/*.ts"
,
"**/*.ts"
,
"**/*.tsx"
],
"exclude"
:
[
"node_modules"
]
"exclude"
:
[
"node_modules"
,
"**/*.test.ts"
,
"**/*.test.tsx"
]
}
test/production/500-page/app-router-only/app-router-only.test.ts
0 → 100644
View file @
8f6cd5e8
import
{
nextTestSetup
}
from
'e2e-utils'
import
fsp
from
'fs/promises'
import
path
from
'path'
describe
(
'500-page app-router-only'
,
()
=>
{
const
{
next
,
isNextStart
,
isTurbopack
,
isNextDeploy
}
=
nextTestSetup
({
files
:
__dirname
,
})
if
(
isNextDeploy
)
{
it
(
'should render app router 500 page when route error'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/route-error'
)
expect
(
$
(
'html'
).
attr
(
'id'
)).
toBe
(
'__next_error__'
)
expect
(
$
(
'body'
).
text
()).
toContain
(
'Internal Server Error.'
)
expect
(
$
(
'body'
).
text
()).
toContain
(
'500'
)
})
}
if
(
isNextStart
)
{
it
(
'should use app router to generate 500.html when no pages _error.tsx exists'
,
async
()
=>
{
const
html
=
await
fsp
.
readFile
(
path
.
join
(
next
.
testDir
,
'.next'
,
'server'
,
'pages'
,
'500.html'
),
'utf8'
)
// Not use pages router to generate 500.html
expect
(
html
).
toContain
(
'__next_error__'
)
expect
(
html
).
toContain
(
'Internal Server Error.'
)
// global-error is not used in app router 500.html
expect
(
html
).
not
.
toContain
(
'app-router-global-error'
)
})
it
(
'pages manifest should only contain 404 and 500'
,
async
()
=>
{
const
pagesManifest
=
await
fsp
.
readFile
(
path
.
join
(
next
.
testDir
,
'.next'
,
'server'
,
'pages-manifest.json'
),
'utf8'
)
const
pagesManifestJson
=
JSON
.
parse
(
pagesManifest
)
expect
(
pagesManifestJson
).
toMatchInlineSnapshot
(
`
{
"/404": "pages/404.html",
"/500": "pages/500.html",
}
`
)
})
it
(
'should not contain pages router routes default assets'
,
async
()
=>
{
// do not contain _app, _document, _error routes folder or files in .next/server/pages
const
pagesDir
=
path
.
join
(
next
.
testDir
,
'.next'
,
'server'
,
'pages'
)
const
files
=
await
fsp
.
readdir
(
pagesDir
)
expect
(
files
).
not
.
toContain
(
'500'
)
if
(
isTurbopack
)
{
// In turbopack, _app, _document, _error are still generated with manifest, but no javascript files.
// The manifest are under the folder, they're still needed for static generation.
expect
(
files
).
not
.
toContain
(
'_app.js'
)
expect
(
files
).
not
.
toContain
(
'_document.js'
)
expect
(
files
).
not
.
toContain
(
'_error.js'
)
}
else
{
expect
(
files
).
not
.
toContain
(
'_app'
)
expect
(
files
).
not
.
toContain
(
'_document'
)
expect
(
files
).
not
.
toContain
(
'_error'
)
}
})
}
})
test/production/500-page/app-router-only/app/actions.ts
0 → 100644
View file @
8f6cd5e8
'use server'
export
async
function
throwErrorAction
()
{
throw
new
Error
(
'action error'
)
}
test/production/500-page/app-router-only/app/global-error.tsx
0 → 100644
View file @
8f6cd5e8
'use client'
export
default
function
GlobalError
()
{
return
(
<
html
>
<
body
>
<
h1
>
app-router-global-error
</
h1
>
</
body
>
</
html
>
)
}
test/production/500-page/app-router-only/app/layout.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
RootLayout
({
children
,
}:
{
children
:
React
.
ReactNode
})
{
return
(
<
html
lang=
"en"
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/production/500-page/app-router-only/app/page.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
Page
()
{
return
<
p
>
app page
</
p
>
}
test/production/500-page/app-router-only/app/route-error/route.tsx
0 → 100644
View file @
8f6cd5e8
import
{
connection
}
from
'next/server'
export
async
function
GET
()
{
await
connection
()
throw
new
Error
(
'route error'
)
}
test/production/500-page/app-router-only/middleware.ts
0 → 100644
View file @
8f6cd5e8
import
{
NextRequest
,
NextResponse
}
from
'next/server'
export
async
function
middleware
(
request
:
NextRequest
)
{
if
(
request
.
nextUrl
.
pathname
===
'/middleware-error'
)
{
throw
new
Error
(
'middleware error'
)
}
return
NextResponse
.
next
()
}
test/production/500-page/mixed-router-no-custom-pages-error/app/app-page/page.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
AppPage
()
{
return
<
p
>
app page
</
p
>
}
test/production/500-page/mixed-router-no-custom-pages-error/app/global-error.tsx
0 → 100644
View file @
8f6cd5e8
'use client'
export
default
function
GlobalError
()
{
return
(
<
html
>
<
body
>
<
p
>
app-router-global-error
</
p
>
</
body
>
</
html
>
)
}
test/production/500-page/mixed-router-no-custom-pages-error/app/layout.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
RootLayout
({
children
,
}:
{
children
:
React
.
ReactNode
})
{
return
(
<
html
lang=
"en"
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/production/500-page/mixed-router-no-custom-pages-error/mixed-router-no-error.test.ts
0 → 100644
View file @
8f6cd5e8
import
{
nextTestSetup
}
from
'e2e-utils'
import
fsp
from
'fs/promises'
import
path
from
'path'
describe
(
'500-page - mixed-router-no-custom-pages-error'
,
()
=>
{
const
{
next
,
skipped
}
=
nextTestSetup
({
files
:
__dirname
,
skipDeployment
:
true
,
})
if
(
skipped
)
{
return
}
it
(
'should not use app router global-error for 500.html when no pages _error.tsx exists'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/pages-error'
)
const
text
=
$
(
'#__next'
).
text
()
expect
(
text
).
toContain
(
'500'
)
expect
(
text
).
toContain
(
'Internal Server Error'
)
})
it
(
'pages manifest should only contain 404 and 500'
,
async
()
=>
{
const
pagesManifest
=
await
fsp
.
readFile
(
path
.
join
(
next
.
testDir
,
'.next'
,
'server'
,
'pages-manifest.json'
),
'utf8'
)
const
pagesManifestJson
=
JSON
.
parse
(
pagesManifest
)
expect
(
pagesManifestJson
).
toMatchInlineSnapshot
(
`
{
"/404": "pages/404.html",
"/_app": "pages/_app.js",
"/_document": "pages/_document.js",
"/_error": "pages/_error.js",
"/pages-error": "pages/pages-error.js",
}
`
)
})
it
(
'should generate 500.html with pages builtin _error'
,
async
()
=>
{
const
html
=
await
fsp
.
readFile
(
path
.
join
(
next
.
testDir
,
'.next'
,
'server'
,
'pages'
,
'500.html'
),
'utf8'
)
expect
(
html
).
toContain
(
'500'
)
expect
(
html
).
toContain
(
'Internal Server Error'
)
})
})
test/production/500-page/mixed-router-no-custom-pages-error/pages/pages-error.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
PagesPage
()
{
throw
new
Error
(
'pages page error'
)
return
<
p
>
pages page
</
p
>
}
export
async
function
getServerSideProps
()
{
return
{
props
:
{},
}
}
test/production/500-page/mixed-router-with-custom-pages-error/app/app-page/page.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
AppPage
()
{
return
<
p
>
app page
</
p
>
}
test/production/500-page/mixed-router-with-custom-pages-error/app/global-error.tsx
0 → 100644
View file @
8f6cd5e8
'use client'
export
default
function
GlobalError
({
error
,
reset
,
}:
{
error
:
Error
&
{
digest
?:
string
}
reset
:
()
=>
void
})
{
return
(
<
html
>
<
body
>
<
p
>
app-router-global-error
</
p
>
</
body
>
</
html
>
)
}
test/production/500-page/mixed-router-with-custom-pages-error/app/layout.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
RootLayout
({
children
,
}:
{
children
:
React
.
ReactNode
})
{
return
(
<
html
lang=
"en"
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/production/500-page/mixed-router-with-custom-pages-error/mixed-router-with-error.test.ts
0 → 100644
View file @
8f6cd5e8
import
{
nextTestSetup
}
from
'e2e-utils'
import
fsp
from
'fs/promises'
import
path
from
'path'
describe
(
'500-page - mixed-router-with-custom-pages-error'
,
()
=>
{
const
{
next
,
skipped
}
=
nextTestSetup
({
files
:
__dirname
,
skipDeployment
:
true
,
})
if
(
skipped
)
{
return
}
it
(
'should not override 500.html with app router global-error when pages _error.tsx exists'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/pages-error'
)
expect
(
$
(
'#__next'
).
text
()).
toBe
(
'pages-custom-error'
)
})
it
(
'should generate 500.html with pages builtin _error'
,
async
()
=>
{
const
html
=
await
fsp
.
readFile
(
path
.
join
(
next
.
testDir
,
'.next'
,
'server'
,
'pages'
,
'500.html'
),
'utf8'
)
expect
(
html
).
toContain
(
'pages-custom-error'
)
})
})
test/production/500-page/mixed-router-with-custom-pages-error/pages/_error.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
Error
()
{
return
<
p
>
pages-custom-error
</
p
>
}
test/production/500-page/mixed-router-with-custom-pages-error/pages/pages-error.tsx
0 → 100644
View file @
8f6cd5e8
export
default
function
PagesPage
()
{
throw
new
Error
(
'pages page error'
)
return
<
p
>
pages page
</
p
>
}
export
async
function
getServerSideProps
()
{
return
{
props
:
{},
}
}
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