Skip to content

Commit fe96df4

Browse files
discard expired entries in the kvCache get
1 parent 642c3d2 commit fe96df4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

.changeset/spotty-baboons-rule.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+
Fix: make sure `kvCache` discards expired entries

packages/cloudflare/src/api/kvCache.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ export const CACHE_ASSET_DIR = "cnd-cgi/_next_cache";
77

88
export const STATUS_DELETED = 1;
99

10+
// https://github.com/vercel/next.js/blob/9a1cd356/packages/next/src/server/response-cache/types.ts#L26-L38
11+
export type CachedFetchValue = {
12+
kind: "FETCH";
13+
data: {
14+
headers: { [k: string]: string };
15+
body: string;
16+
url: string;
17+
status?: number;
18+
// field used by older versions of Next.js (see: https://github.com/vercel/next.js/blob/fda1ecc/packages/next/src/server/response-cache/types.ts#L23)
19+
tags?: string[];
20+
};
21+
// tags are only present with file-system-cache
22+
// fetch cache stores tags outside of cache entry
23+
tags?: string[];
24+
revalidate: number;
25+
};
26+
1027
/**
1128
* Open Next cache based on cloudflare KV and Assets.
1229
*
@@ -62,6 +79,17 @@ class Cache implements IncrementalCache {
6279
};
6380
}
6481
}
82+
83+
const entryValue = entry?.value as CachedFetchValue | undefined;
84+
if (entryValue?.kind === "FETCH") {
85+
const expires = entryValue.data.headers?.expires;
86+
const expiresTime = new Date(expires as string).getTime();
87+
if (!isNaN(expiresTime) && expiresTime <= Date.now()) {
88+
this.debug(`found expired entry (expire time: ${expires})`);
89+
return {};
90+
}
91+
}
92+
6593
this.debug(entry ? `-> hit` : `-> miss`);
6694
return { value: entry?.value, lastModified: entry?.lastModified };
6795
} catch {

0 commit comments

Comments
 (0)