Skip to content

Commit aa204fd

Browse files
fix issue of calling getCloudflareContext too early
1 parent df0f1dd commit aa204fd

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ class RegionalCache implements IncrementalCache {
7272
}
7373
this.name = this.store.name;
7474
this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived";
75-
this.opts.bypassTagCacheOnCacheHit ??= getCloudflareContext().env.NEXT_CACHE_DO_PURGE ? true : false;
75+
}
76+
77+
get #bypassTagCacheOnCacheHit(): boolean {
78+
if (this.opts.bypassTagCacheOnCacheHit) {
79+
// If the bypassTagCacheOnCacheHit option is set we return that one
80+
return this.opts.bypassTagCacheOnCacheHit;
81+
}
82+
83+
// Otherwise we default to wether the automatic cache purging is enabled or not
84+
const hasAutomaticCachePurging = !!getCloudflareContext().env.NEXT_CACHE_DO_PURGE;
85+
return hasAutomaticCachePurging;
7686
}
7787

7888
async get<CacheType extends CacheEntryType = "cache">(
@@ -113,7 +123,7 @@ class RegionalCache implements IncrementalCache {
113123
this.putToCache({ key, cacheType, entry: { value, lastModified } })
114124
);
115125

116-
return { value, lastModified, shouldBypassTagCache: this.opts.bypassTagCacheOnCacheHit };
126+
return { value, lastModified, shouldBypassTagCache: this.#bypassTagCacheOnCacheHit };
117127
} catch (e) {
118128
error("Failed to get from regional cache", e);
119129
return null;

0 commit comments

Comments
 (0)