dynamic routes param value is not defined for server side render when literal dynamic route value is provided as the pathname
See original GitHub issueBug 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:
- Go to https://3q2nu.sse.codesandbox.io/
- Click on the
[day]
link - Observe the values for
router
andgIP
(both are[day]
as expected) - Reload the page (https://3q2nu.sse.codesandbox.io/days/[day])
- Observe the values for
router
andgIP
(both areundefined
)
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:
- Created 3 years ago
- Reactions:1
- Comments:8 (1 by maintainers)
Top GitHub Comments
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:
Unless you mean the query object on the router.
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.