Skip to content

Commit

Permalink
Install AsyncLocalStorage for appDir rendering (vercel/turborepo#3374)
Browse files Browse the repository at this point in the history
#44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by patching the node import onto `globalThis.` Importantly, it's then used in the module scope during imports, so we need to install the polyfill early in the app rendering startup.

Fixes vercel/turborepo#3319
  • Loading branch information
jridgewell authored Jan 19, 2023
1 parent 0417e52 commit 37c2aba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/next-core/js/src/entry/app-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { RenderData } from "types/turbopack";

import "next/dist/server/node-polyfill-fetch";
import "next/dist/server/node-polyfill-web-streams";
import "@vercel/turbopack-next/polyfill/async-local-storage";
import { RenderOpts, renderToHTMLOrFlight } from "next/dist/server/app-render";
import { PassThrough } from "stream";
import { ServerResponseShim } from "@vercel/turbopack-next/internal/http";
Expand Down
6 changes: 6 additions & 0 deletions crates/next-core/js/src/polyfill/async-local-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Next.js now creates an AsyncLocalStorage in the module scope, meaning we
// need to:
// 1. Install it (it's accessed as `globalthis.AsyncLocalStorage`)
// 2. Do it in an import (import ordering is guaranteed)
import { AsyncLocalStorage } from "node:async_hooks";
globalThis.AsyncLocalStorage = AsyncLocalStorage;
9 changes: 9 additions & 0 deletions crates/next-core/js/types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ declare global {
page: string,
paths: string[]
): unknown;

var AsyncLocalStorage = class AsyncLocalStorage<T> {
getStore(): T | undefined;
run<R, TArgs extends any[]>(
store: T,
callback: (...args: TArgs) => R,
...args: TArgs
): R;
};
}

export {};

0 comments on commit 37c2aba

Please sign in to comment.