Skip to content

Commit f5fe6d3

Browse files
committed
fixup! delete, lastModified
1 parent 082ab4d commit f5fe6d3

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

packages/cloudflare/src/api/kvCache.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getCloudflareContext } from "./get-cloudflare-context.js";
88

99
export const CACHE_ASSET_DIR = "cnd-cgi/_next_cache";
1010

11+
export const STATUS_DELETED = 1;
12+
1113
/**
1214
* Open Next cache based on cloudflare KV and Assets.
1315
*
@@ -38,26 +40,32 @@ class Cache implements IncrementalCache {
3840
try {
3941
this.debug(`- From KV`);
4042
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
41-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
42-
let value: any = await this.kv?.get(kvKey, "json");
43-
if (!value && this.assets) {
43+
44+
let entry = (await this.kv?.get(kvKey, "json")) as {
45+
value?: CacheValue<IsFetch>;
46+
lastModified?: number;
47+
status?: number;
48+
} | null;
49+
if (entry?.status === STATUS_DELETED) {
50+
return {};
51+
}
52+
if (!entry && this.assets) {
4453
const url = this.getAssetUrl(key);
4554
const response = await this.assets.fetch(url);
4655
this.debug(`- From Assets`);
4756
if (response.ok) {
48-
value = await response.json();
57+
entry = {
58+
value: await response.json(),
59+
// __BUILD_TIMESTAMP_MS__ is injected by ESBuild.
60+
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
61+
};
4962
}
5063
}
51-
if (value) {
52-
this.debug(`-> hit`);
53-
return { value };
54-
}
64+
this.debug(entry ? `-> hit` : `-> miss`);
65+
return { value: entry?.value, lastModified: entry?.lastModified };
5566
} catch {
5667
throw new RecoverableError(`Failed to get cache [${key}]`);
5768
}
58-
59-
this.debug(`-> miss`);
60-
throw new RecoverableError(`Not found [${key}]`);
6169
}
6270

6371
async set<IsFetch extends boolean = false>(
@@ -74,8 +82,16 @@ class Cache implements IncrementalCache {
7482
this.debug(`Set ${key}`);
7583
try {
7684
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
77-
// TODO: add TTL to avoid cache growing too big ?
78-
await this.kv.put(kvKey, JSON.stringify(value));
85+
// Note: We can not set a TTL as we might fallback to assets,
86+
// still removing old data (old BUILD_ID) could help avoiding
87+
// the cache growing too big.
88+
await this.kv.put(
89+
kvKey,
90+
JSON.stringify({
91+
value,
92+
lastModified: Date.now(),
93+
})
94+
);
7995
} catch {
8096
throw new RecoverableError(`Failed to set cache [${key}]`);
8197
}
@@ -91,7 +107,8 @@ class Cache implements IncrementalCache {
91107
this.debug(`Delete ${key}`);
92108
try {
93109
const kvKey = this.getKVKey(key, "cache");
94-
await this.kv.delete(kvKey);
110+
// Do not delete the key as we will then fallback to the assets.
111+
await this.kv.put(kvKey, JSON.stringify({ status: STATUS_DELETED }));
95112
} catch (e) {
96113
throw new RecoverableError(`Failed to delete cache [${key}]`);
97114
}

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export async function bundleServer(config: Config, openNextOptions: BuildOptions
6969
"process.env.NEXT_RUNTIME": '"nodejs"',
7070
"process.env.NODE_ENV": '"production"',
7171
"process.env.NEXT_MINIMAL": "true",
72+
"process.env.__BUILD_TIMESTAMP_MS__": String(Date.now()),
7273
},
7374
// We need to set platform to node so that esbuild doesn't complain about the node imports
7475
platform: "node",
@@ -109,6 +110,7 @@ globalThis.Request = CustomRequest;
109110
Request = globalThis.Request;
110111
// Makes the edge converter returns either a Response or a Request.
111112
globalThis.__dangerous_ON_edge_converter_returns_request = true;
113+
globalThis.__BUILD_TIMESTAMP_MS__ = ${Date.now()};
112114
`,
113115
},
114116
});

packages/cloudflare/src/cli/config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const PACKAGE_NAME = "@opennextjs/cloudflare";
55

66
export type Config = {
77
build: {
8-
// Timestamp for when the build was started
9-
timestamp: number;
108
// Whether to skip building the Next.js app or not
119
skipNextBuild: boolean;
1210
// Whether minification should be enabled or not
@@ -72,7 +70,6 @@ export function getConfig(projectOpts: ProjectOptions): Config {
7270

7371
return {
7472
build: {
75-
timestamp: Date.now(),
7673
skipNextBuild: projectOpts.skipNextBuild,
7774
shouldMinify: projectOpts.minify,
7875
},

0 commit comments

Comments
 (0)