Skip to content

Commit 2c1de0b

Browse files
authored
Log cache purge errors as errors (#867)
1 parent 38d462b commit 2c1de0b

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

.changeset/good-eyes-jog.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+
Log cache purge errors as errors

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default defineCloudflareConfig({
2020
},
2121
},
2222
}),
23-
cachePurge: purgeCache({ type: "durableObject" }),
23+
// `CACHE_PURGE_ZONE_ID` and `CACHE_PURGE_API_TOKEN` are required to enable cache purge
24+
// cachePurge: purgeCache({ type: "durableObject" }),
2425
enableCacheInterception: true,
2526
queue: queueCache(doQueue),
2627
});

packages/cloudflare/src/api/cloudflare-context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ declare global {
5656
// This can be safely used if you don't use an eventually consistent incremental cache (i.e. R2 without the regional cache for example)
5757
NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE?: string;
5858

59-
// Below are the optional env variables for purging the cache
60-
// Durable Object namespace to use for the durable object cache purge
59+
// Below are the env variables to use for purging the cache
60+
// Durable Object namespace to use for the durable object cache purge (not needed in direct mode)
6161
NEXT_CACHE_DO_PURGE?: DurableObjectNamespace<BucketCachePurge>;
62-
// The amount of time in seconds that the cache purge will wait before purging the cache
62+
// The amount of time in seconds that the cache purge will wait before purging the cache (not needed in direct mode)
6363
NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS?: string;
6464
// The zone ID to use for the cache purge https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/
6565
CACHE_PURGE_ZONE_ID?: string;

packages/cloudflare/src/api/overrides/cache-purge/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
12
import type { CDNInvalidationHandler } from "@opennextjs/aws/types/overrides.js";
23

34
import { getCloudflareContext } from "../../cloudflare-context.js";
@@ -19,7 +20,7 @@ export const purgeCache = ({ type = "direct" }: PurgeOptions) => {
1920
} else {
2021
const durableObject = env.NEXT_CACHE_DO_PURGE;
2122
if (!durableObject) {
22-
debugCache("cdnInvalidation", "No durable object found. Skipping cache purge.");
23+
error("Purge cache: NEXT_CACHE_DO_PURGE not found. Skipping cache purge.");
2324
return;
2425
}
2526
const id = durableObject.idFromName("cache-purge");

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createHash } from "node:crypto";
22

3+
import { error } from "@opennextjs/aws/adapters/logger.js";
34
import type { CacheEntryType, CacheValue } from "@opennextjs/aws/types/overrides.js";
45

56
import { getCloudflareContext } from "../cloudflare-context.js";
@@ -50,7 +51,7 @@ export async function purgeCacheByTags(tags: string[]) {
5051
export async function internalPurgeCacheByTags(env: CloudflareEnv, tags: string[]) {
5152
if (!env.CACHE_PURGE_ZONE_ID && !env.CACHE_PURGE_API_TOKEN) {
5253
// THIS IS A NO-OP
53-
debugCache("purgeCacheByTags", "No cache zone ID or API token provided. Skipping cache purge.");
54+
error("No cache zone ID or API token provided. Skipping cache purge.");
5455
return "missing-credentials";
5556
}
5657

@@ -71,17 +72,16 @@ export async function internalPurgeCacheByTags(env: CloudflareEnv, tags: string[
7172
);
7273
if (response.status === 429) {
7374
// Rate limit exceeded
74-
debugCache("purgeCacheByTags", "Rate limit exceeded. Skipping cache purge.");
75+
error("purgeCacheByTags: Rate limit exceeded. Skipping cache purge.");
7576
return "rate-limit-exceeded";
7677
}
7778
const bodyResponse = (await response.json()) as {
7879
success: boolean;
7980
errors: Array<{ code: number; message: string }>;
8081
};
8182
if (!bodyResponse.success) {
82-
debugCache(
83-
"purgeCacheByTags",
84-
"Cache purge failed. Errors:",
83+
error(
84+
"purgeCacheByTags: Cache purge failed. Errors:",
8585
bodyResponse.errors.map((error) => `${error.code}: ${error.message}`)
8686
);
8787
return "purge-failed";

0 commit comments

Comments
 (0)