Unverified Commit 958e34e0 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

actions: forward fetch rejections to the action handler (#49577)

Pretty edge case-y but this PR fixes the case where a fetch error happens for an action and we want to handle at the action handler level.



link NEXT-1123
parent a7fd95e3
...@@ -107,54 +107,62 @@ export function serverActionReducer( ...@@ -107,54 +107,62 @@ export function serverActionReducer(
fetchServerAction(state, action) fetchServerAction(state, action)
) )
} }
try {
// suspends until the server action is resolved.
const { actionResult, actionFlightData, redirectLocation } =
readRecordValue(
action.mutable.inFlightServerAction!
) as Awaited<FetchServerActionResult>
// suspends until the server action is resolved. if (redirectLocation) {
const { actionResult, actionFlightData, redirectLocation } = readRecordValue( // the redirection might have a flight data associated with it, so we'll populate the cache with it
action.mutable.inFlightServerAction! if (actionFlightData) {
) as Awaited<FetchServerActionResult> const href = createHrefFromUrl(
redirectLocation,
// Ensures the hash is not part of the cache key as it does not affect fetching the server
false
)
state.prefetchCache.set(href, {
data: createRecordFromThenable(
Promise.resolve([
actionFlightData,
// TODO-APP: verify the logic around canonical URL overrides
undefined,
])
),
kind: PrefetchKind.TEMPORARY, //TODO-APP: maybe this could cached longer?
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: action.mutable.previousTree!,
lastUsedTime: null,
})
}
if (redirectLocation) { // we throw the redirection in the action handler so that it is caught during render
// the redirection might have a flight data associated with it, so we'll populate the cache with it action.reject(
if (actionFlightData) { getRedirectError(redirectLocation.toString(), RedirectType.push)
const href = createHrefFromUrl(
redirectLocation,
// Ensures the hash is not part of the cache key as it does not affect fetching the server
false
) )
state.prefetchCache.set(href, { } else {
data: createRecordFromThenable( // TODO-APP: populate the prefetch cache with the new flight data
Promise.resolve([ if (actionFlightData) {
// this is an intentional hack around React: we want to update the tree in a new render
setTimeout(() => {
action.changeByServerResponse(
action.mutable.previousTree!,
actionFlightData, actionFlightData,
// TODO-APP: verify the logic around canonical URL overrides // TODO-APP: verify the logic around canonical URL overrides
undefined, undefined
]) )
), })
kind: PrefetchKind.TEMPORARY, //TODO-APP: maybe this could cached longer? }
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: action.mutable.previousTree!,
lastUsedTime: null,
})
}
// we throw the redirection in the action handler so that it is caught during render action.resolve(actionResult)
action.reject( }
getRedirectError(redirectLocation.toString(), RedirectType.push) } catch (e: any) {
) if (e.status === 'rejected') {
} else { action.reject(e.value)
// TODO-APP: populate the prefetch cache with the new flight data } else {
if (actionFlightData) { throw e
// this is an intentional hack around React: we want to update the tree in a new render
setTimeout(() => {
action.changeByServerResponse(
action.mutable.previousTree!,
actionFlightData,
// TODO-APP: verify the logic around canonical URL overrides
undefined
)
})
} }
action.resolve(actionResult)
} }
action.mutable.serverActionApplied = true action.mutable.serverActionApplied = true
......
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