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
d31472c4
Commit
d31472c4
authored
Mar 12, 2025
by
Janka Uryga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: nicer return types in action handler
parent
eb2a8512
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
97 deletions
+110
-97
action-handler.ts
packages/next/src/server/app-render/action-handler.ts
+77
-73
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+33
-24
No files found.
packages/next/src/server/app-render/action-handler.ts
View file @
d31472c4
...
@@ -455,12 +455,19 @@ type HandleActionResult =
...
@@ -455,12 +455,19 @@ type HandleActionResult =
type
:
'not-found'
type
:
'not-found'
}
}
|
{
|
{
/** The request turned out to not be an action. */
type
:
'not-an-action'
}
|
{
/** We decoded a FormState, but haven't rendered anything yet. */
type
:
'form-state'
formState
:
any
}
|
{
/** A finished result. */
type
:
'done'
type
:
'done'
result
:
RenderResult
|
undefined
result
:
RenderResult
formState
?:
any
}
}
/** The request turned out not to be a server action. */
|
null
export
async
function
handleAction
({
export
async
function
handleAction
({
req
,
req
,
...
@@ -500,7 +507,7 @@ export async function handleAction({
...
@@ -500,7 +507,7 @@ export async function handleAction({
// Note that this can be a false positive -- any multipart/urlencoded POST can get us here,
// Note that this can be a false positive -- any multipart/urlencoded POST can get us here,
// But won't know if it's an MPA action or not until we call `decodeAction` below.
// But won't know if it's an MPA action or not until we call `decodeAction` below.
if
(
!
isPossibleServerAction
)
{
if
(
!
isPossibleServerAction
)
{
return
null
return
{
type
:
'not-an-action'
}
}
}
if
(
workStore
.
isStaticGeneration
)
{
if
(
workStore
.
isStaticGeneration
)
{
...
@@ -696,36 +703,35 @@ export async function handleAction({
...
@@ -696,36 +703,35 @@ export async function handleAction({
throwIfActionNotInModuleMap
(
serverModuleMap
)
throwIfActionNotInModuleMap
(
serverModuleMap
)
)
)
if
(
typeof
action
===
'function'
)
{
if
(
typeof
action
!==
'function'
)
{
// an MPA action.
// We couldn't decode an action, so this POST request turned out not to be a server action request.
return
{
type
:
'not-an-action'
}
}
// Only warn if it's a server action, otherwise skip for other post requests
// an MPA action.
warnBadServerActionRequest
()
const
actionReturnedState
=
// Only warn if it's a server action, otherwise skip for other post requests
await
executeActionAndPrepareForRender
(
warnBadServerActionRequest
()
action
as
()
=>
Promise
<
unknown
>
,
[],
workStore
,
requestStore
)
const
formState
=
await
decodeFormState
(
const
actionReturnedState
=
actionReturnedState
,
await
executeActionAndPrepareForRender
(
formData
,
action
as
()
=>
Promise
<
unknown
>
,
serverModuleMap
[],
workStore
,
requestStore
)
)
// Skip the fetch path.
const
formState
=
await
decodeFormState
(
// We need to render a full HTML version of the page for the response, we'll handle that in app-render.
actionReturnedState
,
return
{
formData
,
type
:
'done'
,
serverModuleMap
result
:
undefined
,
)
formState
,
}
// Skip the fetch path.
}
else
{
// We need to render a full HTML version of the page for the response, we'll handle that in app-render.
// We couldn't decode an action, so this POST request turned out not to be a server action request.
return
{
return
null
type
:
'form-state'
,
formState
,
}
}
}
}
}
else
{
}
else
{
...
@@ -734,7 +740,7 @@ export async function handleAction({
...
@@ -734,7 +740,7 @@ export async function handleAction({
// If it's not multipart AND not a fetch action,
// If it's not multipart AND not a fetch action,
// then it can't be an action request.
// then it can't be an action request.
if
(
!
isFetchAction
)
{
if
(
!
isFetchAction
)
{
return
null
return
{
type
:
'not-an-action'
}
}
}
try
{
try
{
...
@@ -901,36 +907,35 @@ export async function handleAction({
...
@@ -901,36 +907,35 @@ export async function handleAction({
throwIfActionNotInModuleMap
(
serverModuleMap
)
throwIfActionNotInModuleMap
(
serverModuleMap
)
)
)
if
(
typeof
action
===
'function'
)
{
if
(
typeof
action
!==
'function'
)
{
// an MPA action.
// We couldn't decode an action, so this POST request turned out not to be a server action request.
return
{
type
:
'not-an-action'
}
}
// Only warn if it's a server action, otherwise skip for other post requests
// an MPA action.
warnBadServerActionRequest
()
const
actionReturnedState
=
// Only warn if it's a server action, otherwise skip for other post requests
await
executeActionAndPrepareForRender
(
warnBadServerActionRequest
()
action
as
()
=>
Promise
<
unknown
>
,
[],
workStore
,
requestStore
)
const
formState
=
await
decodeFormState
(
const
actionReturnedState
=
actionReturnedState
,
await
executeActionAndPrepareForRender
(
formData
,
action
as
()
=>
Promise
<
unknown
>
,
serverModuleMap
[],
workStore
,
requestStore
)
)
// Skip the fetch path.
const
formState
=
await
decodeFormState
(
// We need to render a full HTML version of the page for the response, we'll handle that in app-render.
actionReturnedState
,
return
{
formData
,
type
:
'done'
,
serverModuleMap
result
:
undefined
,
)
formState
,
}
// Skip the fetch path.
}
else
{
// We need to render a full HTML version of the page for the response, we'll handle that in app-render.
// We couldn't decode an action, so this POST request turned out not to be a server action request.
return
{
return
null
type
:
'form-state'
,
formState
,
}
}
}
}
}
else
{
}
else
{
...
@@ -939,7 +944,7 @@ export async function handleAction({
...
@@ -939,7 +944,7 @@ export async function handleAction({
// If it's not multipart AND not a fetch action,
// If it's not multipart AND not a fetch action,
// then it can't be an action request.
// then it can't be an action request.
if
(
!
isFetchAction
)
{
if
(
!
isFetchAction
)
{
return
null
return
{
type
:
'not-an-action'
}
}
}
try
{
try
{
...
@@ -978,6 +983,11 @@ export async function handleAction({
...
@@ -978,6 +983,11 @@ export async function handleAction({
throw
new
Error
(
'Invariant: Unknown request type.'
)
throw
new
Error
(
'Invariant: Unknown request type.'
)
}
}
// Handle a fetch action.
// Ensure that non-fetch codepaths can't reach this part.
isFetchAction
satisfies
true
// actions.js
// actions.js
// app/page.js
// app/page.js
// action worker1
// action worker1
...
@@ -993,6 +1003,7 @@ export async function handleAction({
...
@@ -993,6 +1003,7 @@ export async function handleAction({
const
actionMod
=
(
await
ComponentMod
.
__next_app__
.
require
(
const
actionMod
=
(
await
ComponentMod
.
__next_app__
.
require
(
actionModId
actionModId
))
as
Record
<
string
,
(...
args
:
unknown
[])
=>
Promise
<
unknown
>>
))
as
Record
<
string
,
(...
args
:
unknown
[])
=>
Promise
<
unknown
>>
const
actionHandler
=
actionMod
[
actionId
]
const
actionHandler
=
actionMod
[
actionId
]
const
returnVal
=
await
executeActionAndPrepareForRender
(
const
returnVal
=
await
executeActionAndPrepareForRender
(
...
@@ -1004,23 +1015,16 @@ export async function handleAction({
...
@@ -1004,23 +1015,16 @@ export async function handleAction({
addRevalidationHeader
(
res
,
{
workStore
,
requestStore
})
addRevalidationHeader
(
res
,
{
workStore
,
requestStore
})
})
})
// For form actions, we need to continue rendering the page.
const
actionResult
=
await
generateFlight
(
req
,
ctx
,
requestStore
,
{
if
(
isFetchAction
)
{
actionResult
:
Promise
.
resolve
(
returnVal
),
const
actionResult
=
await
generateFlight
(
req
,
ctx
,
requestStore
,
{
// if the page was not revalidated, or if the action was forwarded from another worker, we can skip the rendering the flight tree
actionResult
:
Promise
.
resolve
(
returnVal
),
skipFlight
:
!
workStore
.
pathWasRevalidated
||
actionWasForwarded
,
// if the page was not revalidated, or if the action was forwarded from another worker, we can skip the rendering the flight tree
temporaryReferences
,
skipFlight
:
!
workStore
.
pathWasRevalidated
||
actionWasForwarded
,
})
temporaryReferences
,
})
return
{
return
{
type
:
'done'
,
type
:
'done'
,
result
:
actionResult
,
result
:
actionResult
,
}
}
else
{
// TODO: this shouldn't be reachable, because all non-fetch codepaths return early.
// this will be handled in a follow-up refactor PR.
return
null
}
}
}
}
)
)
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
d31472c4
...
@@ -1585,31 +1585,40 @@ async function renderToHTMLOrFlightImpl(
...
@@ -1585,31 +1585,40 @@ async function renderToHTMLOrFlightImpl(
metadata
,
metadata
,
})
})
if
(
actionRequestResult
)
{
if
(
actionRequestResult
.
type
===
'not-found'
)
{
if
(
actionRequestResult
.
type
===
'not-found'
)
{
const
notFoundLoaderTree
=
createNotFoundLoaderTree
(
loaderTree
)
const
notFoundLoaderTree
=
createNotFoundLoaderTree
(
loaderTree
)
res
.
statusCode
=
404
res
.
statusCode
=
404
metadata
.
statusCode
=
404
metadata
.
statusCode
=
404
const
stream
=
await
renderToStreamWithTracing
(
const
stream
=
await
renderToStreamWithTracing
(
requestStore
,
requestStore
,
req
,
req
,
res
,
res
,
ctx
,
ctx
,
workStore
,
notFoundLoaderTree
,
notFoundLoaderTree
,
formState
,
formState
,
postponedState
,
postponedState
,
metadata
metadata
)
)
return
new
RenderResult
(
stream
,
{
metadata
})
return
new
RenderResult
(
stream
,
{
metadata
})
}
else
if
(
actionRequestResult
.
type
===
'done'
)
{
}
else
if
(
actionRequestResult
.
type
===
'done'
)
{
if
(
actionRequestResult
.
result
)
{
// We ran the action, and have a flight result. Nothing more to do here.
actionRequestResult
.
result
.
assignMetadata
(
metadata
)
actionRequestResult
.
result
.
assignMetadata
(
metadata
)
return
actionRequestResult
.
result
return
actionRequestResult
.
result
}
else
if
(
actionRequestResult
.
formState
)
{
}
else
if
(
actionRequestResult
.
type
===
'form-state'
)
{
formState
=
actionRequestResult
.
formState
// an MPA action. We ran the action, but haven't rendered anything yet, only decoded a form state.
}
// We need to produce an HTML response, so continue rendering below.
}
formState
=
actionRequestResult
.
formState
}
else
if
(
actionRequestResult
.
type
===
'not-an-action'
)
{
// This is likely a form submission request targetting a page. Generally, this can happen if:
// - the user did `<form action="/this-page">` using a vanilla HTML form
// - the user did `<form action="/router-handler">` using a vanilla HTML form,
// and the route handler responded with a 307/308 to a page, so the browser retried the POST request at the new URL
// In both of these cases we should render the page to HTML as a response.
//
// TODO: we should consider disallowing these requests altogether, because they only work for dynamic pages,
// and would stop working if the page became static. (a POST to a static page will result in a 405).
}
}
}
}
...
...
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