11

History API is now supported in every popular browser. It seems there's no need for hash fallbacks, _escaped_fragment_ tricks or other workarounds anymore. Cool libraries from 2013 like History.js seem useless now. But there are some things where I'm not sure - for example title handling seems tricky beacuse apparently title argument in pushState doesn't do anything.

My question is, can I actually rely on the History API to behave consistently across browsers, or do I still need some browser-specific code? This also means: do I need integration tests running in different browsers to test my code then? And if there are inconsistencies, what are they? (Note I'm interested only in modern browsers, so no IE<11).

Maybe someone who implemented routing for a big SPA could share their experience?

Community
  • 1
  • 1
mik01aj
  • 10,150
  • 11
  • 65
  • 109
  • 1
    "I'm not interested only in modern browsers, so no IE<11" - there seems to be an extra not? – mgol Aug 16 '16 at 08:46
  • 1
    No browser supports and no browser will support the title argument to `pushState`/`replaceState`, it's too late now. You can just always pass an empty string there, it doesn't matter. – mgol Aug 16 '16 at 08:47

3 Answers3

4

Note: This is not a complete answer so I'm not expecting a bounty but it still answers some concerns so I decided to put it as an answer

There are still some differences as with most APIs (you won't believe how inconsistent classList is between browsers); the question is mostly how severe they are.

pushState is most commonly used in SPAs and they seem to ignore the object parameter and mostly handle the URL. This means bugs related to state object handling may be less "visible" to the general public.

The only issue I've recently found that influenced what I was doing is that in IE & Edge (even 14) history.state is a getter so it gets a fresh object instance on every access. This means you can't cache the state object and compare it to history.state to see if a new one was pushed. Here's the bug report: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/944050/

Also, note that no browser supports setting title via pushState and no one ever will, it's too late. If a browser started supporting that, pushState-using sites would suddenly clear all titles in browser history because code is passing empty strings there. You just have to accept that the second parameter is useless.

mgol
  • 1,171
  • 13
  • 20
  • 1
    You said you're not expecting the bounty, but there is no answer I could mark as accepted, so according to SO bounty rules you will probably get it anyway. ;) – mik01aj Aug 21 '16 at 14:09
2

Actually you can see all the supported browsers by history lib: https://github.com/browserstate/history.js/#browsers-tested-and-working-in

And buglist solved by this lib, from the same page:

  • HTML5 Browsers
    • Chrome 8 sometimes does not contain the correct state data when traversing back to the initial state
    • Safari 5, Safari iOS 4 and Firefox 3 and 4 do not fire the onhashchange event when the page is loaded with a hash
    • Safari 5 and Safari iOS 4 do not fire the onpopstate event when the hash has changed unlike the other browsers
    • Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a replaceState call / bug report
    • Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions / bug report
    • Google Chrome 8,9,10 and Firefox 4 prior to the RC will always fire onpopstate once the page has loaded / change recommendation
    • Safari iOS 4.0, 4.1, 4.2 have a working HTML5 History API - although the actual back buttons of the browsers do not work, therefore we treat them as HTML4 browsers
    • None of the HTML5 browsers actually utilise the title argument to the pushState and replaceState calls
  • HTML4 Browsers
    • Old browsers like MSIE 6,7 and Firefox 2 do not have a onhashchange event
    • MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
    • Non-Opera HTML4 browsers sometimes do not apply the hash when the hash is not urlencoded
  • All Browsers
    • State data and titles do not persist once the site is left and then returned (includes page refreshes)
    • State titles are never applied to the document.title

This might tell you something about existing differences.

mik01aj
  • 10,150
  • 11
  • 65
  • 109
  • 1
    All the bug descriptions there are quite old. – mgol Aug 16 '16 at 08:43
  • @Artur, thank you for the bug list. It's good but note the project is quite old. I'm still not sure if this necessarily means "no more bugs in browsers since 2013". – mik01aj Aug 16 '16 at 08:56
0

There were some issues with History API in Androids before 4 version, but generally it work well across all main browsers.

Anton Kononenko
  • 437
  • 2
  • 13