Skip to content

Commit a1fcbb6

Browse files
authored
refactor: Next env loading (#337)
1 parent 1ccff65 commit a1fcbb6

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

packages/cloudflare/env.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ declare global {
77
NEXT_PRIVATE_DEBUG_CACHE?: string;
88
OPEN_NEXT_ORIGIN: string;
99
NODE_ENV?: string;
10-
// Whether process.env has been populated (on first request).
11-
__PROCESS_ENV_POPULATED?: string;
1210
}
1311
}
1412
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { extractProjectEnvVars } from "../utils/index.js";
99
* Compiles the values extracted from the project's env files to the output directory for use in the worker.
1010
*/
1111
export function compileEnvFiles(buildOpts: BuildOptions) {
12+
const envDir = path.join(buildOpts.outputDir, "env");
13+
fs.mkdirSync(envDir, { recursive: true });
1214
["production", "development", "test"].forEach((mode) =>
1315
fs.appendFileSync(
14-
path.join(buildOpts.outputDir, `.env.mjs`),
16+
path.join(envDir, `next-env.mjs`),
1517
`export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, buildOpts))};\n`
1618
)
1719
);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { AsyncLocalStorage } from "node:async_hooks";
22

33
import type { CloudflareContext } from "../../api";
44
// @ts-expect-error: resolved by wrangler build
5+
import * as nextEnvVars from "./env/next-env.mjs";
6+
// @ts-expect-error: resolved by wrangler build
57
import { handler as middlewareHandler } from "./middleware/handler.mjs";
68
// @ts-expect-error: resolved by wrangler build
79
import { handler as serverHandler } from "./server-functions/default/handler.mjs";
@@ -21,15 +23,15 @@ const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
2123
}
2224
);
2325

26+
// Populate process.env on the first request
27+
let processEnvPopulated = false;
28+
2429
export default {
2530
async fetch(request, env, ctx) {
2631
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
2732
const url = new URL(request.url);
2833

29-
if (process.env.__PROCESS_ENV_POPULATED !== "1") {
30-
await populateProcessEnv(url, env.NEXTJS_ENV);
31-
process.env.__PROCESS_ENV_POPULATED = "1";
32-
}
34+
populateProcessEnv(url, env.NEXTJS_ENV);
3335

3436
if (url.pathname === "/_next/image") {
3537
const imageUrl = url.searchParams.get("url") ?? "";
@@ -59,10 +61,11 @@ export default {
5961
*
6062
* Note that cloudflare env string values are copied by the middleware handler.
6163
*/
62-
async function populateProcessEnv(url: URL, nextJsEnv?: string) {
63-
// @ts-expect-error: resolved by wrangler build
64-
const nextEnvVars = await import("./.env.mjs");
65-
64+
function populateProcessEnv(url: URL, nextJsEnv?: string) {
65+
if (processEnvPopulated) {
66+
return;
67+
}
68+
processEnvPopulated = true;
6669
const mode = nextJsEnv ?? "production";
6770

6871
if (nextEnvVars[mode]) {

0 commit comments

Comments
 (0)