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

replaceState #18

Closed
individual8 opened this issue Jun 17, 2016 · 9 comments
Closed

replaceState #18

individual8 opened this issue Jun 17, 2016 · 9 comments

Comments

@individual8
Copy link

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.

krasimir pushed a commit that referenced this issue Jun 18, 2016
@krasimir
Copy link
Owner

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

@individual8
Copy link
Author

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.

@krasimir
Copy link
Owner

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.

@individual8
Copy link
Author

individual8 commented Jun 18, 2016

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;
    }

@krasimir
Copy link
Owner

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.

@individual8
Copy link
Author

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

krasimir pushed a commit that referenced this issue Jun 18, 2016
@krasimir
Copy link
Owner

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

@individual8
Copy link
Author

Works the way intended, thank you.

@hirasso
Copy link

hirasso commented Sep 11, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants