55 IncrementalCache ,
66 WithLastModified ,
77} from "@opennextjs/aws/types/overrides.js" ;
8+ import { compareSemver } from "@opennextjs/aws/utils/semver.js" ;
89
910import { getCloudflareContext } from "../../cloudflare-context.js" ;
1011import { debugCache , FALLBACK_BUILD_ID , IncrementalCacheEntry , isPurgeCacheEnabled } from "../internal.js" ;
@@ -31,25 +32,33 @@ type Options = {
3132 defaultLongLivedTtlSec ?: number ;
3233
3334 /**
34- * Whether the regional cache entry should be updated in the background or not when it experiences
35- * a cache hit.
35+ * Whether the regional cache entry should be updated in the background on regional cache hits.
3636 *
37- * @default `true` in `long-lived` mode when cache purge is not used, `false` otherwise.
37+ * NOTE: Use the default value unless you know what you are doing. It is set to:
38+ * - Next < 16:
39+ * `true` in `long-lived` mode when cache purge is not used, `false` otherwise.
40+ * - Next >= 16:
41+ * `!bypassTagCacheOnCacheHit`
3842 */
3943 shouldLazilyUpdateOnCacheHit ?: boolean ;
4044
4145 /**
42- * Whether on cache hits the tagCache should be skipped or not. Skipping the tagCache allows requests to be
43- * handled faster,
46+ * Whether the tagCache should be skipped on regional cache hits.
4447 *
45- * Note: When this is enabled, make sure that the cache gets purged
46- * either by enabling the auto cache purging feature or manually.
48+ * Note:
49+ * - Skipping the tagCache allows requests to be handled faster
50+ * - When `true`, make sure the cache gets purged
51+ * either by enabling the auto cache purging feature or manually
4752 *
48- * This is currently not compatible with swr types of revalidateTag (i.e. on Next 16+, anything different than `revalidateTag("tag", { expire: 0 })`),
49- * unless you also enable the `shouldLazilyUpdateOnCacheHit` option to make sure the cache gets updated in the background after a hit,
50- * and ONLY use it for pages, not data cache entries .
53+ * `true` is not compatible with SWR types of revalidateTag
54+ * i.e. on Next 16+, anything different than `revalidateTag("tag", { expire: 0 })`.
55+ * That's why the default is `false` for Next 16+ which uses SWR by default .
5156 *
52- * @default `true` if the auto cache purging is enabled, `false` otherwise.
57+ * NOTE: Use the default value unless you know what you are doing. It is set to:
58+ * - Next <16:
59+ * `true` if the auto cache purging is enabled, `false` otherwise.
60+ * - Next >= 16:
61+ * `false`
5362 */
5463 bypassTagCacheOnCacheHit ?: boolean ;
5564} ;
@@ -82,17 +91,30 @@ class RegionalCache implements IncrementalCache {
8291 private opts : Options
8392 ) {
8493 this . name = this . store . name ;
85- // `shouldLazilyUpdateOnCacheHit` is not needed when cache purge is enabled.
86- this . opts . shouldLazilyUpdateOnCacheHit ??= this . opts . mode === "long-lived" && ! isPurgeCacheEnabled ( ) ;
87- }
8894
89- get #bypassTagCacheOnCacheHit( ) : boolean {
90- if ( this . opts . bypassTagCacheOnCacheHit !== undefined ) {
91- return this . opts . bypassTagCacheOnCacheHit ;
95+ const { nextVersion } = globalThis ;
96+
97+ if ( compareSemver ( nextVersion , "<" , "16" ) ) {
98+ // Next < 16
99+ this . opts . shouldLazilyUpdateOnCacheHit ??= this . opts . mode === "long-lived" && ! isPurgeCacheEnabled ( ) ;
100+ this . opts . bypassTagCacheOnCacheHit ??= isPurgeCacheEnabled ( ) ;
101+ } else {
102+ // Next >= 16
103+ this . opts . bypassTagCacheOnCacheHit ??= false ;
104+ if ( this . opts . bypassTagCacheOnCacheHit ) {
105+ debugCache (
106+ "RegionalCache" ,
107+ `bypassTagCacheOnCacheHit is not recommended for Next 16+ as it is not compatible with SWR tags. Make sure to always use \`revalidateTag\` with \`{ expire: 0 }\` if you want to bypass the tag cache.`
108+ ) ;
109+ }
110+ this . opts . shouldLazilyUpdateOnCacheHit ??= ! this . opts . bypassTagCacheOnCacheHit ;
111+ if ( this . opts . shouldLazilyUpdateOnCacheHit !== this . opts . bypassTagCacheOnCacheHit ) {
112+ debugCache (
113+ "RegionalCache" ,
114+ `\`shouldLazilyUpdateOnCacheHit\` and \`bypassTagCacheOnCacheHit\` are mutually exclusive for Next 16+.`
115+ ) ;
116+ }
92117 }
93-
94- // When `bypassTagCacheOnCacheHit` is not set, we default to whether the automatic cache purging is enabled or not
95- return isPurgeCacheEnabled ( ) ;
96118 }
97119
98120 async get < CacheType extends CacheEntryType = "cache" > (
@@ -127,7 +149,7 @@ class RegionalCache implements IncrementalCache {
127149
128150 return {
129151 ...responseJson ,
130- shouldBypassTagCache : this . # bypassTagCacheOnCacheHit,
152+ shouldBypassTagCache : this . opts . bypassTagCacheOnCacheHit ,
131153 } ;
132154 }
133155
0 commit comments