Query is empty inside getInitialProps on server side
See original GitHub issueBug report
Describe the bug
I have mask: https://somedomain.com/someurl/:projectName?price and I try to get params like this:
getInitialProps({ query }) {
console.log('getInitialProps = ', query);
}
but the query = {}, its reproduce only on server side!
if I open the url from client side by click on <Link route"...">, the query works good.
To Reproduce
- open some url with query
- add that code to your page:
getInitialProps({ query }) {
console.log('getInitialProps = ', query);
}
Expected behavior
The query shouldn’t be empty, it should be {projectName: '...', price: '...'}
Screenshots
System information
- OS:Windows
- Browser chrome
- Version of Next.js: 8.1.0
Additional context
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Why is the Query from getInitialProps empty? - Stack Overflow
I want to fetch some data using query parameters on the server for a page. However, my query is empty inside getInitialProps when...
Read more >getInitialProps - Data Fetching - Next.js
getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data...
Read more >getInitialProps vs. getServerSideProps in Next.js
Explore the differences between getInitialProps and getServerSideProps, two methods for defining props for server-side rendered pages.
Read more >Next.js Server Side Rendering and getInitialProps - YouTube
We take a look at the power of Next.js and how it handles fetching data using getInitialProps. We also cover some of the...
Read more >What is Next.js getInitialProps and getServerSideProps? | refine
It includes many built-in features such as Server-side Rendering (SSR), Static Site Generation (SSG), automatic image optimization, backend ...
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 Free
Top 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

If another noob like me is running into this. What @timneutkens means is that in your server.js you have probably forgotten to add req.query. That was the issue for me at least.
If you don’t want to do any custom server-side stuff at all, you can follow this to learn that
shows the query even on the server side, as well as on the client. To get the same value in the template it’s
This is very badly documented in the docs themselves, but super clear in the tutorial.