Skip to content

Commit 782f0d1

Browse files
committed
switch between modes at runtime and apply on process.env
1 parent 835cea8 commit 782f0d1

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export async function copyPackageCliFiles(
2222

2323
fs.cpSync(sourceDir, destinationDir, { recursive: true });
2424

25-
const envVars = extractProjectEnvVars(openNextConfig);
25+
const devEnvVars = extractProjectEnvVars("development", openNextConfig);
26+
const prodEnvVars = extractProjectEnvVars("production", openNextConfig);
27+
2628
await build({
2729
entryPoints: [path.join(packageDistDir, "cli", "templates", "worker.ts")],
2830
outfile: path.join(openNextConfig.outputDir, "worker.js"),
@@ -31,7 +33,8 @@ export async function copyPackageCliFiles(
3133
bundle: false,
3234
minify: false,
3335
define: {
34-
__OPENNEXT_BUILD_TIME_ENV: JSON.stringify(envVars),
36+
__OPENNEXT_BUILD_TIME_DEV_ENV: JSON.stringify(devEnvVars),
37+
__OPENNEXT_BUILD_TIME_PROD_ENV: JSON.stringify(prodEnvVars),
3538
},
3639
});
3740
}

packages/cloudflare/src/cli/build/utils/extract-project-env-vars.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("extractProjectEnvVars", () => {
2323
afterEach(() => mockFs.restore());
2424

2525
it("should extract production env vars", () => {
26-
const result = extractProjectEnvVars(options);
26+
const result = extractProjectEnvVars("production", options);
2727
expect(result).toEqual({
2828
ENV_LOCAL_VAR: "value",
2929
ENV_PROD_LOCAL_VAR: "value",
@@ -35,7 +35,7 @@ describe("extractProjectEnvVars", () => {
3535
it("should extract development env vars", () => {
3636
writeFileSync(".dev.vars", 'NEXTJS_ENV = "development"');
3737

38-
const result = extractProjectEnvVars(options);
38+
const result = extractProjectEnvVars("development", options);
3939
expect(result).toEqual({
4040
ENV_LOCAL_VAR: "value",
4141
ENV_DEV_LOCAL_VAR: "value",
@@ -47,7 +47,7 @@ describe("extractProjectEnvVars", () => {
4747
it("should override env vars with those in a local file", () => {
4848
writeFileSync(".env.production.local", "ENV_PROD_VAR=overridden");
4949

50-
const result = extractProjectEnvVars(options);
50+
const result = extractProjectEnvVars("production", options);
5151
expect(result).toEqual({
5252
ENV_LOCAL_VAR: "value",
5353
ENV_PROD_VAR: "overridden",

packages/cloudflare/src/cli/build/utils/extract-project-env-vars.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ function readEnvFiles(fileNames: string[], { monorepoRoot, appPath }: BuildOptio
2929
* In a monorepo, the env files in an app's directory will take precedence over
3030
* the env files at the root of the monorepo.
3131
*/
32-
export function extractProjectEnvVars(options: BuildOptions) {
33-
const wranglerDevVars = readEnvFiles([".dev.vars"], options);
34-
35-
const mode = wranglerDevVars["NEXTJS_ENV"] ?? "production";
36-
32+
export function extractProjectEnvVars(mode: "development" | "production", options: BuildOptions) {
3733
return readEnvFiles([".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`], options);
3834
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
2121
}
2222
);
2323

24-
declare const __OPENNEXT_BUILD_TIME_ENV: Record<string, string>;
24+
declare const __OPENNEXT_BUILD_TIME_DEV_ENV: Record<string, string>;
25+
declare const __OPENNEXT_BUILD_TIME_PROD_ENV: Record<string, string>;
26+
27+
function applyBuildTimeEnv(mode?: string) {
28+
const secrets = mode === "development" ? __OPENNEXT_BUILD_TIME_DEV_ENV : __OPENNEXT_BUILD_TIME_PROD_ENV;
29+
30+
for (const key in secrets) {
31+
process.env[key] = secrets[key];
32+
}
33+
}
2534

2635
export default {
2736
async fetch(request, env, ctx) {
28-
const combinedEnv = { ...__OPENNEXT_BUILD_TIME_ENV, ...env };
29-
30-
return cloudflareContextALS.run({ env: combinedEnv, ctx, cf: request.cf }, async () => {
37+
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
3138
// Set the default Origin for the origin resolver.
3239
const url = new URL(request.url);
3340
process.env.OPEN_NEXT_ORIGIN = JSON.stringify({
@@ -38,16 +45,18 @@ export default {
3845
},
3946
});
4047

48+
applyBuildTimeEnv(env["NEXTJS_ENV"]);
49+
4150
// The Middleware handler can return either a `Response` or a `Request`:
4251
// - `Response`s should be returned early
4352
// - `Request`s are handled by the Next server
44-
const reqOrResp = await middlewareHandler(request, combinedEnv, ctx);
53+
const reqOrResp = await middlewareHandler(request, env, ctx);
4554

4655
if (reqOrResp instanceof Response) {
4756
return reqOrResp;
4857
}
4958

50-
return serverHandler(reqOrResp, combinedEnv, ctx);
59+
return serverHandler(reqOrResp, env, ctx);
5160
});
5261
},
53-
} as ExportedHandler<{ ASSETS: Fetcher }>;
62+
} as ExportedHandler<{ ASSETS: Fetcher; NEXTJS_ENV?: string }>;

0 commit comments

Comments
 (0)