vercel / vercel/next.js

Empty URL search params are swallowed when rewriting via middleware on Vercel

Open
#73,722 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug 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/migueloller/vercel-middleware-search-params-bug-repro

To Reproduce

Deploy the application to Vercel. The easiest way is using the Vercel CLI. I have a deployed version that can be used to test here.

Visit the path /?foo= and then visit /?foo=bar. Note that while the search params are there for /?foo=bar, they are not there for /?foo=.

Current vs. Expected behavior

The expectation is that URL search params are there even if the value is an empty string. Note, this is only happening because of the rewrite in the middleware. If the middleware.ts file is deleted, then the expected behavior occurs.

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:04 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 18.19.0
  npm: 10.2.3
  Yarn: 1.22.21
  pnpm: 8.15.6
Relevant Packages:
  next: 15.0.3 // There is a newer version (15.0.4) available, upgrade recommended! 
  eslint-config-next: 15.0.3
  react: 19.0.0-rc-66855b96-20241106
  react-dom: 19.0.0-rc-66855b96-20241106
  typescript: 5.7.2
Next.js Config:
  output: N/A
 ⚠ There is a newer version (15.0.4) available, upgrade recommended! 
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue
Which area(s) are affected? (Select all that apply)

Middleware

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

Vercel (Deployed)

Additional context

The issue only happens when deploying to Vercel, it does not reproduce during local development. This is likely due to differences in the runtime middleware runs on in Vercel vs local dev.

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 and its middleware.ts, deploy it to Vercel with the Vercel CLI, and compare /?foo= with /?foo=bar. Trace the deployed middleware rewrite path in Next.js and add a regression test for preserving an empty search-parameter value; done when the deployed behavior matches local behavior and both URLs are covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
nextjs, react, typescript
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.