From 8158146844a77a1144668007e1e67c16702bfc36 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Tue, 19 May 2026 11:13:55 +0200 Subject: [PATCH] fix: pre-bundle Next.js 16 navigation/redirect-error for Vite dev mode Next.js 16 ships internal re-export chains that Vite's esbuild pre-bundler fails to resolve in dev mode, causing missing exports like ServerInsertedHTMLContext and RedirectStatusCode. Add next/navigation and next/dist/client/components/redirect-error to optimizeDeps.include for Next.js 16+ so Vite pre-bundles them correctly. See: https://github.com/storybookjs/storybook/issues/34688 --- .changeset/nextjs16-optimizedeps-navigation.md | 5 +++++ src/index.ts | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .changeset/nextjs16-optimizedeps-navigation.md diff --git a/.changeset/nextjs16-optimizedeps-navigation.md b/.changeset/nextjs16-optimizedeps-navigation.md new file mode 100644 index 0000000..d00b71b --- /dev/null +++ b/.changeset/nextjs16-optimizedeps-navigation.md @@ -0,0 +1,5 @@ +--- +"vite-plugin-storybook-nextjs": patch +--- + +Fix missing Next.js 16 internal exports (`ServerInsertedHTMLContext`, `RedirectStatusCode`) in Vite dev mode by adding `next/navigation` and `next/dist/client/components/redirect-error` to `optimizeDeps.include` for Next.js 16+. See [storybookjs/storybook#34688](https://github.com/storybookjs/storybook/issues/34688). diff --git a/src/index.ts b/src/index.ts index 5872325..6628d80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -182,6 +182,17 @@ function VitePlugin({ ? ["@emotion/react/jsx-dev-runtime"] : []), ...(isNext16orNewer ? [] : ["next/config"]), + // Next.js 16 ships internal re-export chains that Vite's esbuild + // pre-bundler fails to resolve in dev mode, causing missing exports + // like ServerInsertedHTMLContext and RedirectStatusCode. Forcing + // these modules to be pre-bundled fixes the resolution. + // See: https://github.com/storybookjs/storybook/issues/34688 + ...(isNext16orNewer + ? [ + "next/navigation", + "next/dist/client/components/redirect-error", + ] + : []), ], }, test: {