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.

Router query object not immediately populated from dynamic route params

See original GitHub issue

Bug report

The router query object is not populated immediately from dynamic route params on either the server or the client.

Describe the bug

The example for Dynamic routing shows using useRouter() to extract a query param to use for rendering. However, on the server, and on client first load, query is an empty object.

This becomes a problem when relying on the param to do anything more than display it - for example using swr to retrieve data based on it.

It’s also unexpected, because it appears to be information that is knowable immediately, given either a req or a window.location.

To Reproduce

Example repro:

// [id].tsx
const TestPage: NextPage = () => {
  const router = useRouter()
  const { query } = router
  const id = query.id
  console.log('Props', process.browser, router)

  return <h1>ID: {id}</h1>
}

Actual behaviour

Visiting /42

There is one entry in the server log:

Props false undefined

The component runs twice on the client, with two entries in the client console: Props true undefined Props true 42

Expected behavior

I expect query to be populated the first time I call useRouter().

I expect a server log of Props false 42.

I expect the function to render once on the client, with one entry in the console: Props true 42

System information

  • Version of Next.js: 9.3.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
VinSpeecommented, Mar 25, 2020

I bumped into this today as well, and tried withRouter as well, but it behaves the same.

3reactions
Timercommented, Apr 13, 2020

This should work:

const ProfilePage = ({ query }: Props) => {
  const { username } = query;
  const { data: user } = useSwr(() =>
    username ? `/api/v1/users/${username}` : false
  );

  if (!user) return null;
  return <div>{user.name}</div>;
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Router query object not immediately populated from dynamic ...
The router query object is not populated immediately from dynamic route params on either the server or the client.
Read more >
Next.js router is returning query parameters as undefined on ...
I think your answer is on the right track. It seems that Next.js initially gets the query object from window.__NEXT_DATA__ , which is...
Read more >
Dynamic Routes - Next.js
Dynamic Routes are pages that allow you to add custom params to your URLs. ... For example, the route /post/abc will have the...
Read more >
Implementing SSR in Next.js: Dynamic routing and prefetching
Something to keep in mind is that a query parameter with the same name as a particular route parameter will be overwritten by...
Read more >
NextJS Router and Query Params #09 - YouTube
NextJS full Course | Server-side rendering with React JSFull Playlist for React JS Full Course ...
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