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.

Query is empty inside getInitialProps on server side

See original GitHub issue

Bug 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

  1. open some url with query
  2. 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
olajcommented, May 15, 2019

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.

    server.get("/page/home", (req, res) => {
      const actualPage = "/home";
      app.render(req, res, actualPage, req.query);
    });
4reactions
nikodunkcommented, Oct 14, 2019

If you don’t want to do any custom server-side stuff at all, you can follow this to learn that

getInitialProps(context) {
    console.log('getInitialProps = ', context.query);
  }

shows the query even on the server side, as well as on the client. To get the same value in the template it’s

<p>{this.props.router.query.id}</p>

This is very badly documented in the docs themselves, but super clear in the tutorial.

Read more comments on GitHub >

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

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