link to dynamic route with query params not working client-side
See original GitHub issueBug report
Note there are 4 similar issues but I think none of them is duplicate.
Describe the bug
I think it’s not possible to create a Link for dynamic route with query params in a way which would work both server-side & client-side.
To Reproduce
- create
/pages/[id].js
const { query: { id, foo } } = useRouter()
console.log(id, foo)
- create
/pages/index.js
<Link href="/[id]" as={{ pathname: '/123', query: { foo: 'bar' } }}><a>...
- this way, it doesn’t propagate to client-side (only
id
is available,foo
isundefined
)
- this way, it doesn’t propagate to client-side (only
<Link href={{ pathname: '/123', query: { foo: 'bar' } }}>...
- this seems to work but it also does request to
/pages/123.js
and that throws404
which in turn does full-page reload which actually shows what’s expected (but it’s super-ugly and much slower as it’s just SSR result and not regular client-side route transition)
- this seems to work but it also does request to
Expected behavior
I’d expect href="/[id]" as={{ pathname: '/123', query: { foo: 'bar' } }}
to work with both client-side routing and prefetching (if I understand correctly what that 123.js
request is)
System information
- OS: macOS
- Browser: latest Chrome
- Next.js: 9.2.2-canary.15
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Next.js router not working properly when using dynamic routes ...
Router not working properly client side when using dynamic routes and query params. I have the following structure in pages:
Read more >Dynamic Routes - Next.js
Dynamic Routes are pages that allow you to add custom params to your URLs. Start creating Dynamic Routes and learn more here.
Read more >Client-Side Routing In Next.js - Smashing Magazine
The dynamic segment(s) of a route is exposed as a query parameter that can be accessed in any of the connecting component involved...
Read more >Fixing Next.js router query param returning undefined on ...
First, let's just try to reproduce this issue. You might encounter some cases where you need access to query params of a route....
Read more >[Solution] NextJS Router Query Not Working in useEffect
If you ever tried to get query params from next/router in useEffect , you probably have this similar issue which the params are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What happens when you do
?
I was also having this problem, but it was due to a misunderstanding of required link props. The docs (https://nextjs.org/docs/api-reference/next/link) are not very clear about this IMO.
In my case I have a variable called
endCursor
on a page called[namespace].js
and I want the URL to be/[namespace]?after=${endCursor}
. To make a link to this page work with both SSR and CSR, you need to pass the fully qualified props toas
andhref
like so:What I was doing before, which did NOT work:
I hope this helps someone in the future.