Skip to content

Commit f500aea

Browse files
committed
refactor: move process.env to ALS proxy
1 parent 83df4f5 commit f500aea

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
@@ -11,53 +11,61 @@ import { createALSProxy } from "./utils";
1111

1212
const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
1313

14+
const processEnvALS = new AsyncLocalStorage<Record<string, unknown>>();
1415
const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
1516

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

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

2330
let requestHandler: NodeRequestHandler | null = null;
2431

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

45-
const url = new URL(request.url);
52+
const url = new URL(request.url);
4653

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

56-
const { req, res, webResponse } = getWrappedStreams(request, ctx);
63+
const { req, res, webResponse } = getWrappedStreams(request, ctx);
5764

58-
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));
65+
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));
5966

60-
return await webResponse();
67+
return await webResponse();
68+
});
6169
});
6270
},
6371
} as ExportedHandler<{ ASSETS: Fetcher }>;

0 commit comments

Comments
 (0)