Commit 9954a21c authored by JJ Kasper's avatar JJ Kasper

Update revalidateTag to batch tags in one request (#65296)

As discussed this collects all `revalidateTag` calls and invokes in a
single request to avoid race conditions and overhead from multiple
pending requests.

x-ref: [slack
thread](https://vercel.slack.com/archives/C0676QZBWKS/p1714688045037509?thread_ts=1710902198.529179&cid=C0676QZBWKS)

Closes NEXT-3306
parent 937651fe
...@@ -117,9 +117,12 @@ async function addRevalidationHeader( ...@@ -117,9 +117,12 @@ async function addRevalidationHeader(
requestStore: RequestStore requestStore: RequestStore
} }
) { ) {
await Promise.all( await Promise.all([
Object.values(staticGenerationStore.pendingRevalidates || []) staticGenerationStore.incrementalCache?.revalidateTag(
) staticGenerationStore.revalidatedTags || []
),
...Object.values(staticGenerationStore.pendingRevalidates || {}),
])
// If a tag was revalidated, the client router needs to invalidate all the // If a tag was revalidated, the client router needs to invalidate all the
// client router cache as they may be stale. And if a path was revalidated, the // client router cache as they may be stale. And if a path was revalidated, the
...@@ -481,9 +484,12 @@ export async function handleAction({ ...@@ -481,9 +484,12 @@ export async function handleAction({
if (isFetchAction) { if (isFetchAction) {
res.statusCode = 500 res.statusCode = 500
await Promise.all( await Promise.all([
Object.values(staticGenerationStore.pendingRevalidates || []) staticGenerationStore.incrementalCache?.revalidateTag(
) staticGenerationStore.revalidatedTags || []
),
...Object.values(staticGenerationStore.pendingRevalidates || {}),
])
const promise = Promise.reject(error) const promise = Promise.reject(error)
try { try {
...@@ -840,9 +846,12 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc ...@@ -840,9 +846,12 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc
if (isFetchAction) { if (isFetchAction) {
res.statusCode = 500 res.statusCode = 500
await Promise.all( await Promise.all([
Object.values(staticGenerationStore.pendingRevalidates || []) staticGenerationStore.incrementalCache?.revalidateTag(
) staticGenerationStore.revalidatedTags || []
),
...Object.values(staticGenerationStore.pendingRevalidates || {}),
])
const promise = Promise.reject(err) const promise = Promise.reject(err)
try { try {
// we need to await the promise to trigger the rejection early // we need to await the promise to trigger the rejection early
......
...@@ -1350,9 +1350,12 @@ async function renderToHTMLOrFlightImpl( ...@@ -1350,9 +1350,12 @@ async function renderToHTMLOrFlightImpl(
// If we have pending revalidates, wait until they are all resolved. // If we have pending revalidates, wait until they are all resolved.
if (staticGenerationStore.pendingRevalidates) { if (staticGenerationStore.pendingRevalidates) {
options.waitUntil = Promise.all( options.waitUntil = Promise.all([
Object.values(staticGenerationStore.pendingRevalidates) staticGenerationStore.incrementalCache?.revalidateTag(
) staticGenerationStore.revalidatedTags || []
),
...Object.values(staticGenerationStore.pendingRevalidates || {}),
])
} }
addImplicitTags(staticGenerationStore) addImplicitTags(staticGenerationStore)
......
...@@ -383,11 +383,14 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -383,11 +383,14 @@ export class AppRouteRouteModule extends RouteModule<
context.renderOpts.fetchMetrics = context.renderOpts.fetchMetrics =
staticGenerationStore.fetchMetrics staticGenerationStore.fetchMetrics
context.renderOpts.waitUntil = Promise.all( context.renderOpts.waitUntil = Promise.all([
Object.values( staticGenerationStore.incrementalCache?.revalidateTag(
staticGenerationStore.pendingRevalidates || [] staticGenerationStore.revalidatedTags || []
) ),
) ...Object.values(
staticGenerationStore.pendingRevalidates || {}
),
])
addImplicitTags(staticGenerationStore) addImplicitTags(staticGenerationStore)
;(context.renderOpts as any).fetchTags = ;(context.renderOpts as any).fetchTags =
......
...@@ -68,15 +68,6 @@ function revalidate(tag: string, expression: string) { ...@@ -68,15 +68,6 @@ function revalidate(tag: string, expression: string) {
store.revalidatedTags.push(tag) store.revalidatedTags.push(tag)
} }
if (!store.pendingRevalidates) {
store.pendingRevalidates = {}
}
store.pendingRevalidates[tag] = store.incrementalCache
.revalidateTag?.(tag)
.catch((err) => {
console.error(`revalidate failed for ${tag}`, err)
})
// TODO: only revalidate if the path matches // TODO: only revalidate if the path matches
store.pathWasRevalidated = true store.pathWasRevalidated = 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