krasimir/navigo

replaceState

individual8 opened this issue · 9 comments

Is there a way to replace the current state, instead of just push via navigate()?

For changing language I'm looking for such functionality, since changing language is not really navigating away from the current content.

@individual8 just released a new version 2.0.1. Can you please check this solution https://github.com/krasimir/navigo#pausing-the-router

Thx for the update, @krasimir.

It generally seems to work the way intended. I think one issue's still that you're adding that "paused" navigate() to the list of routes. Hence, it's breaking browser's back history.

I see. That's because we are using pushState to change the URL. If we use window.location.href we'll reload the page. If we use hashes then will be possible to avoid that.

But to be honest I would prefer to have that in the history.

Wouldn't something like this solve that?

overwrite: function overwrite(path, absolute) {
      path = path || '';
      if (this._ok) {
        history.replaceState({}, '', (!absolute ? this._getRoot() + '/' : '') + clean(path));
        this.pause(true);
        this.resolve();
        this.pause(false);
      } else if (typeof window !== 'undefined') {
        window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path;
      }
      return this;
    }

So maybe it's a matter of using replaceState instead of pushState because

history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one. Note that this doesn't prevent the creation of a new entry in the global browser history.

Not sure why, but it seems that 2.0.1 won't resolve '*' (Home) anymore btw.

Can you try with 2.0.3. It uses replaceState if the router is paused.

Works the way intended, thank you.

Hi there, I would need the replaceState option without navigo being paused, I have a scrollSpy running on my site that should change the url with events being fired on scroll, but if the user would hit the back button he should not have to go through all the entries.