From 9694fc4f7cd064549f7f45b7a03da6f6e5f8db8c Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Mon, 6 Feb 2023 17:47:29 -0500 Subject: [PATCH] Import Next's CJS AsyncLocalStorage modules (vercel/turbo#3634) The node evaluation always renders with `type: "commonjs"` and `require()` calls, so we'll always import the CJS files. But here we're importing the ESM files. That means we have 2 distinct instances of `requestAsyncStorage` in our node instance, and they cannot properly communicate with the other. Fixes WEB-543 --- .../crates/next-core/js/src/entry/app-renderer.tsx | 4 +++- .../next-core/js/src/entry/app/layout-entry.tsx | 6 +++--- .../crates/next-core/js/src/entry/edge-bootstrap.ts | 2 +- .../next-swc/crates/next-core/js/types/modules.d.ts | 6 +++--- .../app/async-local-storage/input/app/layout.tsx | 12 ++++++++++++ .../next/app/async-local-storage/input/app/page.tsx | 9 +++++++++ .../next/app/async-local-storage/input/app/test.tsx | 12 ++++++++++++ .../app/async-local-storage/input/next.config.js | 5 +++++ 8 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/layout.tsx create mode 100644 packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/page.tsx create mode 100644 packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/test.tsx create mode 100644 packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js diff --git a/packages/next-swc/crates/next-core/js/src/entry/app-renderer.tsx b/packages/next-swc/crates/next-core/js/src/entry/app-renderer.tsx index 09a5d102ef6b3..ace7c1f03bab4 100644 --- a/packages/next-swc/crates/next-core/js/src/entry/app-renderer.tsx +++ b/packages/next-swc/crates/next-core/js/src/entry/app-renderer.tsx @@ -238,7 +238,9 @@ async function runOperation(renderData: RenderData) { body = result.toUnchunkedString(); } return { - headers: [["Content-Type", result.contentType() ?? MIME_TEXT_HTML_UTF8]], + headers: [ + ["Content-Type", result.contentType() ?? MIME_TEXT_HTML_UTF8], + ] as [string, string][], body, }; } diff --git a/packages/next-swc/crates/next-core/js/src/entry/app/layout-entry.tsx b/packages/next-swc/crates/next-core/js/src/entry/app/layout-entry.tsx index a2de7577ef23b..d5b5dd503ff06 100644 --- a/packages/next-swc/crates/next-core/js/src/entry/app/layout-entry.tsx +++ b/packages/next-swc/crates/next-core/js/src/entry/app/layout-entry.tsx @@ -2,9 +2,9 @@ export { default as AppRouter } from "next/dist/client/components/app-router.js" export { default as LayoutRouter } from "next/dist/client/components/layout-router.js"; export { default as RenderFromTemplateContext } from "next/dist/client/components/render-from-template-context.js"; export { default as GlobalError } from "next/dist/client/components/error-boundary.js"; -export { staticGenerationAsyncStorage } from "next/dist/esm/client/components/static-generation-async-storage.js"; -export { requestAsyncStorage } from "next/dist/esm/client/components/request-async-storage.js"; -import * as serverHooks from "next/dist/esm/client/components/hooks-server-context.js"; +export { staticGenerationAsyncStorage } from "next/dist/client/components/static-generation-async-storage.js"; +export { requestAsyncStorage } from "next/dist/client/components/request-async-storage.js"; +import * as serverHooks from "next/dist/client/components/hooks-server-context.js"; export { serverHooks }; export { renderToReadableStream } from "next/dist/compiled/react-server-dom-webpack/server.browser"; diff --git a/packages/next-swc/crates/next-core/js/src/entry/edge-bootstrap.ts b/packages/next-swc/crates/next-core/js/src/entry/edge-bootstrap.ts index dd5e027c8748e..9da889da3f206 100644 --- a/packages/next-swc/crates/next-core/js/src/entry/edge-bootstrap.ts +++ b/packages/next-swc/crates/next-core/js/src/entry/edge-bootstrap.ts @@ -1,6 +1,6 @@ declare const PAGE: string; -import { adapter, enhanceGlobals } from "next/dist/esm/server/web/adapter"; +import { adapter, enhanceGlobals } from "next/dist/server/web/adapter"; enhanceGlobals(); diff --git a/packages/next-swc/crates/next-core/js/types/modules.d.ts b/packages/next-swc/crates/next-core/js/types/modules.d.ts index 6400dd185c226..febf391e9d99c 100644 --- a/packages/next-swc/crates/next-core/js/types/modules.d.ts +++ b/packages/next-swc/crates/next-core/js/types/modules.d.ts @@ -1,4 +1,4 @@ -declare module "next/dist/esm/client/components/static-generation-async-storage.js"; -declare module "next/dist/esm/client/components/request-async-storage.js"; -declare module "next/dist/esm/client/components/hooks-server-context.js"; +declare module "next/dist/client/components/static-generation-async-storage.js"; +declare module "next/dist/client/components/request-async-storage.js"; +declare module "next/dist/client/components/hooks-server-context.js"; declare module "next/dist/compiled/react-server-dom-webpack/server.browser"; diff --git a/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/layout.tsx b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/layout.tsx new file mode 100644 index 0000000000000..c7108f765016d --- /dev/null +++ b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/layout.tsx @@ -0,0 +1,12 @@ +import { cookies } from "next/headers"; + +export default function RootLayout({ children }: { children: any }) { + return ( + + + {JSON.stringify(cookies(), null, 2)} + {children} + + + ); +} diff --git a/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/page.tsx b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/page.tsx new file mode 100644 index 0000000000000..9886a00c76a65 --- /dev/null +++ b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/page.tsx @@ -0,0 +1,9 @@ +import Test from "./test"; + +export default function Page() { + return ( +
+ +
+ ); +} diff --git a/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/test.tsx b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/test.tsx new file mode 100644 index 0000000000000..692d1a7ffc3ff --- /dev/null +++ b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/test.tsx @@ -0,0 +1,12 @@ +"use client"; + +import { useEffect } from "react"; + +export default function Test() { + useEffect(() => { + import("@turbo/pack-test-harness").then(() => { + it("should run", () => {}); + }); + return () => {}; + }, []); +} diff --git a/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js new file mode 100644 index 0000000000000..ed0e87891f9e8 --- /dev/null +++ b/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js @@ -0,0 +1,5 @@ +module.exports = { + experimental: { + appDir: true, + }, +};