Skip to content

Commit 2c5b472

Browse files
edmundhungvicb
andauthored
replace NEXT_BUILD_ID with OPEN_NEXT_BUILD_ID (#1208)
Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 1e8d232 commit 2c5b472

13 files changed

Lines changed: 150 additions & 140 deletions

File tree

.changeset/funny-toes-exist.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
Use `OPEN_NEXT_BUILD_ID` instead of `NEXT_BUILD_ID` in the cache keys.
6+
7+
As of Next 16.2 `NEXT_BUILD_ID` is a fixed value when deploymentId is set explicitly.
8+
9+
See <https://github.com/opennextjs/opennextjs-aws/pull/1144>

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"dependencies": {
5555
"@ast-grep/napi": "^0.40.5",
5656
"@dotenvx/dotenvx": "catalog:",
57-
"@opennextjs/aws": "3.10.3",
57+
"@opennextjs/aws": "3.10.4",
5858
"ci-info": "^4.2.0",
5959
"cloudflare": "^4.4.1",
6060
"comment-json": "^4.5.1",

packages/cloudflare/src/api/durable-objects/queue.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
165165
"INSERT OR REPLACE INTO sync (id, lastSuccess, buildId) VALUES (?, unixepoch(), ?)",
166166
// We cannot use the deduplication id because it's not unique per route - every time a route is revalidated, the deduplication id is different.
167167
`${host}${url}`,
168-
process.env.__NEXT_BUILD_ID
168+
process.env.__OPEN_NEXT_BUILD_ID
169169
);
170170
}
171171
// If everything went well, we can remove the route from the failed state
@@ -238,7 +238,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
238238
"INSERT OR REPLACE INTO failed_state (id, data, buildId) VALUES (?, ?, ?)",
239239
msg.MessageDeduplicationId,
240240
JSON.stringify(updatedFailedState),
241-
process.env.__NEXT_BUILD_ID
241+
process.env.__OPEN_NEXT_BUILD_ID
242242
);
243243
}
244244
// We probably want to do something if routeInFailedState is becoming too big, at least log it
@@ -273,8 +273,8 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
273273

274274
// Before doing anything else, we clear the DB for any potential old data
275275
// TODO: extract this to a function so that it could be called by the user at another time than init
276-
this.sql.exec("DELETE FROM failed_state WHERE buildId != ?", process.env.__NEXT_BUILD_ID);
277-
this.sql.exec("DELETE FROM sync WHERE buildId != ?", process.env.__NEXT_BUILD_ID);
276+
this.sql.exec("DELETE FROM failed_state WHERE buildId != ?", process.env.__OPEN_NEXT_BUILD_ID);
277+
this.sql.exec("DELETE FROM sync WHERE buildId != ?", process.env.__OPEN_NEXT_BUILD_ID);
278278

279279
const failedStateCursor = this.sql.exec<{ id: string; data: string }>("SELECT * FROM failed_state");
280280
for (const row of failedStateCursor) {

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
@@ -219,7 +219,7 @@ class RegionalCache implements IncrementalCache {
219219
}
220220

221221
protected getCacheUrlKey(key: string, cacheType?: CacheEntryType) {
222-
const buildId = process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
222+
const buildId = process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
223223
return "http://cache.local" + `/${buildId}/${key}`.replace(/\/+/g, "/") + `.${cacheType ?? "cache"}`;
224224
}
225225

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

@@ -545,9 +546,9 @@ describe("D1NextModeTagCache", () => {
545546
expect(cacheKey).toBe(`${FALLBACK_BUILD_ID}/${key}`);
546547
});
547548

548-
it("should use custom build ID when NEXT_BUILD_ID is set", () => {
549+
it("should use custom build ID when OPEN_NEXT_BUILD_ID is set", () => {
549550
const customBuildId = "custom-build-id";
550-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
551+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
551552

552553
const key = "test-tag";
553554
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -556,7 +557,7 @@ describe("D1NextModeTagCache", () => {
556557
});
557558

558559
it("should handle double slashes by replacing them with single slash", () => {
559-
vi.stubEnv("NEXT_BUILD_ID", "build//id");
560+
vi.stubEnv("OPEN_NEXT_BUILD_ID", "build//id");
560561

561562
const key = "test-tag";
562563
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -566,16 +567,16 @@ describe("D1NextModeTagCache", () => {
566567
});
567568

568569
describe("getBuildId", () => {
569-
it("should return NEXT_BUILD_ID when set", () => {
570+
it("should return OPEN_NEXT_BUILD_ID when set", () => {
570571
const customBuildId = "custom-build-id";
571-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
572+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
572573

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

575576
expect(buildId).toBe(customBuildId);
576577
});
577578

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

581582
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
@@ -196,7 +196,7 @@ export class D1NextModeTagCache implements NextModeTagCache {
196196
}
197197

198198
protected getBuildId() {
199-
return process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
199+
return process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
200200
}
201201

202202
/**

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

@@ -406,9 +407,9 @@ describe("KVNextModeTagCache", () => {
406407
expect(cacheKey).toBe(`${FALLBACK_BUILD_ID}/${key}`);
407408
});
408409

409-
it("should use custom build ID when NEXT_BUILD_ID is set", () => {
410+
it("should use custom build ID when OPEN_NEXT_BUILD_ID is set", () => {
410411
const customBuildId = "custom-build-id";
411-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
412+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
412413

413414
const key = "test-tag";
414415
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -417,7 +418,7 @@ describe("KVNextModeTagCache", () => {
417418
});
418419

419420
it("should handle double slashes by replacing them with single slash", () => {
420-
vi.stubEnv("NEXT_BUILD_ID", "build//id");
421+
vi.stubEnv("OPEN_NEXT_BUILD_ID", "build//id");
421422

422423
const key = "test-tag";
423424
const cacheKey = (tagCache as unknown as { getCacheKey: (key: string) => string }).getCacheKey(key);
@@ -427,18 +428,16 @@ describe("KVNextModeTagCache", () => {
427428
});
428429

429430
describe("getBuildId", () => {
430-
it("should return NEXT_BUILD_ID when set", () => {
431+
it("should return OPEN_NEXT_BUILD_ID when set", () => {
431432
const customBuildId = "custom-build-id";
432-
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
433+
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);
433434

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

436437
expect(buildId).toBe(customBuildId);
437438
});
438439

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

444443
expect(buildId).toBe(FALLBACK_BUILD_ID);

0 commit comments

Comments
 (0)