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

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

About this issue

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

Most upvoted comments

@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 👍

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

@endrureza @ratheo I created #16407. Please complement with your use cases

@Timer, I face the same problem with “getServerSideProps” but only when I export the pages statically (npm run export). I can able to run npm run start successfully. Not sure what’s the issue with getServerSideProps. Is it fixed?