vercel / vercel/next.js

Tree shaking in middleware doesn't work

Open
#75,180 3 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

linear: next Middleware
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/amannn/nextjs-bug-repro-middleware/commit/6c442b025f45b0f777fda6f32fc355e58bef1fb9

To Reproduce
  1. Run pnpm i && pnpm build
  2. See console output and analyzer output
Current vs. Expected behavior

Console output:

ƒ Middleware                             1.07 MB

I'd expect getMessages to not be bundled into the middleware as it's not used there.

See also the analyzer output:

Image

Provide environment information
Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:00 PDT 2024; root:xnu-10063.141.2~1/RELEASE_X86_64
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.22
  pnpm: 9.15.4
Relevant Packages:
  next: 15.1.5 // Latest available version is detected (15.1.5).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Middleware

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

next build (local)

Additional context

No response

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

Start with the linked reproduction at commit 6c442b025f45b0f777fda6f32fc355e58bef1fb9 and run pnpm i && pnpm build. Inspect the middleware bundle and analyzer output to trace why getMessages is included; done means the middleware no longer bundles that unused function.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.