Skip to content

Commit 7148ae7

Browse files
committed
refactor: only try to purge the cache when invalidation is configured
1 parent dcc864d commit 7148ae7

File tree

8 files changed

+29
-17
lines changed

8 files changed

+29
-17
lines changed

.changeset/cute-papayas-win.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+
refactor: only try to purge the cache when invalidation is configured

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@opennextjs/aws/types/overrides.js";
88

99
import { getCloudflareContext } from "../../cloudflare-context.js";
10-
import { debugCache, FALLBACK_BUILD_ID, IncrementalCacheEntry } from "../internal.js";
10+
import { debugCache, FALLBACK_BUILD_ID, IncrementalCacheEntry, isPurgeCacheEnabled } from "../internal.js";
1111
import { NAME as KV_CACHE_NAME } from "./kv-incremental-cache.js";
1212

1313
const ONE_MINUTE_IN_SECONDS = 60;
@@ -82,8 +82,7 @@ class RegionalCache implements IncrementalCache {
8282
throw new Error("The KV incremental cache does not need a regional cache.");
8383
}
8484
this.name = this.store.name;
85-
this.opts.shouldLazilyUpdateOnCacheHit ??=
86-
this.opts.mode === "long-lived" && !this.#hasAutomaticCachePurging;
85+
this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived" && !isPurgeCacheEnabled();
8786
}
8887

8988
get #bypassTagCacheOnCacheHit(): boolean {
@@ -93,14 +92,7 @@ class RegionalCache implements IncrementalCache {
9392
}
9493

9594
// Otherwise we default to whether the automatic cache purging is enabled or not
96-
return this.#hasAutomaticCachePurging;
97-
}
98-
99-
get #hasAutomaticCachePurging() {
100-
// The `?` is required at `openNextConfig?` or the Open Next build fails because of a type error
101-
const cdnInvalidation = globalThis.openNextConfig?.default?.override?.cdnInvalidation;
102-
103-
return cdnInvalidation !== undefined && cdnInvalidation !== "dummy";
95+
return isPurgeCacheEnabled();
10496
}
10597

10698
async get<CacheType extends CacheEntryType = "cache">(

packages/cloudflare/src/api/overrides/internal.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export function computeCacheKey(key: string, options: KeyOptions) {
3232
return `${prefix}/${buildId}/${hash}.${cacheType}`.replace(/\/+/g, "/");
3333
}
3434

35+
export function isPurgeCacheEnabled(): boolean {
36+
// The `?` is required at `openNextConfig?` or the Open Next build fails because of a type error
37+
const cdnInvalidation = globalThis.openNextConfig?.default?.override?.cdnInvalidation;
38+
39+
return cdnInvalidation !== undefined && cdnInvalidation !== "dummy";
40+
}
41+
3542
export async function purgeCacheByTags(tags: string[]) {
3643
const { env } = getCloudflareContext();
3744
// We have a durable object for purging cache

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ vi.mock("../internal.js", () => ({
2121
debugCache: vi.fn(),
2222
FALLBACK_BUILD_ID: "fallback-build-id",
2323
purgeCacheByTags: vi.fn(),
24+
isPurgeCacheEnabled: () => true,
2425
}));
2526

2627
describe("D1NextModeTagCache", () => {

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { error } from "@opennextjs/aws/adapters/logger.js";
22
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
33

44
import { getCloudflareContext } from "../../cloudflare-context.js";
5-
import { debugCache, FALLBACK_BUILD_ID, purgeCacheByTags } from "../internal.js";
5+
import { debugCache, FALLBACK_BUILD_ID, isPurgeCacheEnabled, purgeCacheByTags } from "../internal.js";
66

77
export const NAME = "d1-next-mode-tag-cache";
88

@@ -67,7 +67,9 @@ export class D1NextModeTagCache implements NextModeTagCache {
6767
);
6868

6969
// TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986
70-
await purgeCacheByTags(tags);
70+
if (isPurgeCacheEnabled()) {
71+
await purgeCacheByTags(tags);
72+
}
7173
}
7274

7375
private getConfig() {

packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { IgnorableError } from "@opennextjs/aws/utils/error.js";
66
import { getCloudflareContext } from "../../cloudflare-context.js";
77
import type { OpenNextConfig } from "../../config.js";
88
import { DOShardedTagCache } from "../../durable-objects/sharded-tag-cache.js";
9-
import { debugCache, purgeCacheByTags } from "../internal.js";
9+
import { debugCache, isPurgeCacheEnabled, purgeCacheByTags } from "../internal.js";
1010

1111
export const DEFAULT_WRITE_RETRIES = 3;
1212
export const DEFAULT_NUM_SHARDS = 4;
@@ -229,7 +229,9 @@ class ShardedDOTagCache implements NextModeTagCache {
229229
);
230230

231231
// TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986
232-
await purgeCacheByTags(tags);
232+
if (isPurgeCacheEnabled()) {
233+
await purgeCacheByTags(tags);
234+
}
233235
}
234236

235237
/**

packages/cloudflare/src/api/overrides/tag-cache/kv-next-tag-cache.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ vi.mock("../internal.js", () => ({
1818
debugCache: vi.fn(),
1919
FALLBACK_BUILD_ID: "fallback-build-id",
2020
purgeCacheByTags: vi.fn(),
21+
isPurgeCacheEnabled: () => true,
2122
}));
2223

2324
describe("KVNextModeTagCache", () => {

packages/cloudflare/src/api/overrides/tag-cache/kv-next-tag-cache.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { error } from "@opennextjs/aws/adapters/logger.js";
22
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
33

44
import { getCloudflareContext } from "../../cloudflare-context.js";
5-
import { FALLBACK_BUILD_ID, purgeCacheByTags } from "../internal.js";
5+
import { FALLBACK_BUILD_ID, isPurgeCacheEnabled, purgeCacheByTags } from "../internal.js";
66

77
export const NAME = "kv-next-mode-tag-cache";
88

@@ -65,7 +65,9 @@ export class KVNextModeTagCache implements NextModeTagCache {
6565
);
6666

6767
// TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986
68-
await purgeCacheByTags(tags);
68+
if (isPurgeCacheEnabled()) {
69+
await purgeCacheByTags(tags);
70+
}
6971
}
7072

7173
/**

0 commit comments

Comments
 (0)