Skip to content

Commit 19dedc7

Browse files
authored
fix: process.env has a higher loading priority than .env files (#523)
1 parent 3bd200a commit 19dedc7

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

.changeset/modern-yaks-add.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: process.env has a higher loading priority than .env files
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
TEST_ENV_VAR=TEST_VALUE
1+
TEST_ENV_VAR=TEST_VALUE
2+
PROCESS_ENV_VAR=.ENV_FILE

examples/playground14/e2e/cloudflare.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ test.describe("playground/cloudflare", () => {
1313
const { nextConfig } = await res.json();
1414
expect(nextConfig.output).toEqual("standalone");
1515
});
16+
17+
test("Environment variable defined on process.env are not overridden by .env files", async ({ page }) => {
18+
const res = await page.request.get("/api/env");
19+
await expect(res.json()).resolves.toEqual(expect.objectContaining({ PROCESS_ENV_VAR: "process.env" }));
20+
});
1621
});

examples/playground14/wrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
}
1616
],
1717
"vars": {
18-
"hello": "Hello World from the cloudflare context!"
18+
"hello": "Hello World from the cloudflare context!",
19+
"PROCESS_ENV_VAR": "process.env"
1920
}
2021
}

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function populateProcessEnv(url: URL, env: CloudflareEnv) {
8686
const mode = env.NEXTJS_ENV ?? "production";
8787
if (nextEnvVars[mode]) {
8888
for (const key in nextEnvVars[mode]) {
89-
process.env[key] = nextEnvVars[mode][key];
89+
process.env[key] ??= nextEnvVars[mode][key];
9090
}
9191
}
9292

0 commit comments

Comments
 (0)