Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
feat: add support to asyncLocalStorage (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
enicio authored Jan 19, 2024
1 parent 97099ff commit ba228ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/build-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class BuildDispatcher {
await builder.build(options)
} else {
VersionChecker.nextjs_version(targetDir);
VersionChecker.nextjs_project_type();
const builder = new NextjsBuilder(targetDir);
await builder.build(options);
}
Expand Down
3 changes: 2 additions & 1 deletion src/models/builders/nextjs-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as vcService from "./services/vercel-service";

import { ManifestBuilderService } from "./services/manifest-builder-service";
import { nodeBuiltInModulesPlugin } from "./plugins/esbuild-plugins";
import { globalBanner } from "../../templates/handlers/nextjs/globals";

class NextjsBuilder extends Builder {
manifestBuilderService = new ManifestBuilderService();
Expand Down Expand Up @@ -88,8 +89,8 @@ class NextjsBuilder extends Builder {
inject: [
join(this.dirname, "../../templates/handlers/nextjs/libs.js"),
params.outputReferencesFilePath,
join(this.dirname, "../../templates/handlers/nextjs/globals.js"),
],
banner: { js: globalBanner()},
minify: true,
target: "es2022",
platform: "neutral",
Expand Down
11 changes: 9 additions & 2 deletions src/templates/handlers/nextjs/globals.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export function globalBanner() {
return `import { AsyncLocalStorage } from "node:async_hooks";
globalThis.AsyncLocalStorage = AsyncLocalStorage;
const envAsyncLocalStorage = new AsyncLocalStorage();
globalThis.process = {
env: {
NODE_ENV: "production",
NODE_ENV: "production"
}
};
};`;
}
26 changes: 16 additions & 10 deletions src/templates/handlers/nextjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ const getStorageAsset = async (request) => {
async function main(request, env, ctx) {
globalThis.process.env = { ...globalThis.process.env, ...env };

const adjustedRequest = adjustRequestForVercel(request);

return handleRequest(
{
request: adjustedRequest,
ctx,
assetsFetcher: env.ASSETS,
},
__CONFIG__,
__BUILD_OUTPUT__
return envAsyncLocalStorage.run(
{...env},
async () => {
const adjustedRequest = adjustRequestForVercel(request);
return handleRequest(
{
request: adjustedRequest,
ctx,
assetsFetcher: env.ASSETS,
},
__CONFIG__,
__BUILD_OUTPUT__
);
}
);


}
addEventListener("fetch", (event) => {
try {
Expand Down

0 comments on commit ba228ce

Please sign in to comment.