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.

"No router instance found." error when setting Router.beforePopState in pages/_app.js

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

Expected Behavior

Router.beforePopState should be easy to set. The code should reside in pages/_app.js with client-side only guard since it should be run on client only once.

Current Behavior

I put up an example repository here.

Server-side went ok without an error, but browsers complain with the following.

No router instance found.
You should only use "next/router" inside the client side of your app.

Error: No router instance found.
You should only use "next/router" inside the client side of your app.

    at throwIfNoRouter (http://localhost:3000/_next/static/commons/main.js:7835:11)
    at Object.SingletonRouter.(anonymous function) [as beforePopState] (http://localhost:3000/_next/static/commons/main.js:7799:5)
    at Object.<anonymous> (http://localhost:3000/_next/-/page/_app.js:2819:55)
    at Object../pages/_app.js (http://localhost:3000/_next/-/page/_app.js:2955:30)
    at __webpack_require__ (http://localhost:3000/_next/static/commons/manifest.js:715:31)
    at fn (http://localhost:3000/_next/static/commons/manifest.js:118:20)
    at Object.1 (http://localhost:3000/_next/-/page/_app.js:2962:18)
    at __webpack_require__ (http://localhost:3000/_next/static/commons/manifest.js:715:31)
    at webpackJsonpCallback (http://localhost:3000/_next/static/commons/manifest.js:26:23)
    at http://localhost:3000/_next/-/page/_app.js:5:7

Steps to Reproduce (for bugs)

Example repository

Context

See “Expected Behavior” above.

Your Environment

Tech Version
next 6.0.0
node 10.0.0
OS macOS High Sierra 10.13.4
browser Google Chrome 66.0.3359.139
etc

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
afganhalbanacommented, Jun 25, 2018

@peacefullybound Well at first i did have the same problem as yours, and after move that code to componentDidMount in _app.js the error disappear, but unfortunately it’s still not working as well without any error informed on the dev console, here is my componentDidUpdate code:

componentDidMount() {
    Router.beforePopState(({ url, as: asUrl, options }) => {
        console.log('why you never hit me :( tho')
	if (asUrl !== '/dashboard') {
            console.log('should redirect to 404 but it didnt')
	    window.location.href = asUrl
	    return false
	}
	return true
    })
}

my next version is 6.0.3, please share how you solved this issue

3reactions
afganhalbanacommented, Jun 12, 2018

hi @peacefullybound i did move it to componentDidMount lifecycle but unfortunately it still didnt work whenever i changed the route, any thought tho?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.JS Error: No router instance found - Marcusoft.net
I have a page that is server-side rendered, since I'm the getServerSideProps . One of the components on the page, reach out and...
Read more >
Next Unhandled runtime error. No router instance found
This solved the problem: Hooks error: Invalid Hook Call using NextJS or ReactJS on Windows. I was in "d/projects/.
Read more >
Error: No router instance found. You should only use “next ...
Error : No router instance found. You should only use “next/router” inside the client side of your app. this error it happens when...
Read more >
next/router beforePopState not firing : r/nextjs - Reddit
As per title, beforePopState just doesn't fire when I hit the back button. This code is a hook called in _app.js as per...
Read more >
next/router | Next.js
Learn more about the API of the Next.js Router, and access the router instance in your page with the useRouter hook.
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