Unverified Commit a1de339f authored by Siam's avatar Siam Committed by GitHub

fix: revalidateTag not reaching custom cache handlers when profiles differ (#99359)

Fix `updateTags` skipping cache handlers when a request uses multiple
cache profiles.

## Why

`getCacheHandlers()` returns a one-time iterator. When tags are grouped
by profile, the first group consumes the iterator, so subsequent groups
don't reach any handlers.

This is especially noticeable with custom cache handlers, where tags may
not be propagated to other instances.

## How

Convert the handlers iterator to an array once and reuse it for each
profile group.

## Test

Added an E2E test covering `updateTag()` and `revalidateTag()` with
different profiles.

Fixes #99352

---------
Co-authored-by: 's avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 50e90fb7
...@@ -89,7 +89,7 @@ async function revalidateTags( ...@@ -89,7 +89,7 @@ async function revalidateTags(
return return
} }
const handlers = getCacheHandlers() const handlers = getCacheHandlers() ?? []
const promises: Promise<void>[] = [] const promises: Promise<void>[] = []
// Group tags by profile for batch processing // Group tags by profile for batch processing
...@@ -167,7 +167,7 @@ async function revalidateTags( ...@@ -167,7 +167,7 @@ async function revalidateTags(
// If profile is not found and not 'max', durations will be undefined // If profile is not found and not 'max', durations will be undefined
// which will trigger immediate expiration in the cache handler // which will trigger immediate expiration in the cache handler
for (const handler of handlers || []) { for (const handler of handlers) {
if (profile) { if (profile) {
promises.push(handler.updateTags?.(tagsForProfile, durations)) promises.push(handler.updateTags?.(tagsForProfile, durations))
} else { } else {
......
...@@ -246,44 +246,34 @@ export function getDevTieredCacheHandler( ...@@ -246,44 +246,34 @@ export function getDevTieredCacheHandler(
} }
/** /**
* Get an iterator over the cache handlers. * Get the cache handlers. In dev, this also includes the built-in handlers (the
* @returns An iterator over the cache handlers, or `undefined` if they are not * private handler and the per-kind front handlers). The built-in handlers are
* initialized. * not part of the registered set, but tag operations must still reach them:
* their `updateTags` writes the shared tags manifest that their `get` consults,
* so `revalidateTag` can invalidate their entries.
* @returns The cache handlers, or `undefined` if they are not initialized.
*/ */
export function getCacheHandlers(): IterableIterator<CacheHandler> | undefined { export function getCacheHandlers(): CacheHandler[] | undefined {
const handlersSet = reference[handlersSetSymbol] const handlersSet = reference[handlersSetSymbol]
if (!handlersSet) { if (!handlersSet) {
return undefined return undefined
} }
if (process.env.__NEXT_DEV_SERVER) { const handlers = Array.from(handlersSet)
return iterateCacheHandlersWithDevBuiltIns(handlersSet)
}
return handlersSet.values()
}
/** if (process.env.__NEXT_DEV_SERVER) {
* Yields the registered handlers plus the dev-only built-in handlers (the const privateHandler = reference[privateHandlerSymbol]
* private handler and the per-kind front handlers). The built-in handlers are if (privateHandler) {
* not part of the registered set, but tag operations must still reach them: handlers.push(privateHandler)
* their `updateTags` writes the shared tags manifest that their `get` consults, }
* so `revalidateTag` can invalidate their entries.
*/
function* iterateCacheHandlersWithDevBuiltIns(
handlersSet: Set<CacheHandler>
): IterableIterator<CacheHandler> {
yield* handlersSet
const privateHandler = reference[privateHandlerSymbol] const devFrontHandlers = reference[devFrontHandlersSymbol]
if (privateHandler) { if (devFrontHandlers) {
yield privateHandler handlers.push(...devFrontHandlers.values())
}
} }
const devFrontHandlers = reference[devFrontHandlersSymbol] return handlers
if (devFrontHandlers) {
yield* devFrontHandlers.values()
}
} }
/** /**
......
...@@ -6,7 +6,7 @@ import { cloneCacheEntry } from './clone-cache-entry' ...@@ -6,7 +6,7 @@ import { cloneCacheEntry } from './clone-cache-entry'
* The subset of the cache-handler interface used on the per-invocation * The subset of the cache-handler interface used on the per-invocation
* read/write path in the "use cache" wrapper. Tag operations are intentionally * read/write path in the "use cache" wrapper. Tag operations are intentionally
* excluded: they are applied to the front and backing handlers individually by * excluded: they are applied to the front and backing handlers individually by
* the registry iterators (`getCacheHandlers` / `getCacheHandlerEntries`), never * the registry accessors (`getCacheHandlers` / `getCacheHandlerEntries`), never
* through this composite, so the composite is never registered. * through this composite, so the composite is never registered.
*/ */
export type CacheReadWriteHandler = Pick<CacheHandler, 'get' | 'set'> export type CacheReadWriteHandler = Pick<CacheHandler, 'get' | 'set'>
......
import { Suspense } from 'react' import { Suspense } from 'react'
import { cacheLife, cacheTag, revalidatePath, updateTag } from 'next/cache' import {
cacheLife,
cacheTag,
revalidatePath,
revalidateTag,
updateTag,
} from 'next/cache'
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
import { connection } from 'next/server' import { connection } from 'next/server'
import React from 'react' import React from 'react'
...@@ -58,6 +64,17 @@ export default async function Home() { ...@@ -58,6 +64,17 @@ export default async function Home() {
}} }}
> >
Revalidate Tag & Redirect Revalidate Tag & Redirect
</button>{' '}
<button
id="revalidate-multiple-profiles"
formAction={async () => {
'use server'
updateTag('modern')
revalidateTag('other', { expire: 0 })
}}
>
Revalidate Tags With Different Profiles
</button> </button>
</form> </form>
</main> </main>
......
...@@ -137,6 +137,24 @@ describe('use-cache-custom-handler', () => { ...@@ -137,6 +137,24 @@ describe('use-cache-custom-handler', () => {
}) })
}) })
it('should call updateTags once per profile group', async () => {
const browser = await next.browser(`/`)
outputIndex = next.cliOutput.length
await browser.elementById('revalidate-multiple-profiles').click()
await retry(async () => {
const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).toInclude(
'ModernCustomCacheHandler::updateTags ["modern"]'
)
expect(cliOutput).toInclude(
'ModernCustomCacheHandler::updateTags ["other"]'
)
})
})
if (isNextStart) { if (isNextStart) {
it('should save a short-lived cache during prerendering at buildtime', async () => { it('should save a short-lived cache during prerendering at buildtime', async () => {
expect(next.cliOutput).toMatch( expect(next.cliOutput).toMatch(
......
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