Skip to content

Commit 196a205

Browse files
committed
feat: replace NEXT_BUILD_ID with OPEN_NEXT_BUILD_ID
1 parent 585795d commit 196a205

11 files changed

Lines changed: 37 additions & 26 deletions

File tree

.changeset/funny-toes-exist.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+
Prefer `OPEN_NEXT_BUILD_ID` for Cloudflare cache keys, populating it from Next `deploymentId` with the build ID as a fallback.

packages/cloudflare/src/api/overrides/incremental-cache/kv-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class KVIncrementalCache implements IncrementalCache {
102102
protected getKVKey(key: string, cacheType?: CacheEntryType): string {
103103
return computeCacheKey(key, {
104104
prefix: getCloudflareContext().env[PREFIX_ENV_NAME],
105-
buildId: process.env.NEXT_BUILD_ID,
105+
buildId: process.env.OPEN_NEXT_BUILD_ID,
106106
cacheType,
107107
});
108108
}

packages/cloudflare/src/api/overrides/incremental-cache/r2-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class R2IncrementalCache implements IncrementalCache {
8282
protected getR2Key(key: string, cacheType?: CacheEntryType): string {
8383
return computeCacheKey(key, {
8484
prefix: getCloudflareContext().env[PREFIX_ENV_NAME],
85-
buildId: process.env.NEXT_BUILD_ID,
85+
buildId: process.env.OPEN_NEXT_BUILD_ID,
8686
cacheType,
8787
});
8888
}

packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class RegionalCache implements IncrementalCache {
190190
}
191191

192192
protected getCacheUrlKey(key: string, cacheType?: CacheEntryType) {
193-
const buildId = process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
193+
const buildId = process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
194194
return "http://cache.local" + `/${buildId}/${key}`.replace(/\/+/g, "/") + `.${cacheType ?? "cache"}`;
195195
}
196196

packages/cloudflare/src/api/overrides/incremental-cache/static-assets-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class StaticAssetsIncrementalCache implements IncrementalCache {
6565
if (cacheType === "composable") {
6666
throw new Error("Composable cache is not supported in static assets incremental cache");
6767
}
68-
const buildId = process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
68+
const buildId = process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
6969
const name = (
7070
cacheType === "fetch"
7171
? `${CACHE_DIR}/__fetch/${buildId}/${key}`

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ describe("D1NextModeTagCache", () => {
143143
expect(error).toHaveBeenCalledWith(mockError);
144144
});
145145

146-
it("should use custom build ID when NEXT_BUILD_ID is set", async () => {
146+
it("should prefer OPEN_NEXT_BUILD_ID when it is set", async () => {
147147
const customBuildId = "custom-build-id";
148-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
148+
vi.stubEnv("NEXT_BUILD_ID", "legacy-build-id");
149+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
149150

150151
mockRaw.mockResolvedValue([[`${customBuildId}/tag1`, 123, 123, null]]);
151152

@@ -534,9 +535,9 @@ describe("D1NextModeTagCache", () => {
534535
expect(cacheKey).toBe(`${FALLBACK_BUILD_ID}/${key}`);
535536
});
536537

537-
it("should use custom build ID when NEXT_BUILD_ID is set", () => {
538+
it("should use custom build ID when OPEN_NEXT_BUILD_ID is set", () => {
538539
const customBuildId = "custom-build-id";
539-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
540+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
540541

541542
const key = "test-tag";
542543
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -545,7 +546,7 @@ describe("D1NextModeTagCache", () => {
545546
});
546547

547548
it("should handle double slashes by replacing them with single slash", () => {
548-
vi.stubEnv("NEXT_BUILD_ID", "build//id");
549+
vi.stubEnv("OPEN_NEXT_BUILD_ID", "build//id");
549550

550551
const key = "test-tag";
551552
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -555,16 +556,16 @@ describe("D1NextModeTagCache", () => {
555556
});
556557

557558
describe("getBuildId", () => {
558-
it("should return NEXT_BUILD_ID when set", () => {
559+
it("should return OPEN_NEXT_BUILD_ID when set", () => {
559560
const customBuildId = "custom-build-id";
560-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
561+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
561562

562563
const buildId = (tagCache as unknown as { getBuildId: () => string }).getBuildId();
563564

564565
expect(buildId).toBe(customBuildId);
565566
});
566567

567-
it("should return fallback build ID when NEXT_BUILD_ID is not set", () => {
568+
it("should return fallback build ID when no build ID env vars are set", () => {
568569
const buildId = (tagCache as unknown as { getBuildId: () => string }).getBuildId();
569570

570571
expect(buildId).toBe(FALLBACK_BUILD_ID);

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class D1NextModeTagCache implements NextModeTagCache {
190190
}
191191

192192
protected getBuildId() {
193-
return process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
193+
return process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
194194
}
195195

196196
/**

packages/cloudflare/src/api/overrides/tag-cache/kv-next-tag-cache.spec.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ describe("KVNextModeTagCache", () => {
126126
expect(error).toHaveBeenCalledWith(mockError);
127127
});
128128

129-
it("should use custom build ID when NEXT_BUILD_ID is set", async () => {
129+
it("should prefer OPEN_NEXT_BUILD_ID when it is set", async () => {
130130
const customBuildId = "custom-build-id";
131-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
131+
vi.stubEnv("NEXT_BUILD_ID", "legacy-build-id");
132+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
132133

133134
mockGet.mockResolvedValue(new Map([["tag1", null]]));
134135

@@ -393,9 +394,9 @@ describe("KVNextModeTagCache", () => {
393394
expect(cacheKey).toBe(`${FALLBACK_BUILD_ID}/${key}`);
394395
});
395396

396-
it("should use custom build ID when NEXT_BUILD_ID is set", () => {
397+
it("should use custom build ID when OPEN_NEXT_BUILD_ID is set", () => {
397398
const customBuildId = "custom-build-id";
398-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
399+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
399400

400401
const key = "test-tag";
401402
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -404,7 +405,7 @@ describe("KVNextModeTagCache", () => {
404405
});
405406

406407
it("should handle double slashes by replacing them with single slash", () => {
407-
vi.stubEnv("NEXT_BUILD_ID", "build//id");
408+
vi.stubEnv("OPEN_NEXT_BUILD_ID", "build//id");
408409

409410
const key = "test-tag";
410411
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -414,18 +415,16 @@ describe("KVNextModeTagCache", () => {
414415
});
415416

416417
describe("getBuildId", () => {
417-
it("should return NEXT_BUILD_ID when set", () => {
418+
it("should return OPEN_NEXT_BUILD_ID when set", () => {
418419
const customBuildId = "custom-build-id";
419-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
420+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
420421

421422
const buildId = (tagCache as unknown as { getBuildId: () => string }).getBuildId();
422423

423424
expect(buildId).toBe(customBuildId);
424425
});
425426

426-
it("should return fallback build ID when NEXT_BUILD_ID is not set", () => {
427-
// Environment variables are cleared by vi.unstubAllEnvs() in beforeEach
428-
427+
it("should return fallback build ID when no build ID env vars are set", () => {
429428
const buildId = (tagCache as unknown as { getBuildId: () => string }).getBuildId();
430429

431430
expect(buildId).toBe(FALLBACK_BUILD_ID);

packages/cloudflare/src/api/overrides/tag-cache/kv-next-tag-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export class KVNextModeTagCache implements NextModeTagCache {
216216
}
217217

218218
protected getBuildId() {
219-
return process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
219+
return process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
220220
}
221221

222222
/**

packages/cloudflare/src/cli/build/open-next/compile-init.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "node:path";
22
import { fileURLToPath } from "node:url";
33

4-
import { loadConfig } from "@opennextjs/aws/adapters/config/util.js";
4+
import { loadBuildId, loadConfig } from "@opennextjs/aws/adapters/config/util.js";
55
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
66
import { build } from "esbuild";
77
import type { Unstable_Config } from "wrangler";
@@ -13,10 +13,12 @@ export async function compileInit(options: BuildOptions, wranglerConfig: Unstabl
1313
const currentDir = path.join(path.dirname(fileURLToPath(import.meta.url)));
1414
const templatesDir = path.join(currentDir, "../../templates");
1515
const initPath = path.join(templatesDir, "init.js");
16+
const dotNextDir = path.join(options.appBuildOutputPath, ".next");
1617

17-
const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
18+
const nextConfig = loadConfig(dotNextDir);
1819
const basePath = nextConfig.basePath ?? "";
1920
const deploymentId = nextConfig.deploymentId ?? "";
21+
const openNextBuildId = nextConfig.deploymentId ?? loadBuildId(dotNextDir);
2022
const trailingSlash = nextConfig.trailingSlash ?? false;
2123

2224
await build({
@@ -32,6 +34,7 @@ export async function compileInit(options: BuildOptions, wranglerConfig: Unstabl
3234
__NEXT_BASE_PATH__: JSON.stringify(basePath),
3335
__ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
3436
__DEPLOYMENT_ID__: JSON.stringify(deploymentId),
37+
__OPEN_NEXT_BUILD_ID__: JSON.stringify(openNextBuildId),
3538
__TRAILING_SLASH__: JSON.stringify(trailingSlash),
3639
},
3740
});

0 commit comments

Comments
 (0)