In my application, To Hide some information in URL i am using below code.
history.replaceState ({}, "", "bar.html");
It is working in all the browsers except firefox latest version (v56+)
In Firefox, if i press F5 then it is going back to previous URL which i have already replaced with above code.
Any help will be highly appreciated.
The big difference is, that while pushState will create a new entry in the browser's history, replaceState will only replace the current state. As a side effect of this, using the replaceState method will change the URL in the address bar, without creating a new history entry.
The History. replaceState() method modifies the current history entry, replacing it with the state object and URL passed in the method parameters. This method is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.
In an HTML document, the history. pushState() method adds an entry to the browser's session history stack.
There is an open issue on Bugzilla
.
Video Example 1 and Video Example 2 explain how to reproduce the bug.
Conditions:
- Mozilla Firefox version only
56+
- Single Page Application
- For routing uses
history.replaceState
, all parameters not nullSteps:
- Login & redirect to main page base
URL
- Navigate on any application tab & replace URL parameters
- Press
F5
,cmd + r
or clickRefresh
button- Ups!.. Again open main page with base
URL
(but in other browsers we see the selected tab and the correct URL)
The same behaviour is experienced when removing query strings from the url.
It could be caused from the following behaviour (I quote Vadim Goncharov)
The main problem is that after using
history.replaceState
and then clickingcmd+r/f5
we will see that browser sends replaced (correct)url
to server, but shows incorrect url both inlocation.search
and browser url bar. And this behaviour continues (if click to "cmd+r/f5") until we click "enter" on browser url bar.
First Workaround posted from Felix Lee
Before calling
history.replaceState
, dolocation.hash = location.hash;
Setting
hash
to itself has no effect, but makes the bug go away.
This workaround is not ideal and mtomalley adds a second Workaround
The browser is requesting a different URL than what is shown in the location bar....
Additionally, the workaround isn't ideal because if the
URL
doesn't already have ahash
,location.hash = location.hash
adds one, callspopstate
, and adds a history entry.An alternate workaround that is much less simple:
- Use whatever means available to your backend technology to expose the request
URI
on the client- On page load (before any client routing code), check the
URI
againstwindow.location
- If they're different, use
replaceState
to fix it.The location will briefly show the incorrect URL on any reload, but at least it'll be fixed and routing can work as expected...
Third Workaround proposed from Mathis Wiehl
window.addEventListener('unload', function(event) { location.replace(location) });
This way the state of the js location is flushed to FFs location in cases of refreshes and tab closes (which by the way have the same issue when reopened with e.g. ⌘+⇧+t).
The above workaround from Mathis has the following issue (I quote jimmyhmiller)
Next.js tried using the workaround that Mathis mentioned above and it caused some bad issues for them. Details here: https://github.com/zeit/next.js/pull/6896
A new bug was experienced with the above workaround, explained in the issue #6882
when landing on a page that contains query parameters, the browser becomes "locked" to that page and programmatically or manually navigating to a different same-domain page insta-redirects back to the original. note that this does not start happening until a query parameter is involved in the url, totally bizarre
I also include a list of other mozilla related issues with history.replaceState.
I keep myself available for further analysis, research, improvements to this posts and I am happy to receive your feedback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With