Skip to content

Commit 627d10b

Browse files
committed
refactor: move process.env to ALS proxy
1 parent 335fac5 commit 627d10b

File tree

1 file changed

+36
-28
lines changed
  • packages/cloudflare/src/cli/templates

1 file changed

+36
-28
lines changed

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,61 @@ import type { CloudflareContext } from "../../api";
1313

1414
const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
1515

16+
const processEnvALS = new AsyncLocalStorage<Record<string, unknown>>();
1617
const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
1718

1819
// Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
1920
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2021
(globalThis as any)[Symbol.for("__cloudflare-context__")] = createALSProxy(cloudflareContextALS);
2122

23+
globalThis.process = {
24+
...globalThis.process,
25+
// @ts-expect-error - populated when we run inside the ALS context
26+
env: createALSProxy(processEnvALS),
27+
};
28+
2229
// Injected at build time
2330
const nextConfig: NextConfig = JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_CONFIG ?? "{}");
2431

2532
let requestHandler: NodeRequestHandler | null = null;
2633

2734
export default {
2835
async fetch(request, env, ctx) {
29-
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
30-
if (requestHandler == null) {
31-
globalThis.process.env = { ...globalThis.process.env, ...env };
32-
// Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
33-
// (since esbuild can run in projects with different module resolutions)
34-
// eslint-disable-next-line @typescript-eslint/no-require-imports
35-
const NextNodeServer = require("next/dist/server/next-server")
36-
.default as typeof import("next/dist/server/next-server").default;
37-
38-
requestHandler = new NextNodeServer({
39-
conf: nextConfig,
40-
customServer: false,
41-
dev: false,
42-
dir: "",
43-
minimalMode: false,
44-
}).getRequestHandler();
45-
}
36+
return processEnvALS.run({ NODE_ENV: "production", ...env }, () => {
37+
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
38+
if (requestHandler == null) {
39+
// Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
40+
// (since esbuild can run in projects with different module resolutions)
41+
// eslint-disable-next-line @typescript-eslint/no-require-imports
42+
const NextNodeServer = require("next/dist/server/next-server")
43+
.default as typeof import("next/dist/server/next-server").default;
44+
45+
requestHandler = new NextNodeServer({
46+
conf: nextConfig,
47+
customServer: false,
48+
dev: false,
49+
dir: "",
50+
minimalMode: false,
51+
}).getRequestHandler();
52+
}
4653

47-
const url = new URL(request.url);
54+
const url = new URL(request.url);
4855

49-
if (url.pathname === "/_next/image") {
50-
const imageUrl =
51-
url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
52-
if (imageUrl.startsWith("/")) {
53-
return env.ASSETS.fetch(new URL(imageUrl, request.url));
56+
if (url.pathname === "/_next/image") {
57+
const imageUrl =
58+
url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
59+
if (imageUrl.startsWith("/")) {
60+
return env.ASSETS.fetch(new URL(imageUrl, request.url));
61+
}
62+
return fetch(imageUrl, { cf: { cacheEverything: true } });
5463
}
55-
return fetch(imageUrl, { cf: { cacheEverything: true } });
56-
}
5764

58-
const { req, res, webResponse } = getWrappedStreams(request, ctx);
65+
const { req, res, webResponse } = getWrappedStreams(request, ctx);
5966

60-
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));
67+
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));
6168

62-
return await webResponse();
69+
return await webResponse();
70+
});
6371
});
6472
},
6573
} as ExportedHandler<{ ASSETS: Fetcher }>;

0 commit comments

Comments
 (0)