Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cute-papayas-win.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

refactor: only try to purge the cache when invalidation is configured
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@opennextjs/aws/types/overrides.js";

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

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

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

// Otherwise we default to whether the automatic cache purging is enabled or not
return this.#hasAutomaticCachePurging;
}

get #hasAutomaticCachePurging() {
// The `?` is required at `openNextConfig?` or the Open Next build fails because of a type error
const cdnInvalidation = globalThis.openNextConfig?.default?.override?.cdnInvalidation;

return cdnInvalidation !== undefined && cdnInvalidation !== "dummy";
return isPurgeCacheEnabled();
}

async get<CacheType extends CacheEntryType = "cache">(
Expand Down
7 changes: 7 additions & 0 deletions packages/cloudflare/src/api/overrides/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export function computeCacheKey(key: string, options: KeyOptions) {
return `${prefix}/${buildId}/${hash}.${cacheType}`.replace(/\/+/g, "/");
}

export function isPurgeCacheEnabled(): boolean {
// The `?` is required at `openNextConfig?` or the Open Next build fails because of a type error
const cdnInvalidation = globalThis.openNextConfig?.default?.override?.cdnInvalidation;

return cdnInvalidation !== undefined && cdnInvalidation !== "dummy";
}

export async function purgeCacheByTags(tags: string[]) {
const { env } = getCloudflareContext();
// We have a durable object for purging cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ vi.mock("../internal.js", () => ({
debugCache: vi.fn(),
FALLBACK_BUILD_ID: "fallback-build-id",
purgeCacheByTags: vi.fn(),
isPurgeCacheEnabled: () => true,
}));

describe("D1NextModeTagCache", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { error } from "@opennextjs/aws/adapters/logger.js";
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";

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

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

Expand Down Expand Up @@ -67,7 +67,9 @@ export class D1NextModeTagCache implements NextModeTagCache {
);

// TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986
await purgeCacheByTags(tags);
if (isPurgeCacheEnabled()) {
await purgeCacheByTags(tags);
}
}

private getConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IgnorableError } from "@opennextjs/aws/utils/error.js";
import { getCloudflareContext } from "../../cloudflare-context.js";
import type { OpenNextConfig } from "../../config.js";
import { DOShardedTagCache } from "../../durable-objects/sharded-tag-cache.js";
import { debugCache, purgeCacheByTags } from "../internal.js";
import { debugCache, isPurgeCacheEnabled, purgeCacheByTags } from "../internal.js";

export const DEFAULT_WRITE_RETRIES = 3;
export const DEFAULT_NUM_SHARDS = 4;
Expand Down Expand Up @@ -229,7 +229,9 @@ class ShardedDOTagCache implements NextModeTagCache {
);

// TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986
await purgeCacheByTags(tags);
if (isPurgeCacheEnabled()) {
await purgeCacheByTags(tags);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ vi.mock("../internal.js", () => ({
debugCache: vi.fn(),
FALLBACK_BUILD_ID: "fallback-build-id",
purgeCacheByTags: vi.fn(),
isPurgeCacheEnabled: () => true,
}));

describe("KVNextModeTagCache", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { error } from "@opennextjs/aws/adapters/logger.js";
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";

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

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

Expand Down Expand Up @@ -65,7 +65,9 @@ export class KVNextModeTagCache implements NextModeTagCache {
);

// TODO: See https://github.com/opennextjs/opennextjs-aws/issues/986
await purgeCacheByTags(tags);
if (isPurgeCacheEnabled()) {
await purgeCacheByTags(tags);
}
}

/**
Expand Down