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,21 +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- * @default `true` if the auto cache purging is enabled, `false` otherwise.
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.
56+ *
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`
4962 */
5063 bypassTagCacheOnCacheHit ?: boolean ;
5164} ;
@@ -78,17 +91,33 @@ class RegionalCache implements IncrementalCache {
7891 private opts : Options
7992 ) {
8093 this . name = this . store . name ;
81- // `shouldLazilyUpdateOnCacheHit` is not needed when cache purge is enabled.
82- this . opts . shouldLazilyUpdateOnCacheHit ??= this . opts . mode === "long-lived" && ! isPurgeCacheEnabled ( ) ;
83- }
8494
85- get #bypassTagCacheOnCacheHit( ) : boolean {
86- if ( this . opts . bypassTagCacheOnCacheHit !== undefined ) {
87- return this . opts . bypassTagCacheOnCacheHit ;
95+ // `globalThis.nextVersion` is only defined at runtime but not when the Open Next build runs.
96+ // The options do no matter at build time so we can skip setting them.
97+ const { nextVersion } = globalThis ;
98+ if ( nextVersion ) {
99+ if ( compareSemver ( nextVersion , "<" , "16" ) ) {
100+ // Next < 16
101+ this . opts . shouldLazilyUpdateOnCacheHit ??= this . opts . mode === "long-lived" && ! isPurgeCacheEnabled ( ) ;
102+ this . opts . bypassTagCacheOnCacheHit ??= isPurgeCacheEnabled ( ) ;
103+ } else {
104+ // Next >= 16
105+ this . opts . bypassTagCacheOnCacheHit ??= false ;
106+ if ( this . opts . bypassTagCacheOnCacheHit ) {
107+ debugCache (
108+ "RegionalCache" ,
109+ `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.`
110+ ) ;
111+ }
112+ this . opts . shouldLazilyUpdateOnCacheHit ??= ! this . opts . bypassTagCacheOnCacheHit ;
113+ if ( this . opts . shouldLazilyUpdateOnCacheHit !== this . opts . bypassTagCacheOnCacheHit ) {
114+ debugCache (
115+ "RegionalCache" ,
116+ `\`shouldLazilyUpdateOnCacheHit\` and \`bypassTagCacheOnCacheHit\` are mutually exclusive for Next 16+.`
117+ ) ;
118+ }
119+ }
88120 }
89-
90- // When `bypassTagCacheOnCacheHit` is not set, we default to whether the automatic cache purging is enabled or not
91- return isPurgeCacheEnabled ( ) ;
92121 }
93122
94123 async get < CacheType extends CacheEntryType = "cache" > (
@@ -123,7 +152,7 @@ class RegionalCache implements IncrementalCache {
123152
124153 return {
125154 ...responseJson ,
126- shouldBypassTagCache : this . # bypassTagCacheOnCacheHit,
155+ shouldBypassTagCache : this . opts . bypassTagCacheOnCacheHit ,
127156 } ;
128157 }
129158
0 commit comments