Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetch-cache): add check for updated tags when checking same cache key #63547

Merged
merged 21 commits into from
Apr 9, 2024
Merged
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,17 @@ export default class FetchCache implements CacheHandler {
// on successive requests
let data = memoryCache?.get(key)

// get data from fetch cache
if (!data && this.cacheEndpoint) {
const isCacheable = !data && this.cacheEndpoint
const hasFetchKindAndIncludesAllTags = tags?.every((tag) => {
samcx marked this conversation as resolved.
Show resolved Hide resolved
if (data?.value?.kind !== 'FETCH') {
return false
}
return data.value.tags?.includes(tag)
samcx marked this conversation as resolved.
Show resolved Hide resolved
})

// Get data from fetch cache. Also check if new tags have been
// specified with the same cache key (fetch URL)
if (isCacheable || !hasFetchKindAndIncludesAllTags) {
samcx marked this conversation as resolved.
Show resolved Hide resolved
try {
const start = Date.now()
const fetchParams: NextFetchCacheParams = {
Expand Down Expand Up @@ -220,6 +229,11 @@ export default class FetchCache implements CacheHandler {
throw new Error(`invalid cache value`)
}

// if new tags were specified, merge those tags to the existing tags
if (cached.kind === 'FETCH') {
cached.tags = [...new Set([...(cached.tags ?? []), ...(tags ?? [])])]
samcx marked this conversation as resolved.
Show resolved Hide resolved
}

const cacheState = res.headers.get(CACHE_STATE_HEADER)
const age = res.headers.get('age')

Expand Down