Skip to content

Commit

Permalink
Skip setting to fetch cache when not modified (#66055)
Browse files Browse the repository at this point in the history
To avoid extra network hops we can compare existing cache entries we've
already fetched and see if the revalidated value matches and if it does
we can avoid sending the set request with the identical data.
  • Loading branch information
ijjk authored May 22, 2024
1 parent ad8d1c2 commit b17ca02
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,26 @@ export default class FetchCache implements CacheHandler {

public async set(...args: Parameters<CacheHandler['set']>) {
const [key, data, ctx] = args

const newValue = data?.kind === 'FETCH' ? data.data : undefined
const existingCache = memoryCache?.get(key)
const existingValue = existingCache?.value
if (
existingValue?.kind === 'FETCH' &&
Object.keys(existingValue.data).every(
(field) =>
JSON.stringify(
(existingValue.data as Record<string, string | Object>)[field]
) ===
JSON.stringify((newValue as Record<string, string | Object>)[field])
)
) {
if (this.debug) {
console.log(`skipping cache set for ${key} as not modified`)
}
return
}

const { fetchCache, fetchIdx, fetchUrl, tags } = ctx
if (!fetchCache) return

Expand Down

0 comments on commit b17ca02

Please sign in to comment.