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.

router.asPath shows previous URL on client side from _app.js getInitialProps

See original GitHub issue

console.log(router) shows correct asPath but If I access console.log(router.asPath) shows the previous URL

  • I have searched the issues of this repository and believe that this is not a duplicate.

Code (_app.js)

export default class MyApp extends App {
  constructor(props) {
    super(props)
  }

  static async getInitialProps({ Component, router, ctx }) {
    // Router.asPath shows current URL, can be seen from browser console as below
    console.log(router)
    // But if i access the asPath property as below, it shows previous URL. On server side, it's fine
    console.log(router.asPath)
  }
  ...
}

Expected Behavior

Refer to above code, router.asPath should return current URL on client side

Current Behavior

Refer to above code, router.asPath returns previous URL on client side

Steps to Reproduce (for bugs)

Please refer to above code example.

Context

After url is deprecated, we pass URL from _app.js, as all pages need this for some sort of SEO related stuffs.

as a workaround using ctx.asPath for now

Your Environment

Tech Version
next 6.0.0
node 9.x
OS mac
browser Chrome

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
timneutkenscommented, Jun 15, 2018

Specifically this PR fixed it: #4541

0reactions
timneutkenscommented, Jun 15, 2018

Should be fixed in next@canary

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get previous URL in Next.js? - Stack Overflow
Actually, I want to call router.push after login. I know that router.back goes to the previous page. But it's possible to go to...
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 >
What is Next.js getInitialProps and getServerSideProps? | refine
getInitialProps behavior can be tricky for developers who are new to Next.js. The function runs only on the server at the initial page...
Read more >
Refreshing Server-Side Props - Next.js - Josh W Comeau
Next allows you to do server-side data-fetching, but what happens when that data needs to change on the client? This brief tutorial shows...
Read more >
Client-Side Routing In Next.js - Smashing Magazine
Next.js has a file-based routing system in which each page automatically becomes a route based on its file name. This article will guide...
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