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 in getInitialProps ignores query params in dev mode

See original GitHub issue

What version of Next.js are you using?

10.0.5

What version of Node.js are you using?

12.18.3

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

npm run dev

Describe the Bug

When using dev mode, the value of asPath in getInitialProps is correct only when rendered by the server. When rendered by the browser, only the path is included, and the query portion is ignored.

In prod (npm start), everything works as expected. This leads me to believe that there is some kind of race condition that surfaces only when the app is slowed down due to being in dev mode.

Expected Behavior

The value of asPath should consistently include the query portion both on the server and in the browser, even in dev mode.

To Reproduce

/pages/test1.js:

import { useRouter } from "next/router";

const Test1 = ({ asPath }) => {
  const router = useRouter();
  return (
    <div onClick={() => router.push("/test2", "/test2?foo=bar")}>
      go to /test2?foo=bar
      <br />
      <br />
      asPath = {asPath}
    </div>
  );
};

Test1.getInitialProps = ({ asPath }) => {
  return { asPath };
};

export default Test1;

/pages/test2.js:

import Link from "next/link";

const Test2 = ({ asPath }) => {
  return (
    <Link href="/test1" as="/test1?foo=bar">
      <div>
        go to /test1?foo=bar
        <br />
        <br />
        asPath = {asPath}
      </div>
    </Link>
  );
};

Test2.getInitialProps = ({ asPath }) => {
  return { asPath };
};

export default Test2;

Upon initial SSR render, navigating to /test1?foo=bar will correctly display the entire asPath. But after clicking “go to /test2?foo=bar,” only /test2 is displayed, even though the actual path includes a query string.

Note that this happens both when using the Link component, as well as with router.push.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ijjkcommented, Jan 22, 2021

Hi, this has been updated in the latest version of Next.js v10.0.6-canary.7, please upgrade and give it a try!

2reactions
kaykdmcommented, Jan 21, 2021

I can reproduce this issue. I think it was working fine before this

I think the reason that this issue happens in only dev is https://github.com/vercel/next.js/blob/699a7aeaaa48a6c3611ede7a35af2d9676421de0/packages/next/build/webpack-config.ts#L216 Since a rewirte flag is on in dev, it goes through here where a query is not applied to the path https://github.com/vercel/next.js/blob/699a7aeaaa48a6c3611ede7a35af2d9676421de0/packages/next/next-server/lib/router/router.ts#L934

I am not sure why a rewrite flag is always true in dev.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get (query string) parameters from the URL in Next.js?
To get the query parameters, use getInitialProps : ... asPath and parse the query yourself with a library like query-string:
Read more >
getInitialProps - Data Fetching - Next.js
query - Query string section of URL parsed as an object; asPath - String of the actual path (including the query) shown in...
Read more >
@sheerun/next NPM | npm.io
getInitialProps receives a context object with the following properties: pathname - Current route. That is the path of the page in /pages; query...
Read more >
Next.js: packages/next/shared/lib/router/router.ts | Fossies
() => Promise<T> 91 locale?: string 92 asPath: string 93 router: Router 94 } 95 96 export ... 169 query 170 171 interpolatedRoute...
Read more >
gaolijuan/yetech-2.0: 云能科技官网2.0 - Gogs
asPath = // @ts-ignore this is temporarily global (attached to window)\n is_dynamic_1. ... getInitialProps;\n }\n\n _this2.set(route, pathname, query, as, ...
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