Skip to content

Commit 4e633a6

Browse files
committed
fixup! default values depend on version + comment
1 parent 619c4aa commit 4e633a6

4 files changed

Lines changed: 914 additions & 605 deletions

File tree

examples/e2e/app-router/open-next.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cac
33
import shardedTagCache from "@opennextjs/cloudflare/overrides/tag-cache/do-sharded-tag-cache";
44
import doQueue from "@opennextjs/cloudflare/overrides/queue/do-queue";
55
import queueCache from "@opennextjs/cloudflare/overrides/queue/queue-cache";
6+
import { withRegionalCache } from "@opennextjs/cloudflare/overrides/incremental-cache/regional-cache";
67

78
const baseConfig = defineCloudflareConfig({
8-
incrementalCache: r2IncrementalCache,
9+
incrementalCache: withRegionalCache(r2IncrementalCache, { mode: "long-lived" }),
910
// With such a configuration, we could have up to 12 * (8 + 2) = 120 Durable Objects instances
1011
tagCache: shardedTagCache({
1112
baseShardSize: 12,

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"dependencies": {
5555
"@ast-grep/napi": "^0.40.5",
5656
"@dotenvx/dotenvx": "catalog:",
57-
"@opennextjs/aws": "3.10.2",
57+
"@opennextjs/aws": "3.10.3",
5858
"ci-info": "^4.2.0",
5959
"cloudflare": "^4.4.1",
6060
"comment-json": "^4.5.1",

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

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
IncrementalCache,
66
WithLastModified,
77
} from "@opennextjs/aws/types/overrides.js";
8+
import { compareSemver } from "@opennextjs/aws/utils/semver.js";
89

910
import { getCloudflareContext } from "../../cloudflare-context.js";
1011
import { 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

Comments
 (0)