next.js icon indicating copy to clipboard operation
next.js copied to clipboard

useSearchParams context is missing search params from rewritten url

Open noreiller opened this issue 1 year ago • 3 comments

Link to the code that reproduces this issue

https://github.com/noreiller/next-missing-search-params-with-rewrites

To Reproduce

  1. Start the application (npm run dev)
  2. Navigate to http://localhost:3000/app-url-to-rewrite
  3. The search param foo is not detected

Current vs. Expected behavior

When we use a rewrite containing search params, for example to the destination /my-page?foo=bar, we expect that the search param foo is available from the useSearchParams() hook and has the value bar.

Currently, the useSearchParams() hook does not return any search param.

With next/router (Page Router), the query object contains the expected search params.

This occurs in both App Router and Page Router.

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct  6 10:23:26 UTC 2
Binaries:
  Node: 18.12.1
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: 8.2.0
Relevant Packages:
  next: 14.0.1
  eslint-config-next: 14.0.1
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

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

App Router, Routing (next/router, next/navigation, next/link)

Additional context

No response

noreiller avatar Nov 09 '23 17:11 noreiller

I found in the e2e tests that the search params from the rewrite are available in the searchParams prop available in the page. It sound strange to me that the searchParams from the url and the rewrite are not merged and available in the SearchParamsContext.

noreiller avatar Nov 16 '23 17:11 noreiller

@noreiller Hey! Could you please tell the status of this issue. Do you have an idea when it's gonna be fixed?

tiana25 avatar Jan 30 '24 00:01 tiana25

Hi, I had no update on this so I changed my code. I think that query params should be avoided as much as possible in rewirtes. You can also use redirects.

noreiller avatar Jan 30 '24 06:01 noreiller