rails/turbolinks — pull request #49 by vickash (2012-09-29 23:06:17+00:00)
Title: Support WebKit on iOS5+

Or any other browser that implements the History API, but not `window.history.state`.

I switched an app I'm working on from pjax to turbolinks and it started doing full reloads on iOS 5 Safari. Apparently that version of WebKit (possibly others?) supports the History API, but doesn't implement `window.history.state`.

turbolinks uses this to set `currentState`. It uses `currentState.position` to key on `pageCache`, so everything (not just history) gets broken without `window.history.state`.

I'm not sure exactly how pjax works around this, but it seems pretty simple. I've kept it using `window.history.state` if it exists, but otherwise:

1) To initialize, set the position to `window.history.length -1`, just as in `rememberCurrentUrl`.
2) On `visit`, set position to 1+position of the last state, i.e. `window.history.length - 1 + 1`.
3) On `popstate` set the position to `state.position` from the event.

It could use some refactoring, but I just wanted to get some feedback. It works great on an old iPod Touch I'm testing with (iOS 5.1.1), but I'm not too familiar with the History API. Does this make sense, or is it too much of a hack?