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
95b4dc0e
Commit
95b4dc0e
authored
Mar 31, 2026
by
Will Binns-Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
handle top-level await
parent
c9193143
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
7 deletions
+37
-7
module.ts
packages/next/src/server/route-modules/app-route/module.ts
+37
-7
No files found.
packages/next/src/server/route-modules/app-route/module.ts
View file @
95b4dc0e
...
@@ -177,7 +177,9 @@ export interface AppRouteRouteModuleOptions
...
@@ -177,7 +177,9 @@ export interface AppRouteRouteModuleOptions
RouteModuleOptions
<
AppRouteRouteDefinition
,
AppRouteUserlandModule
>
,
RouteModuleOptions
<
AppRouteRouteDefinition
,
AppRouteUserlandModule
>
,
'userland'
'userland'
>
{
>
{
readonly
userland
:
AppRouteUserlandModule
|
(()
=>
AppRouteUserlandModule
)
readonly
userland
:
|
AppRouteUserlandModule
|
(()
=>
AppRouteUserlandModule
|
Promise
<
AppRouteUserlandModule
>
)
readonly
resolvedPagePath
:
string
readonly
resolvedPagePath
:
string
readonly
nextConfigOutput
:
NextConfig
[
'output'
]
readonly
nextConfigOutput
:
NextConfig
[
'output'
]
}
}
...
@@ -216,20 +218,47 @@ export class AppRouteRouteModule extends RouteModule<
...
@@ -216,20 +218,47 @@ export class AppRouteRouteModule extends RouteModule<
public
readonly
resolvedPagePath
:
string
public
readonly
resolvedPagePath
:
string
public
readonly
nextConfigOutput
:
NextConfig
[
'output'
]
|
undefined
public
readonly
nextConfigOutput
:
NextConfig
[
'output'
]
|
undefined
private
_loadUserland
:
(()
=>
AppRouteUserlandModule
)
|
null
private
_loadUserland
:
|
(()
=>
AppRouteUserlandModule
|
Promise
<
AppRouteUserlandModule
>
)
|
null
private
_pendingUserland
:
Promise
<
void
>
|
null
=
null
private
_methods
!
:
Record
<
HTTP_METHOD
,
AppRouteHandlerFn
>
private
_methods
!
:
Record
<
HTTP_METHOD
,
AppRouteHandlerFn
>
private
_hasNonStaticMethods
!
:
boolean
private
_hasNonStaticMethods
!
:
boolean
private
_dynamic
!
:
AppRouteUserlandModule
[
'dynamic'
]
private
_dynamic
!
:
AppRouteUserlandModule
[
'dynamic'
]
override
get
userland
():
AppRouteUserlandModule
{
override
get
userland
():
AppRouteUserlandModule
{
if
(
this
.
_loadUserland
)
{
if
(
this
.
_loadUserland
)
{
this
.
_userland
=
this
.
_loadUserland
()
const
result
=
this
.
_loadUserland
()
this
.
_loadUserland
=
null
this
.
_loadUserland
=
null
this
.
_initFromUserland
()
if
(
result
instanceof
Promise
)
{
// The userland module uses top-level await (async module). Store the
// promise so ensureUserland() can await it before the first request.
this
.
_pendingUserland
=
result
.
then
((
mod
)
=>
{
this
.
_userland
=
mod
this
.
_pendingUserland
=
null
this
.
_initFromUserland
()
})
}
else
{
this
.
_userland
=
result
this
.
_initFromUserland
()
}
}
}
return
this
.
_userland
return
this
.
_userland
}
}
/**
* Ensures the userland module is fully loaded before handling a request.
* This is needed when the route file uses top-level await (async module),
* in which case require() returns a Promise instead of the module directly.
*/
private
async
ensureUserland
():
Promise
<
void
>
{
// Trigger lazy loading if not yet started.
void
this
.
userland
if
(
this
.
_pendingUserland
)
{
await
this
.
_pendingUserland
}
}
constructor
({
constructor
({
userland
,
userland
,
definition
,
definition
,
...
@@ -325,9 +354,6 @@ export class AppRouteRouteModule extends RouteModule<
...
@@ -325,9 +354,6 @@ export class AppRouteRouteModule extends RouteModule<
// Ensure that the requested method is a valid method (to prevent RCE's).
// Ensure that the requested method is a valid method (to prevent RCE's).
if
(
!
isHTTPMethod
(
method
))
return
()
=>
new
Response
(
null
,
{
status
:
400
})
if
(
!
isHTTPMethod
(
method
))
return
()
=>
new
Response
(
null
,
{
status
:
400
})
// Trigger lazy initialization of userland if needed.
void
this
.
userland
return
this
.
_methods
[
method
]
return
this
.
_methods
[
method
]
}
}
...
@@ -723,6 +749,10 @@ export class AppRouteRouteModule extends RouteModule<
...
@@ -723,6 +749,10 @@ export class AppRouteRouteModule extends RouteModule<
req
:
NextRequest
,
req
:
NextRequest
,
context
:
AppRouteRouteHandlerContext
context
:
AppRouteRouteHandlerContext
):
Promise
<
Response
>
{
):
Promise
<
Response
>
{
// Ensure the userland module is fully loaded (handles async modules with
// top-level await, where require() returns a Promise).
await
this
.
ensureUserland
()
// Get the handler function for the given method.
// Get the handler function for the given method.
const
handler
=
this
.
resolve
(
req
.
method
)
const
handler
=
this
.
resolve
(
req
.
method
)
...
...
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