next.js: No way to change url without triggering navigation despite what docs say

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

Expected Behavior

Need to be able to update url without any navigation ie a history.replaceState

The docs say in https://github.com/zeit/next.js#imperatively:

Note: in order to programmatically change the route without triggering navigation and component-fetching, use props.url.push and props.url.replace within a component

Current Behavior

The call gives a deprecation warning and still triggers navigation

Steps to Reproduce (for bugs)

  1. use props.url.replace within a component
  2. notice that a navigation is triggered.

Context

I am trying to update the url without anything else changing. A way to do a history.replaceState in the next.js world. Calling history.replaceState steps outside the next.js router and then messes up back navigation. Using {shallow:true} with Router.replace still calls componentWillReceiveProps.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 5
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Try Router.push(url, as, {shallow:true}) .

if as and url match, shallow will work .

I had

Router.replace(`/users`, `/users?${queryString}`, {shallow: true});

This caused reload

Now I have

Router.replace(`/users?${queryString}`, `/users?${queryString}`, {shallow: true});

This worked

any fix for this one?

@johhansantana @sebas5384 I worked around this with history.pushState.

Some people emphasized on the importance of the second param, as, when working with Router.push(url, as, { shallow: true }). I didn’t manage to get it working on serverless no matter what combination of url and as I was using, so I falled back to the history API

I’m not sure what the answer here is but I don’t seem to get the url to change without the page replacing the view using shallow: true

https://codesandbox.io/s/7zrpz5wo0j

Should it be done using window.history.pushState?

I’m trying to achieve what this example has https://github.com/now-examples/nextgram/blob/master/pages/index.js#L58-L62

@shakesBeardZ I think it’s correct for the page to receive new props (even on shallow routing), since the url has changed per props. But the main difference is, that getInitialProps is not called in case of a shallow routing.

any fix for this one?

I mentioned above that I had tried using {shallow:true} what I meant is that I had used Router.replace(url, as, {shallow:true})