Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next 7 - Router.push() does`t work as expected #5264

Closed
severjason opened this issue Sep 23, 2018 · 17 comments
Closed

Next 7 - Router.push() does`t work as expected #5264

severjason opened this issue Sep 23, 2018 · 17 comments

Comments

@severjason
Copy link

severjason commented Sep 23, 2018

Bug report

Describe the bug

Next 7 - Router.push() does`t work.

To Reproduce

I have a search handler that should redirect to '/search' page from main page. But nothing happens.
routeChangeError throws Route cancelled. But on Next 6.1.2. everything works fine.
Also if I go to the link http://localhost:8080/search directly it works as expected, seems like a problem with client side routing. Using HOC withRouter gives the same results.

handleSearch = (event, { suggestionValue }) => {
    if (suggestionValue.address_string === undefined) {
      return;
    }
    const newQueryParams = {
      // New search should always start from 1st page
      page: 1,
      // If search value is empty, it should send default search request
      address: isString(suggestionValue.address_string) ? suggestionValue.address_string : '',
    };
    Router.events.on('routeChangeError', (err) => {
        console.log(err)
    })
    if (Router.router.pathname === '/search') {
      Router.push({ pathname: '/search', query: { ...Router.router.query, ...newQueryParams } })
    } else {
      Router.push({ pathname: '/search', query: newQueryParams })
    }
  };

Expected behavior

Expected to work the same as in Next 6.1.2

System information

  • OS: Ubuntu 18.04
  • Version of Next.js: 7.0.0
@timneutkens
Copy link
Member

Can you create a minimal github repository? That way I can add tests for it if there is indeed a problem, but I suspect there's something else going on.

@severjason
Copy link
Author

I will need some time to prepare repository, but for now, can you tell in what situations Route can be cancelled ?

@udanpe
Copy link

udanpe commented Sep 24, 2018

same problem all pages where i use import css not working.

@msand
Copy link

msand commented Sep 24, 2018

This is probably the same bug as here: #5203
Can be worked around by importing an empty css file from _app.js
It's caused by the chunks having a deferred module dependency on the style chunk, but the style chunk is never loaded, so the new chunk never executes. If you depend on a css file from the _app.js file, then the style chunk will always be available and the new route executes once it's loaded.

@msand
Copy link

msand commented Sep 24, 2018

And this is also the same: vercel/next-plugins#282

@severjason
Copy link
Author

severjason commented Sep 24, 2018

I found comments in Router error:

this.abortComponentLoad(as); // If the url change is only related to a hash change
                // We should not proceed. We should only change the state.

So for me solution was to use window.location:

if (Router.router.pathname === '/search') {
      Router.push({ pathname: '/search', query: { ...Router.router.query, ...newQueryParams } })
    } else {
      window.location.replace(`/search?page=${newQueryParams.page}&address=${newQueryParams.address}`);
    }

@timneutkens
Copy link
Member

This doesn’t sound like the correct solution. Could you provide a minimal reproduction?

@ex3ndr
Copy link
Contributor

ex3ndr commented Sep 25, 2018

Same here. I have simple component that redirects on mount (simple page redirect), router fires events about navigation start, but it never completes.

@futjikato
Copy link

@timneutkens In vercel/next-plugins#282 @standy linked this as his minimal reproduction:
https://github.com/standy/next-css-error

@popuguytheparrot
Copy link
Contributor

This is probably the same bug as here: #5203
Can be worked around by importing an empty css file from _app.js
It's caused by the chunks having a deferred module dependency on the style chunk, but the style chunk is never loaded, so the new chunk never executes. If you depend on a css file from the _app.js file, then the style chunk will always be available and the new route executes once it's loaded.

Not working

DavyBello added a commit to papasunnee/pret-app that referenced this issue Jan 16, 2019
Workaround for bug caused by the chunks having a deferred module dependency on the style chunk, but the style chunk is never loaded, so the new chunk never executes. If you depend on a css file from the _app.js file, then the style chunk will always be available and the new route executes once it's loaded.
see vercel/next.js#5264,
@fortydegrees
Copy link

FWIW, I randomly encountered this issue and importing a random .css file in _app.js fixed it

@jamesrford7
Copy link

It's bitten me too, that was a good 2 hours of pulling my hair out.

@msand workaround fixed it though so a massive thanks.
Would be nice not have the scrappy fix though.

@skptricks
Copy link

This is probably the same bug as here: #5203
Can be worked around by importing an empty css file from _app.js
It's caused by the chunks having a deferred module dependency on the style chunk, but the style chunk is never loaded, so the new chunk never executes. If you depend on a css file from the _app.js file, then the style chunk will always be available and the new route executes once it's loaded.

After following steps, it is still not working. could you please let me know the workaround for the same ?

@alvincrespo
Copy link

@msand you saved my life. Thank you.

@douglas-pires
Copy link

I was having a problem with redirection using Router.push. I was using something like:

useEffect(() => {
  if (authResult) {
    Router.push({ pathname: '/workspaces' }, '/workspaces')
  }
}, [authResult])

Now I have:

  useEffect(() => {
    if (authResult) {
      Router.events.on('routeChangeStart', url => {
        window.location.href = url
      })
      Router.push({ pathname: '/workspaces' }, '/workspaces')
    }
  }, [authResult, Router])

It doesn't smell good but hopefully helps someone.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests