Skip to content

Commit f30a5fe

Browse files
dario-piotrowiczvicb
andauthoredFeb 7, 2025··
Do not serve stale values from the assets when there is no KV (#344)
Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 4cd819b commit f30a5fe

File tree

5 files changed

+68
-51
lines changed

5 files changed

+68
-51
lines changed
 

Diff for: ‎.changeset/fluffy-taxis-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
bump `@opennextjs/aws` dependency to `https://pkg.pr.new/@opennextjs/aws@727`

Diff for: ‎.changeset/spotty-baboons-rule.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
Fix: make sure that the kvCache doesn't serve stale cache values from assets when there is no KV binding

Diff for: ‎packages/cloudflare/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"dependencies": {
7474
"@ast-grep/napi": "^0.34.1",
7575
"@dotenvx/dotenvx": "catalog:",
76-
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@724",
76+
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@727",
7777
"enquirer": "^2.4.1",
7878
"glob": "catalog:",
7979
"ts-morph": "catalog:",

Diff for: ‎packages/cloudflare/src/api/kvCache.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Cache implements IncrementalCache {
2020
async get<IsFetch extends boolean = false>(
2121
key: string,
2222
isFetch?: IsFetch
23-
): Promise<WithLastModified<CacheValue<IsFetch>>> {
23+
): Promise<WithLastModified<CacheValue<IsFetch>> | null> {
2424
const cfEnv = getCloudflareContext().env;
2525
const kv = cfEnv.NEXT_CACHE_WORKERS_KV;
2626
const assets = cfEnv.ASSETS;
@@ -43,7 +43,7 @@ class Cache implements IncrementalCache {
4343
const kvKey = this.getKVKey(key, isFetch);
4444
entry = await kv.get(kvKey, "json");
4545
if (entry?.status === STATUS_DELETED) {
46-
return {};
46+
return null;
4747
}
4848
}
4949

@@ -61,7 +61,25 @@ class Cache implements IncrementalCache {
6161
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
6262
};
6363
}
64+
if (!kv) {
65+
// The cache can not be updated when there is no KV
66+
// As we don't want to keep serving stale data for ever,
67+
// we pretend the entry is not in cache
68+
if (
69+
entry?.value &&
70+
"kind" in entry.value &&
71+
entry.value.kind === "FETCH" &&
72+
entry.value.data?.headers?.expires
73+
) {
74+
const expiresTime = new Date(entry.value.data.headers.expires).getTime();
75+
if (!isNaN(expiresTime) && expiresTime <= Date.now()) {
76+
this.debug(`found expired entry (expire time: ${entry.value.data.headers.expires})`);
77+
return null;
78+
}
79+
}
80+
}
6481
}
82+
6583
this.debug(entry ? `-> hit` : `-> miss`);
6684
return { value: entry?.value, lastModified: entry?.lastModified };
6785
} catch {

Diff for: ‎pnpm-lock.yaml

+37-48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.