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.

dynamic routes param value is not defined for server side render when literal dynamic route value is provided as the pathname

See original GitHub issue

Bug report

Describe the bug

When using dynamic routes the param value is not defined when navigating to a page whose pathname is the string literal value for the dynamic route.

From the dynamic routes example:

Given a page defined as pages/post/[pid].js, navigating to /post/[pid] will not set the pid param value for the server side render if getInitialProps is defined for the page.

If getInitialProps is not provided, then the router correctly extracts the pid param. When getInitialProps is provided, the router does not extract the pid param, and the context argument for getInitialProps will not include pid in the query attribute.

Note: This only occurs for the server side render. When navigating client side, everything works as expected.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to https://3q2nu.sse.codesandbox.io/
  2. Click on the [day] link
  3. Observe the values for router and gIP (both are [day] as expected)
  4. Reload the page (https://3q2nu.sse.codesandbox.io/days/[day])
  5. Observe the values for router and gIP (both are undefined)

codesandbox: https://codesandbox.io/s/romantic-raman-3q2nu

Note: Removing DayPage.getInitialProps from the sandbox above will cause the router value to be correct both when navigating on the client and on the sever side render.

Expected behavior

The query param value should always be set.

Given a page defined as pages/post/[pid].js, navigating to /post/[pid] should set the pid param value to [pid] in both server and client side contexts.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: [macOs]
  • Browser [N/A]
  • Version of Next.js: [9.5.3, present on latest]
  • Version of Node.js: [12.x]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rmsheppardcommented, Apr 23, 2021

I’m having same issue. When using dynamic routes /pages/account/info/[tab]

When I reload the page as such localhost:3000/account/info/user. The correct page is displayed however the querystring tab is not passed to the page. That is at least how I expected it to work. If I do use the useRouter hook it does provide a router object with the correct querystring params. I could be wrong but it seems like a bug. Also this documentation doesn’t seem correct:

image

Unless you mean the query object on the router.

1reaction
JonatanLindstromcommented, Aug 27, 2021

Do we have any updates on this? I’m seeing the same issue in v11.1.0. Would be good with a native solution rather than having to write a function to run in each getServerSideProps that depends on the params existence.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ultimate React Router v6 Guide
Defining dynamic routes in React Router is as simple as putting a colon in front of whatever you want the dynamic part of...
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 >
Implementing SSR in Next.js: Dynamic routing and prefetching
A better approach would be to declare an index.js file under that route so that when the route parameter is not defined, it...
Read more >
The Complete Guide to URL parameters with React Router
URL parameters are parameters whose values are set dynamically in a page's URL. ... this means what you render can be dynamic based...
Read more >
How to create dynamic routes with react-router-dom?
Use Link to dynamically generate a list of routes. Use : to indicate url params, :id in the case; Use the match object...
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