Skip setting to fetch cache when not modified (#66055)
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.
This commit is contained in:
parent
ad8d1c27ff
commit
b17ca02695
1 changed files with 20 additions and 0 deletions
|
@ -297,6 +297,26 @@ export default class FetchCache implements CacheHandler {
|
||||||
|
|
||||||
public async set(...args: Parameters<CacheHandler['set']>) {
|
public async set(...args: Parameters<CacheHandler['set']>) {
|
||||||
const [key, data, ctx] = args
|
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
|
const { fetchCache, fetchIdx, fetchUrl, tags } = ctx
|
||||||
if (!fetchCache) return
|
if (!fetchCache) return
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue