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/fluffy-taxis-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

bump `@opennextjs/aws` dependency to `https://pkg.pr.new/@opennextjs/aws@727`
5 changes: 5 additions & 0 deletions .changeset/spotty-baboons-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

Fix: make sure that the kvCache doesn't serve stale cache values from assets when there is no KV binding
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"dependencies": {
"@ast-grep/napi": "^0.34.1",
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@724",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@727",
"enquirer": "^2.4.1",
"glob": "catalog:",
"ts-morph": "catalog:",
Expand Down
22 changes: 20 additions & 2 deletions packages/cloudflare/src/api/kvCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Cache implements IncrementalCache {
async get<IsFetch extends boolean = false>(
key: string,
isFetch?: IsFetch
): Promise<WithLastModified<CacheValue<IsFetch>>> {
): Promise<WithLastModified<CacheValue<IsFetch>> | null> {
const cfEnv = getCloudflareContext().env;
const kv = cfEnv.NEXT_CACHE_WORKERS_KV;
const assets = cfEnv.ASSETS;
Expand All @@ -43,7 +43,7 @@ class Cache implements IncrementalCache {
const kvKey = this.getKVKey(key, isFetch);
entry = await kv.get(kvKey, "json");
if (entry?.status === STATUS_DELETED) {
return {};
return null;
}
}

Expand All @@ -61,7 +61,25 @@ class Cache implements IncrementalCache {
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
};
}
if (!kv) {
// The cache can not be updated when there is no KV
// As we don't want to keep serving stale data for ever,
// we pretend the entry is not in cache
if (
entry?.value &&
"kind" in entry.value &&
entry.value.kind === "FETCH" &&
entry.value.data?.headers?.expires
) {
const expiresTime = new Date(entry.value.data.headers.expires).getTime();
if (!isNaN(expiresTime) && expiresTime <= Date.now()) {
this.debug(`found expired entry (expire time: ${entry.value.data.headers.expires})`);
return null;
}
}
}
}

this.debug(entry ? `-> hit` : `-> miss`);
return { value: entry?.value, lastModified: entry?.lastModified };
} catch {
Expand Down
Loading