Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-toes-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

Prefer `OPEN_NEXT_BUILD_ID` for Cloudflare cache keys, populating it from Next `deploymentId` with the build ID as a fallback.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class KVIncrementalCache implements IncrementalCache {
protected getKVKey(key: string, cacheType?: CacheEntryType): string {
return computeCacheKey(key, {
prefix: getCloudflareContext().env[PREFIX_ENV_NAME],
buildId: process.env.NEXT_BUILD_ID,
buildId: process.env.OPEN_NEXT_BUILD_ID,
cacheType,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class R2IncrementalCache implements IncrementalCache {
protected getR2Key(key: string, cacheType?: CacheEntryType): string {
return computeCacheKey(key, {
prefix: getCloudflareContext().env[PREFIX_ENV_NAME],
buildId: process.env.NEXT_BUILD_ID,
buildId: process.env.OPEN_NEXT_BUILD_ID,
cacheType,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class RegionalCache implements IncrementalCache {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class StaticAssetsIncrementalCache implements IncrementalCache {
if (cacheType === "composable") {
throw new Error("Composable cache is not supported in static assets incremental cache");
}
const buildId = process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
const buildId = process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
const name = (
cacheType === "fetch"
? `${CACHE_DIR}/__fetch/${buildId}/${key}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ describe("D1NextModeTagCache", () => {
expect(error).toHaveBeenCalledWith(mockError);
});

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

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

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

it("should use custom build ID when NEXT_BUILD_ID is set", () => {
it("should use custom build ID when OPEN_NEXT_BUILD_ID is set", () => {
const customBuildId = "custom-build-id";
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);

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

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

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

describe("getBuildId", () => {
it("should return NEXT_BUILD_ID when set", () => {
it("should return OPEN_NEXT_BUILD_ID when set", () => {
const customBuildId = "custom-build-id";
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);

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

expect(buildId).toBe(customBuildId);
});

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

expect(buildId).toBe(FALLBACK_BUILD_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class D1NextModeTagCache implements NextModeTagCache {
}

protected getBuildId() {
return process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
return process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ describe("KVNextModeTagCache", () => {
expect(error).toHaveBeenCalledWith(mockError);
});

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

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

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

it("should use custom build ID when NEXT_BUILD_ID is set", () => {
it("should use custom build ID when OPEN_NEXT_BUILD_ID is set", () => {
const customBuildId = "custom-build-id";
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);

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

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

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

describe("getBuildId", () => {
it("should return NEXT_BUILD_ID when set", () => {
it("should return OPEN_NEXT_BUILD_ID when set", () => {
const customBuildId = "custom-build-id";
vi.stubEnv("NEXT_BUILD_ID", customBuildId);
vi.stubEnv("OPEN_NEXT_BUILD_ID", customBuildId);

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

expect(buildId).toBe(customBuildId);
});

it("should return fallback build ID when NEXT_BUILD_ID is not set", () => {
// Environment variables are cleared by vi.unstubAllEnvs() in beforeEach

it("should return fallback build ID when no build ID env vars are set", () => {
const buildId = (tagCache as unknown as { getBuildId: () => string }).getBuildId();

expect(buildId).toBe(FALLBACK_BUILD_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class KVNextModeTagCache implements NextModeTagCache {
}

protected getBuildId() {
return process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
return process.env.OPEN_NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions packages/cloudflare/src/cli/build/open-next/compile-init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "node:path";
import { fileURLToPath } from "node:url";

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

const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
const nextConfig = loadConfig(dotNextDir);
const basePath = nextConfig.basePath ?? "";
const deploymentId = nextConfig.deploymentId ?? "";
const openNextBuildId = nextConfig.deploymentId ?? loadBuildId(dotNextDir);
const trailingSlash = nextConfig.trailingSlash ?? false;

await build({
Expand All @@ -32,6 +34,7 @@ export async function compileInit(options: BuildOptions, wranglerConfig: Unstabl
__NEXT_BASE_PATH__: JSON.stringify(basePath),
__ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
__DEPLOYMENT_ID__: JSON.stringify(deploymentId),
__OPEN_NEXT_BUILD_ID__: JSON.stringify(openNextBuildId),
__TRAILING_SLASH__: JSON.stringify(trailingSlash),
},
});
Expand Down
3 changes: 3 additions & 0 deletions packages/cloudflare/src/cli/templates/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function populateProcessEnv(url: URL, env: CloudflareEnv) {
if (__DEPLOYMENT_ID__) {
process.env.DEPLOYMENT_ID = __DEPLOYMENT_ID__;
}
process.env.OPEN_NEXT_BUILD_ID = __OPEN_NEXT_BUILD_ID__;
Comment thread
vicb marked this conversation as resolved.
Outdated
}

declare global {
Expand All @@ -156,6 +157,8 @@ declare global {
var __ASSETS_RUN_WORKER_FIRST__: boolean | string[] | undefined;
// Deployment ID
var __DEPLOYMENT_ID__: string;
// Open Next build ID (deploymentId or fallback buildId)
var __OPEN_NEXT_BUILD_ID__: string;
// Next trailingSlash config
var __TRAILING_SLASH__: boolean;
}
Loading