question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

link to dynamic route with query params not working client-side

See original GitHub issue

Bug 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 is undefined)
  • <Link href={{ pathname: '/123', query: { foo: 'bar' } }}>...
    • this seems to work but it also does request to /pages/123.js and that throws 404 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)

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:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
Janpotcommented, Mar 6, 2020

What happens when you do

<Link 
  href={{ pathname: '/[id]', query: { foo: 'bar' } }} 
  as={{ pathname: '/123', query: { foo: 'bar' } }}
><a>...

?

7reactions
milesrichardsoncommented, Jun 10, 2020

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 to as and href like so:

// This works
const nextLinkProps = {
  as: {
    pathname: `/${namespace}`,
    query: {
      after: endCursor,
    },
  },
  href: {
    pathname: "/[namespace]",
    query: {
      after: endCursor,
    },
  },
}

What I was doing before, which did NOT work:

// This does NOT work (CSR fails as described in this issue)
const badLinkProps = {
  as: {
    pathname: `/${namespace}`,
    query: {
      after: endCursor,
    },
  },
  href: '/[namespace]'
}

I hope this helps someone in the future.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found