Skip to content

Commit df093a1

Browse files
committed
perf: skip lazy update on cache hit when cache purge is enabled
1 parent 9a746d2 commit df093a1

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

.changeset/pink-dodos-decide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
perf: skip lazy update on cache hit when cache purge is enabled

packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ type Options = {
3535
* Whether the regional cache entry should be updated in the background or not when it experiences
3636
* a cache hit.
3737
*
38-
* @default `false` for the `short-lived` mode, and `true` for the `long-lived` mode.
38+
* @default `true` in `long-lived` mode when cache purge is not used, `false` otherwise.
3939
*/
4040
shouldLazilyUpdateOnCacheHit?: boolean;
4141

4242
/**
4343
* Whether on cache hits the tagCache should be skipped or not. Skipping the tagCache allows requests to be
44-
* handled faster, the downside of this is that you need to make sure that the cache gets correctly purged
45-
* either by enabling the auto cache purging feature or doing that manually.
44+
* handled faster,
45+
*
46+
* Note: When this is enabled, make sure that the cache gets purged
47+
* either by enabling the auto cache purging feature or manually.
4648
*
4749
* @default `true` if the auto cache purging is enabled, `false` otherwise.
4850
*/
@@ -71,7 +73,8 @@ class RegionalCache implements IncrementalCache {
7173
throw new Error("The KV incremental cache does not need a regional cache.");
7274
}
7375
this.name = this.store.name;
74-
this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived";
76+
this.opts.shouldLazilyUpdateOnCacheHit ??=
77+
this.opts.mode === "long-lived" && !this.#hasAutomaticCachePurging;
7578
}
7679

7780
get #bypassTagCacheOnCacheHit(): boolean {
@@ -103,7 +106,8 @@ class RegionalCache implements IncrementalCache {
103106
if (cachedResponse) {
104107
debugCache("Get - cached response");
105108

106-
// Re-fetch from the store and update the regional cache in the background
109+
// Re-fetch from the store and update the regional cache in the background.
110+
// Note: this is only useful when the Cache API is not purged automatically.
107111
if (this.opts.shouldLazilyUpdateOnCacheHit) {
108112
getCloudflareContext().ctx.waitUntil(
109113
this.store.get(key, cacheType).then(async (rawEntry) => {

0 commit comments

Comments
 (0)