this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: only try to purge the cache when invalidation is configured (#907)

authored by

Victor Berchet and committed by
GitHub
ba4cac5f dcc864d4

+29 -17
+5
.changeset/cute-papayas-win.md
··· 1 + --- 2 + "@opennextjs/cloudflare": patch 3 + --- 4 + 5 + refactor: only try to purge the cache when invalidation is configured
+3 -11
packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts
··· 7 7 } from "@opennextjs/aws/types/overrides.js"; 8 8 9 9 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"; 11 11 import { NAME as KV_CACHE_NAME } from "./kv-incremental-cache.js"; 12 12 13 13 const ONE_MINUTE_IN_SECONDS = 60; ··· 82 82 throw new Error("The KV incremental cache does not need a regional cache."); 83 83 } 84 84 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(); 87 86 } 88 87 89 88 get #bypassTagCacheOnCacheHit(): boolean { ··· 93 92 } 94 93 95 94 // 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(); 104 96 } 105 97 106 98 async get<CacheType extends CacheEntryType = "cache">(
+7
packages/cloudflare/src/api/overrides/internal.ts
··· 32 32 return `${prefix}/${buildId}/${hash}.${cacheType}`.replace(/\/+/g, "/"); 33 33 } 34 34 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 + 35 42 export async function purgeCacheByTags(tags: string[]) { 36 43 const { env } = getCloudflareContext(); 37 44 // We have a durable object for purging cache
+1
packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.spec.ts
··· 21 21 debugCache: vi.fn(), 22 22 FALLBACK_BUILD_ID: "fallback-build-id", 23 23 purgeCacheByTags: vi.fn(), 24 + isPurgeCacheEnabled: () => true, 24 25 })); 25 26 26 27 describe("D1NextModeTagCache", () => {
+4 -2
packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts
··· 2 2 import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js"; 3 3 4 4 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"; 6 6 7 7 export const NAME = "d1-next-mode-tag-cache"; 8 8 ··· 67 67 ); 68 68 69 69 // TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986 70 - await purgeCacheByTags(tags); 70 + if (isPurgeCacheEnabled()) { 71 + await purgeCacheByTags(tags); 72 + } 71 73 } 72 74 73 75 private getConfig() {
+4 -2
packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts
··· 6 6 import { getCloudflareContext } from "../../cloudflare-context.js"; 7 7 import type { OpenNextConfig } from "../../config.js"; 8 8 import { DOShardedTagCache } from "../../durable-objects/sharded-tag-cache.js"; 9 - import { debugCache, purgeCacheByTags } from "../internal.js"; 9 + import { debugCache, isPurgeCacheEnabled, purgeCacheByTags } from "../internal.js"; 10 10 11 11 export const DEFAULT_WRITE_RETRIES = 3; 12 12 export const DEFAULT_NUM_SHARDS = 4; ··· 229 229 ); 230 230 231 231 // TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986 232 - await purgeCacheByTags(tags); 232 + if (isPurgeCacheEnabled()) { 233 + await purgeCacheByTags(tags); 234 + } 233 235 } 234 236 235 237 /**
+1
packages/cloudflare/src/api/overrides/tag-cache/kv-next-tag-cache.spec.ts
··· 18 18 debugCache: vi.fn(), 19 19 FALLBACK_BUILD_ID: "fallback-build-id", 20 20 purgeCacheByTags: vi.fn(), 21 + isPurgeCacheEnabled: () => true, 21 22 })); 22 23 23 24 describe("KVNextModeTagCache", () => {
+4 -2
packages/cloudflare/src/api/overrides/tag-cache/kv-next-tag-cache.ts
··· 2 2 import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js"; 3 3 4 4 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"; 6 6 7 7 export const NAME = "kv-next-mode-tag-cache"; 8 8 ··· 65 65 ); 66 66 67 67 // TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986 68 - await purgeCacheByTags(tags); 68 + if (isPurgeCacheEnabled()) { 69 + await purgeCacheByTags(tags); 70 + } 69 71 } 70 72 71 73 /**