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
e796cd13
Commit
e796cd13
authored
Mar 11, 2025
by
Janka Uryga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: extract getting the action handler into getActionHandler
parent
1519bd74
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
31 deletions
+52
-31
errors.json
packages/next/errors.json
+2
-1
action-handler.ts
packages/next/src/server/app-render/action-handler.ts
+50
-30
No files found.
packages/next/errors.json
View file @
e796cd13
...
...
@@ -715,5 +715,6 @@
"714"
:
"cannot run loadNative when `NEXT_TEST_WASM` is set"
,
"715"
:
"Server Action
\"
%s
\"
was not found on the server.
\\
nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action"
,
"716"
:
"`unstable_isUnrecognizedActionError` can only be used on the client."
,
"717"
:
"This function cannot be used in the edge runtime"
"717"
:
"This function cannot be used in the edge runtime"
,
"718"
:
"Action handler not found in action module"
}
packages/next/src/server/app-render/action-handler.ts
View file @
e796cd13
...
...
@@ -631,10 +631,10 @@ export async function handleAction({
}
}
const
handleUnrecognizedFetchAction
=
(
err
:
unknown
):
HandleActionResult
=>
{
// If the deployment doesn't have skew protection, this is expected to occasionally happen,
// so we use a warning instead of an error.
console
.
warn
(
err
)
const
handleUnrecognizedFetchAction
=
(
err
?
:
unknown
):
HandleActionResult
=>
{
if
(
err
)
{
console
.
warn
(
err
)
}
// Return an empty response with a header that the client router will interpret.
// We don't need to waste time encoding a flight response, and using a blank body + header
...
...
@@ -886,6 +886,46 @@ export async function handleAction({
}
}
const
getActionHandler
=
async
(
// eslint-disable-next-line @typescript-eslint/no-shadow
actionId
:
string
):
Promise
<
((...
args
:
unknown
[])
=>
Promise
<
unknown
>
)
|
null
>
=>
{
// actions.js
// app/page.js
// action worker1
// appRender1
// app/foo/page.js
// action worker2
// appRender
// / -> fire action -> POST / -> appRender1 -> modId for the action file
// /foo -> fire action -> POST /foo -> appRender2 -> modId for the action file
// Validate the actionId.
let
actionModId
:
string
try
{
actionModId
=
getActionModIdOrError
(
actionId
,
serverModuleMap
)
}
catch
(
err
)
{
// TODO: this is weirdly split with `handleUnrecognizedFetchAction`, should unify
console
.
error
(
err
)
return
null
}
const
actionMod
=
(
await
ComponentMod
.
__next_app__
.
require
(
actionModId
))
as
Record
<
string
,
(...
args
:
unknown
[])
=>
Promise
<
unknown
>>
const
actionHandler
=
actionMod
[
actionId
]
// if the `actionId` is valid, and resolved to a valid `actionModId`, but the module does not contain the action,
// then something went very wrong in the bundling process, and we should error.
if
(
!
actionHandler
)
{
throw
new
InvariantError
(
'Action handler not found in action module'
)
}
return
actionHandler
}
try
{
return
await
actionAsyncStorage
.
run
(
{
isAction
:
true
},
...
...
@@ -896,12 +936,11 @@ export async function handleAction({
// A fetch action (initiated by the client router).
// Check the action id. if it's not valid, we can bail out immediately.
let
actionModId
:
string
try
{
actionModId
=
getActionModIdOrError
(
actionId
,
serverModuleMap
)
}
catch
(
err
)
{
return
handleUnrecognizedFetchAction
(
err
)
// Get the action function.
const
actionHandler
=
await
getActionHandler
(
actionId
)
if
(
!
actionHandler
)
{
// We didn't recognize the provided actionId, so we can't run the action.
return
handleUnrecognizedFetchAction
()
}
// The temporary reference set is used for parsing the arguments and in the catch handler,
...
...
@@ -922,26 +961,7 @@ export async function handleAction({
const
actionArguments
=
await
parseFetchActionArguments
(
temporaryReferences
)
// Get the action function.
// actions.js
// app/page.js
// action worker1
// appRender1
// app/foo/page.js
// action worker2
// appRender
// / -> fire action -> POST / -> appRender1 -> modId for the action file
// /foo -> fire action -> POST /foo -> appRender2 -> modId for the action file
const
actionMod
=
(
await
ComponentMod
.
__next_app__
.
require
(
actionModId
))
as
Record
<
string
,
(...
args
:
unknown
[])
=>
Promise
<
unknown
>>
const
actionHandler
=
actionMod
[
actionId
]
// Run the action.
const
returnVal
=
await
executeActionAndPrepareForRender
(
actionHandler
,
actionArguments
,
...
...
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