vercel / vercel/next.js

Unused constants from constants.js do not get tree-shaked in the client bundles

Open
#85,664 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Output
Dominant language
JavaScript
Stars
142k
Forks
32.5k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/seyoon20087/nextjs-constants-not-treeshaked-demo

To Reproduce

NOTE: This actually happens on all Next.js apps that use the current version at the time of this writing; but still I am reporting it here.

  1. Clone the repo above;
  2. Run yarn build
  3. Inspect the bundles (especially .next/static/chunks/main-xxx.js); observe that unused constants are imported in the client bundle
  4. To additionally confirm this, I ran yarn client:rsd, opened the link printed in the terminal, then go to the "bundle size" section (next to "compile analysis". I see that, when inspecting the "static/chunls/main-xxx.js" file deeply, I see that node_modules/next/dist/shared/lib/constants.js and node_modules/next/dist/lib/constants.js are included in the bundle but does not tree-shake any constants that are not actually used in the client
Current vs. Expected behavior

Expected behavior:

Unused constants get tree-shaked out from the production bundle

Current behavior:

Unused constants ends up in the production bundle

Provide environment information
Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 22.6.0: Tue Jul 15 08:22:28 PDT 2025; root:xnu-8796.141.3.713.2~2/RELEASE_X86_64
  Available memory (MB): 8192
  Available CPU cores: 4
Binaries:
  Node: 24.7.0
  npm: 11.5.1
  Yarn: 1.22.22
  pnpm: 10.19.0
Relevant Packages:
  next: 16.0.2-canary.3 // Latest available version is detected (16.0.2-canary.3).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: N/A
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Output

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

This issue started all the way back from these commits:

I think the exact reason for this issue is that, because you are "transpiling" all these files, and pointing all client entrypoints to, CJS (and not ESM) by default, which negates the possibility of tree-shaking.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the linked demo using yarn build, then inspect .next/static/chunks/main-xxx.js for the constants modules and unused exports. Read the referenced route-regex.ts, router.ts, and page-loader.ts changes alongside the client bundle behavior. Done means production client bundles exclude constants that are not used by the client.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, react
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.