From 08b75b77b09a663299b83da943642081fc35283e Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 24 May 2024 17:39:08 +0200 Subject: [PATCH] Handle next/navigation import in middleware (#66175) ## What? Ensures that `next/navigation` and React is aliased in middleware in the same way that it's aliased in Route Handlers. This matches the behavior we have in Next.js with webpack. Fixes #66162 --------- Co-authored-by: Hendrik Liebau --- .../crates/next-core/src/next_import_map.rs | 6 +++++- .../navigation-redirect-import/app/layout.tsx | 8 ++++++++ .../app-dir/navigation-redirect-import/app/page.tsx | 3 +++ .../app/route-handler/route.ts | 8 ++++++++ .../navigation-redirect-import/middleware.ts | 5 +++++ .../navigation-redirect-import.test.ts | 13 +++++++++++++ .../navigation-redirect-import/next.config.js | 6 ++++++ 7 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/e2e/app-dir/navigation-redirect-import/app/layout.tsx create mode 100644 test/e2e/app-dir/navigation-redirect-import/app/page.tsx create mode 100644 test/e2e/app-dir/navigation-redirect-import/app/route-handler/route.ts create mode 100644 test/e2e/app-dir/navigation-redirect-import/middleware.ts create mode 100644 test/e2e/app-dir/navigation-redirect-import/navigation-redirect-import.test.ts create mode 100644 test/e2e/app-dir/navigation-redirect-import/next.config.js diff --git a/packages/next-swc/crates/next-core/src/next_import_map.rs b/packages/next-swc/crates/next-core/src/next_import_map.rs index f626b371f006b..e21ca1e6d82d6 100644 --- a/packages/next-swc/crates/next-core/src/next_import_map.rs +++ b/packages/next-swc/crates/next-core/src/next_import_map.rs @@ -583,7 +583,10 @@ async fn insert_next_server_special_aliases( rsc_aliases(import_map, project_path, ty, runtime, next_config).await?; } - ServerContextType::Middleware | ServerContextType::Instrumentation => {} + ServerContextType::Middleware => { + rsc_aliases(import_map, project_path, ty, runtime, next_config).await?; + } + ServerContextType::Instrumentation => {} } // see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531 @@ -727,6 +730,7 @@ async fn rsc_aliases( "react-dom" => format!("next/dist/compiled/react-dom{react_channel}/react-dom.react-server"), "next/dist/compiled/react-dom" => format!("next/dist/compiled/react-dom{react_channel}/react-dom.react-server"), "next/dist/compiled/react-dom-experimental" => format!("next/dist/compiled/react-dom-experimental/react-dom.react-server"), + "next/navigation" => format!("next/dist/api/navigation.react-server"), }) } diff --git a/test/e2e/app-dir/navigation-redirect-import/app/layout.tsx b/test/e2e/app-dir/navigation-redirect-import/app/layout.tsx new file mode 100644 index 0000000000000..888614deda3ba --- /dev/null +++ b/test/e2e/app-dir/navigation-redirect-import/app/layout.tsx @@ -0,0 +1,8 @@ +import { ReactNode } from 'react' +export default function Root({ children }: { children: ReactNode }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/app-dir/navigation-redirect-import/app/page.tsx b/test/e2e/app-dir/navigation-redirect-import/app/page.tsx new file mode 100644 index 0000000000000..ff7159d9149fe --- /dev/null +++ b/test/e2e/app-dir/navigation-redirect-import/app/page.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

hello world

+} diff --git a/test/e2e/app-dir/navigation-redirect-import/app/route-handler/route.ts b/test/e2e/app-dir/navigation-redirect-import/app/route-handler/route.ts new file mode 100644 index 0000000000000..ba8650b758e63 --- /dev/null +++ b/test/e2e/app-dir/navigation-redirect-import/app/route-handler/route.ts @@ -0,0 +1,8 @@ +import { redirect } from 'next/navigation' + +// Redirect can't be called in a route handler, but it should be able to be imported. +console.log({ redirect }) + +export function GET() { + return new Response('hello world') +} diff --git a/test/e2e/app-dir/navigation-redirect-import/middleware.ts b/test/e2e/app-dir/navigation-redirect-import/middleware.ts new file mode 100644 index 0000000000000..983080a7b7eed --- /dev/null +++ b/test/e2e/app-dir/navigation-redirect-import/middleware.ts @@ -0,0 +1,5 @@ +import { redirect } from 'next/navigation' +// Redirect can't be called in middleware, but it should be able to be imported. +console.log({ redirect }) + +export default function middleware() {} diff --git a/test/e2e/app-dir/navigation-redirect-import/navigation-redirect-import.test.ts b/test/e2e/app-dir/navigation-redirect-import/navigation-redirect-import.test.ts new file mode 100644 index 0000000000000..69cd7d268ba02 --- /dev/null +++ b/test/e2e/app-dir/navigation-redirect-import/navigation-redirect-import.test.ts @@ -0,0 +1,13 @@ +import { nextTestSetup } from 'e2e-utils' + +describe('navigation-redirect-import', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) + + it('should work using fetch', async () => { + const res = await next.fetch('/route-handler') + expect(res.status).toBe(200) + expect(await res.text()).toBe('hello world') + }) +}) diff --git a/test/e2e/app-dir/navigation-redirect-import/next.config.js b/test/e2e/app-dir/navigation-redirect-import/next.config.js new file mode 100644 index 0000000000000..807126e4cf0bf --- /dev/null +++ b/test/e2e/app-dir/navigation-redirect-import/next.config.js @@ -0,0 +1,6 @@ +/** + * @type {import('next').NextConfig} + */ +const nextConfig = {} + +module.exports = nextConfig