@clerk/nextjs: Pages Router builds bundle next/headers into the client via the App Router provider

オープン
#10,593 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
nextjs, react, typescript

調査の方向性

Start with the import chain: dist/esm/client-boundary/ClerkProvider.js loads both the App Router and Pages Router providers, and app-router/server-actions.js is a "use server" file that imports cookies from next/headers. Build a minimal Pages Router app with ClerkProvider in pages/_app, run next build, and search .next/static/chunks for RequestCookiesAdapter. Done means the client chunks no longer contain next/headers, and the App Router path still works. The maintainers have not chosen between an opt-in entry point and lazy loading, so confirm the approach first.

索引モデルが issue の本文から書いたものです。

説明

This issue was written by Claude Code, but verified by me.

Reproduction

No separate repo. It shows up in any Pages Router app that renders ClerkProvider from @clerk/nextjs: build it and search the client chunks for Next's cookie code (e.g. RequestCookiesAdapter).

Publishable key

N/A. The problem is in the build output, not the instance.

Description

In a Pages Router app, ClerkProvider adds Next's server request code to every route's client JS: cookies, headers, draft mode and the edge cookie parser. In our app that's 18.5 KB gzipped per route.

The import chain, in @clerk/nextjs 7.4.3:

  • dist/esm/client-boundary/ClerkProvider.js imports both app-router/client/ClerkProvider and pages/ClerkProvider, and picks one at runtime with next/compat/router. Both are bundled.
  • app-router/client/ClerkProvider.js imports invalidateCacheAction from app-router/server-actions.js.
  • server-actions.js is a "use server" file that imports cookies from next/headers.

An App Router build turns that file into a server-action reference. A Pages Router build has no server-action boundary, so the file and the whole next/headers graph land in the client bundle.

Steps to reproduce:

  1. Create a Pages Router app with ClerkProvider in pages/_app.tsx.
  2. Run next build.
  3. Search .next/static/chunks for RequestCookiesAdapter or ResponseCookies.

Expected behavior:

A Pages Router app's client bundle doesn't include next/headers.

Actual behavior:

Every route's client bundle includes next/headers and the request code behind it.

Workaround:

We alias next/headers to a stub in the browser only:

// next.config.ts
turbopack: {
  resolveAlias: {
    'next/headers': { browser: './src/utils/next-headers-browser.ts' },
  },
},

The stub exports cookies, headers and draftMode, each of which throws if called. Nothing calls them, since the App Router provider never renders under the Pages Router.

Possible fixes:

Bundling both providers makes sense for apps that mix routers. For Pages-only apps, either of these would avoid it:

  • An opt-in Pages Router entry point that doesn't import the App Router provider.
  • Load the App Router provider, or just invalidateCacheAction, lazily, so a Pages Router build doesn't pull in next/headers.
Environment
System:
  OS: macOS 26.6.2
  CPU: (12) arm64 Apple M2 Pro
Binaries:
  Node: 24.15.0
  pnpm: 12.16.1
npmPackages:
  @clerk/nextjs: ^7.4.3 => 7.4.3
  @clerk/shared: ^4.15.0 => 4.15.0
  next: 16.4.2 => 16.4.2
  react: 19.2.6 => 19.2.6
  react-dom: 19.2.6 => 19.2.6
Bundler: Turbopack (next build)
主要言語
TypeScript
スター
1.8k
フォーク
492
平均マージ
1日 8時間
マージ済み PR(30日)
374

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

clerk/javascript のほかの issue

clerk/javascript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。