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.

`asPath` is omitted in getServerSideProps' parameters and `req.url` is inconsistent regarding query parameters

See original GitHub issue

The new function getServerSideProps does not have the parameter asPath that getInitialProps has. I tried using req.url instead, but the query parameters are not consistent. They are not sent during CSR calls, while they are present during SSR calls.

I would prefer to have the whole URL (like asPath) to analyze the URL using a separate module. Or have the querystring parameters in req.url

How to reproduce

import React from 'react';
import Router from 'next/router';
import { NextPage, GetServerSideProps } from "next";

const changeUrl = () => Router.push('/test?a=2');


const Test: NextPage = (): JSX.Element => {
  return (<>
    <a onClick={changeUrl}>Click me</a>
  </>);
}

export const getServerSideProps: GetServerSideProps = async ({ req, query, params })  => {
  console.log('req.url', req.url)
  console.log('query', JSON.stringify(query))
  console.log('params', JSON.stringify(params))
  return { props: {} };
}

export default Test;

When accessing http://localhost:3002/test, we get the following output:

req.url /test
query {}
params undefined

When clicking the link, we get the following output:

req.url /test
query {"a":"2"}
params undefined

When accessing http://localhost:3002/test?a=2 with SSR, we get the following output:

req.url /test?a=2
query {"a":"2"}
params undefined

System information

  • Version of Next.js: 9.3.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
ijjkcommented, Aug 20, 2020

@jgpacker a new feature request for adding a massaged asPath param to getServerSideProps does sound like it would be good to open since it’s separate from the initial described issue 👍

2reactions
meliborncommented, Oct 19, 2021

Closed somewhy. I can give you one more reason to add asPath to request object. It’s rewrites rules. I can’t get an alias at all in getServerSideProps

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Fetching: getServerSideProps - Next.js
Learn how to fetch data on each request with Next.js. ... The context parameter is an object containing the following keys: params :...
Read more >
How to get previous URL in Next.js? - Stack Overflow
I thought the values this.props.router.asPath and nextProps.router.asPath are different. Actually, I want to call router.push after ...
Read more >
Handling POST requests in Next.js getServerSideProps
urlencoded() returns an middleware function that has the common 3 arguments, request , response and next . We use promisify to turn the...
Read more >
Refreshing Server-Side Props - Next.js - Josh W Comeau
In a Next.js application I'm working on, I have an "admin" page, which lets me manage registered users: A screenshot of a dashboard...
Read more >
Package Diff: @netlify/plugin-nextjs @ 3.4.2 .. 3.6.2
feat: merge `next-on-netlify` into `@netlify/plugin-nextjs` ... \"The provided `href` (\".concat(url, \") value is missing query values ...
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