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

Bug report

Describe the bug

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

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

  render() {
    return <div />;
  }
}

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

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 17 (3 by maintainers)

Most upvoted comments

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.

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

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?