Commit e796cd13 authored by Janka Uryga's avatar Janka Uryga

refactor: extract getting the action handler into getActionHandler

parent 1519bd74
......@@ -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"
}
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment