Next.js: Router.push('/') is not working as expected

Created on 25 Dec 2018  路  17Comments  路  Source: vercel/next.js

Bug report

Describe the bug

```js
import React, { Component } from 'react';
import Router from 'next/router';

export default class _error extends Component {
componentDidMount = () => {
Router.push('/');
};

render() {
return

;
}
}
````

This is code inside _error.js. I was expected it would redirect to index.js page but nothing is happening.
The Weird Thing is When I use Router.push('/error', '/');, It pushes to index.js page.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Make any page.
  2. Router.push('/')

Expected behavior

Push to index.js page

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: macOS
  • Browser Chorme
  • Version of Next.js: 7.0.2

Most helpful comment

It's not currently happening to me right now, but when I had this problem, this code worked.

Router.push('/error', '/');

I'm not sure it's going to work in every page since I used it in _error.js page.

All 17 comments

Router.push('/') will route to the pages/index.js file.

@timneutkens Yes. That is what I expected. but it's not routing to index.js page.

Hi everyone, some solution for that?

Same problem here, routing to "/" and "/index" don't work, but any other route does.

It's not currently happening to me right now, but when I had this problem, this code worked.

Router.push('/error', '/');

I'm not sure it's going to work in every page since I used it in _error.js page.

It works again after upgrading all packages

I bumped into the same issue. The snipped provided by @ktjd123 worked.

next is able to change the url in the browser but somehow in doesn't trigger the getInitialProps

Same here. I have the following on pages directory:

  • pages/page/index.js
  • pages/page/add.js

When I'm at /page/add and use

Router.push({ pathname: '/page', query: { p: object._id } }, `/page/${page._id}`)

It won't work. It starts routing, triggers onRouteChangeStart then freezes.

If I change the pathname to any other page (even if doesn't exists'), e.g. /banana, it routes (if it doesn't exists it throws 404).

So, as any other use case works, I'm guessing the problem is related to being in /pageFile/AnySubPageFile and routing to/pageFile/index.

EDIT: Forgot to mention, routing with window.location.href=`/page?p=${page._id}` works.

Same here. I have the following on pages directory:

  • pages/page/index.js
  • pages/page/add.js

When I'm at /page/add and use

Router.push({ pathname: '/page', query: { p: object._id } }, `/page/${page._id}`)

It won't work. It starts routing, triggers onRouteChangeStart then freezes.

If I change the pathname to any other page (even if doesn't exists'), e.g. /banana, it routes (if it doesn't exists it throws 404).

So, as any other use case works, I'm guessing the problem is related to being in /pageFile/AnySubPageFile and routing to/pageFile/index.

EDIT: Forgot to mention, routing with window.location.href=`/page?p=${page._id}` works.

I am having the same issue. Is there a fix for this or can you just not use the Link component in this way?

I also came across this with the following code:

componentDidMount() {
        const params = new URLSearchParams( location.search.slice(1) );
        const error = params.get('error');
        const token = params.get('token');

        if ( error ) {

        } else if ( token ) {
            console.log('Token found');
            Router.push('/dashboard');
        } 
    }

when i was making this, it would sometimes redirect with the hot reloading when i changed the code inside componentDidMount but didn't redirect on any hard page load. Can confirm the above Router.push('/error', '/'); type of solution resolved it for me for now.

(next.js v8.0.4)

Happening to me as well with next 8.1.0

Also started to fail for me with next 7.0.3. It worked flawlessly one or two weeks ago. I am not sure what changed since then. I did not update next.

I have this behavior where the url in the browser change but getInitialProps and other React stuff are not being called so basically the page content stays the same.

http://localhost:3000/settings/store/thompson-electronics/5d10c73cc56e42a048c5d0df
http://localhost:3000/settings/store/valerio-canez-s.a./5d13c7b234a6e60fdca42c06

this happens when I am trying to switch between those 2 links which share the same page. If I click on the link while being on different page [not sharing the settings] things work normally.

@timneutkens could you please reopen this? It's a real issue and we currently have it in production

@ematipico please create a new issue following the issue template with a clear and concise reproduction.

I'm having this problem too!
I don't know how to repo this...but it seems that a specific route just not working...it triggers the router onRouteChangeStart method.... but nothing happens. I get this when I use custom _app.js file.
Pushing not existing page works, and routes to 404 page. Wierd...any solutions guys?

Hi everyone! I'm going to lock this issue to prevent further "+1"s.

If you are experiencing this issue, please open a new issue. Thank you for your understanding!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

havefive picture havefive  路  3Comments

pie6k picture pie6k  路  3Comments

wagerfield picture wagerfield  路  3Comments

flybayer picture flybayer  路  3Comments

sospedra picture sospedra  路  3Comments