Unverified Commit 3904d0c2 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Flush pending revalidations for forwarded action error responses (#96945)

## Bug

When a Server Action skips page rendering, the successful response path
attaches pending revalidations to the response’s `waitUntil` promise.
Error paths, including `notFound()`, did not.

As a result, a forwarded action that called `revalidatePath()` or
`revalidateTag()` and then threw could return its error response without
executing the pending invalidation.

## Fix

Centralize the skipped-render revalidation handling and attach its
promise to successful and error responses.

Actions that continue into page rendering retain the existing behavior,
where revalidations are executed before rendering.

## Test Plan

Add a production regression test that invokes an action which calls
`revalidatePath()` followed by `notFound()` and verifies:

- The fallback route’s page is not rendered.
- The cache handler receives the expected path-tag invalidation.
- Removing the new `waitUntil` attachment causes the test to fail.
parent 1ab0f1ad
...@@ -564,6 +564,20 @@ type HandleActionResult = ...@@ -564,6 +564,20 @@ type HandleActionResult =
/** The request turned out not to be a server action. */ /** The request turned out not to be a server action. */
| null | null
function getRevalidationWaitUntil(
workStore: WorkStore,
skipPageRendering: boolean
): Promise<void> | undefined {
if (!skipPageRendering) {
// Page rendering executes pending revalidations before rendering. We only
// need to attach them to waitUntil when no page render will take place.
return undefined
}
const revalidatesPromise = executeRevalidates(workStore)
return revalidatesPromise === false ? undefined : revalidatesPromise
}
export async function handleAction({ export async function handleAction({
req, req,
res, res,
...@@ -1241,13 +1255,6 @@ export async function handleAction({ ...@@ -1241,13 +1255,6 @@ export async function handleAction({
// For form actions, we need to continue rendering the page. // For form actions, we need to continue rendering the page.
if (isFetchAction) { if (isFetchAction) {
// If we skip page rendering, we need to ensure pending revalidates
// are awaited before closing the response. Otherwise, this will be
// done after rendering the page.
const maybeRevalidatesPromise = skipPageRendering
? executeRevalidates(workStore)
: false
return { return {
type: 'done', type: 'done',
result: await actionAsyncStorage.exit(() => result: await actionAsyncStorage.exit(() =>
...@@ -1255,10 +1262,10 @@ export async function handleAction({ ...@@ -1255,10 +1262,10 @@ export async function handleAction({
actionResult: Promise.resolve(actionResult), actionResult: Promise.resolve(actionResult),
skipPageRendering, skipPageRendering,
temporaryReferences, temporaryReferences,
waitUntil: waitUntil: getRevalidationWaitUntil(
maybeRevalidatesPromise === false workStore,
? undefined skipPageRendering
: maybeRevalidatesPromise, ),
}) })
), ),
} }
...@@ -1325,6 +1332,10 @@ export async function handleAction({ ...@@ -1325,6 +1332,10 @@ export async function handleAction({
skipPageRendering: shouldSkipPageRendering, skipPageRendering: shouldSkipPageRendering,
actionResult: promise, actionResult: promise,
temporaryReferences, temporaryReferences,
waitUntil: getRevalidationWaitUntil(
workStore,
shouldSkipPageRendering
),
}), }),
} }
} }
...@@ -1355,17 +1366,20 @@ export async function handleAction({ ...@@ -1355,17 +1366,20 @@ export async function handleAction({
// swallow error, it's gonna be handled on the client // swallow error, it's gonna be handled on the client
} }
const skipPageRendering =
workStore.pathWasRevalidated === undefined ||
workStore.pathWasRevalidated === ActionDidNotRevalidate ||
shouldSkipPageRendering
return { return {
type: 'done', type: 'done',
result: await generateFlight(req, ctx, requestStore, { result: await generateFlight(req, ctx, requestStore, {
actionResult: promise, actionResult: promise,
// If the page was not revalidated, or if this is an action-only // If the page was not revalidated, or if this is an action-only
// request, we can skip rendering the page. // request, we can skip rendering the page.
skipPageRendering: skipPageRendering,
workStore.pathWasRevalidated === undefined ||
workStore.pathWasRevalidated === ActionDidNotRevalidate ||
shouldSkipPageRendering,
temporaryReferences, temporaryReferences,
waitUntil: getRevalidationWaitUntil(workStore, skipPageRendering),
}), }),
} }
} }
......
...@@ -9,6 +9,7 @@ describe('action-only fallback resume data cache', () => { ...@@ -9,6 +9,7 @@ describe('action-only fallback resume data cache', () => {
: { : {
NEXT_PRIVATE_TEST_HEADERS: '1', NEXT_PRIVATE_TEST_HEADERS: '1',
NEXT_PRIVATE_MINIMAL_MODE: '1', NEXT_PRIVATE_MINIMAL_MODE: '1',
TEST_CACHE_HANDLER: '1',
}, },
}) })
...@@ -79,13 +80,19 @@ describe('action-only fallback resume data cache', () => { ...@@ -79,13 +80,19 @@ describe('action-only fallback resume data cache', () => {
expect(responseBody).not.toContain('destination page rendered') expect(responseBody).not.toContain('destination page rendered')
}) })
it('does not render the fallback route when the action calls notFound', async () => { it('applies pending revalidations without rendering the fallback route when the action calls notFound', async () => {
const outputIndex = next.cliOutput.length
const response = await invokeAction('notFoundAfterRevalidation') const response = await invokeAction('notFoundAfterRevalidation')
expect(response.status).toBe(404) expect(response.status).toBe(404)
expect(response.headers.get('content-type')).toContain('text/x-component') expect(response.headers.get('content-type')).toContain('text/x-component')
const responseBody = await response.text() const responseBody = await response.text()
expect(responseBody).not.toContain('destination page rendered') expect(responseBody).not.toContain('destination page rendered')
const output = next.cliOutput.slice(outputIndex)
expect(output).toContain('ActionOnlyFallbackCacheHandler::updateTags')
expect(output).toContain('_N_T_/events/foo/group')
}) })
} }
}) })
// @ts-check
const defaultCacheHandler =
require('next/dist/server/lib/cache-handlers/default.external').default
/**
* @type {import('next/dist/server/lib/cache-handlers/types').CacheHandler}
*/
const cacheHandler = {
get: defaultCacheHandler.get.bind(defaultCacheHandler),
set: defaultCacheHandler.set.bind(defaultCacheHandler),
refreshTags: defaultCacheHandler.refreshTags.bind(defaultCacheHandler),
getExpiration: defaultCacheHandler.getExpiration.bind(defaultCacheHandler),
async updateTags(tags) {
console.log(
'ActionOnlyFallbackCacheHandler::updateTags',
JSON.stringify(tags)
)
return defaultCacheHandler.updateTags(tags)
},
}
module.exports = cacheHandler
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
*/ */
const nextConfig = { const nextConfig = {
cacheComponents: true, cacheComponents: true,
cacheHandlers: process.env.TEST_CACHE_HANDLER
? {
default: require.resolve('./cache-handler.js'),
}
: undefined,
} }
module.exports = nextConfig module.exports = nextConfig
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