From cd8d62b3d36c342cff29f8673f1a438b2905e90c Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 3 Feb 2023 23:23:57 -0500 Subject: [PATCH 1/2] Import CJS modules 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. --- crates/next-core/js/src/entry/app-renderer.tsx | 4 +++- crates/next-core/js/src/entry/app/layout-entry.tsx | 6 +++--- crates/next-core/js/src/entry/edge-bootstrap.ts | 2 +- crates/next-core/js/types/modules.d.ts | 6 +++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/next-core/js/src/entry/app-renderer.tsx b/crates/next-core/js/src/entry/app-renderer.tsx index 09a5d102ef6b3..ace7c1f03bab4 100644 --- a/crates/next-core/js/src/entry/app-renderer.tsx +++ b/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/crates/next-core/js/src/entry/app/layout-entry.tsx b/crates/next-core/js/src/entry/app/layout-entry.tsx index a2de7577ef23b..d5b5dd503ff06 100644 --- a/crates/next-core/js/src/entry/app/layout-entry.tsx +++ b/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/crates/next-core/js/src/entry/edge-bootstrap.ts b/crates/next-core/js/src/entry/edge-bootstrap.ts index dd5e027c8748e..9da889da3f206 100644 --- a/crates/next-core/js/src/entry/edge-bootstrap.ts +++ b/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/crates/next-core/js/types/modules.d.ts b/crates/next-core/js/types/modules.d.ts index 6400dd185c226..febf391e9d99c 100644 --- a/crates/next-core/js/types/modules.d.ts +++ b/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"; From f455cef1bd7ab410da531140349c1ca4b6595d7b Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Mon, 6 Feb 2023 17:28:03 -0500 Subject: [PATCH 2/2] Add test case --- Cargo.toml | 1 + .../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 +++++ 5 files changed, 39 insertions(+) create mode 100644 crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/layout.tsx create mode 100644 crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/page.tsx create mode 100644 crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/test.tsx create mode 100644 crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js diff --git a/Cargo.toml b/Cargo.toml index 79565361dba2d..761c510c17359 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ default-members = [ "crates/next-binding", "crates/next-core", "crates/next-dev", + "crates/next-dev-tests", "crates/next-transform-dynamic", "crates/next-transform-strip-page-exports", "crates/node-file-trace", diff --git a/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/layout.tsx b/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/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/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/page.tsx b/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/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/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/app/test.tsx b/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/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/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js b/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/crates/next-dev-tests/tests/integration/next/app/async-local-storage/input/next.config.js @@ -0,0 +1,5 @@ +module.exports = { + experimental: { + appDir: true, + }, +};