Skip to content

Commit

Permalink
[changed] fallback to window.location for history
Browse files Browse the repository at this point in the history
When `<Routes location=“history”/>` is used
we now fall back to window.location for browsers
that don’t support the HTML5 history API

Rather than falling back to hash, falling back to
window.location ensures urls are identical for
all users, and sharing them will always work

closes #50
  • Loading branch information
ryanflorence committed Jul 27, 2014
1 parent 2a3582e commit 395a590
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions modules/stores/URLStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var URLStore = {
if (path === _currentPath)
return;

if (_location === 'disabledHistory')
return window.location = path;

if (_location === 'history') {
window.history.pushState({ path: path }, '', path);
notifyChange();
Expand All @@ -87,7 +90,9 @@ var URLStore = {
* to the browser's history.
*/
replace: function (path) {
if (_location === 'history') {
if (_location === 'disabledHistory') {
window.location.replace(path);
} else if (_location === 'history') {
window.history.replaceState({ path: path }, '', path);
notifyChange();
} else if (_location === 'hash') {
Expand Down Expand Up @@ -143,10 +148,15 @@ var URLStore = {
return; // Don't setup twice.
}

if (location === 'history' && !supportsHistory()) {
location = 'disabledHistory';
return;
}

var changeEvent = CHANGE_EVENTS[location];

invariant(
changeEvent,
changeEvent || location === 'disabledHistory',
'The URL store location "' + location + '" is not valid. ' +
'It must be either "hash" or "history"'
);
Expand Down Expand Up @@ -185,4 +195,19 @@ var URLStore = {

};

function supportsHistory() {
/*! taken from modernizr
* https://github.com/Modernizr/Modernizr/blob/master/LICENSE
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js
*/
var ua = navigator.userAgent;
if ((ua.indexOf('Android 2.') !== -1 ||
(ua.indexOf('Android 4.0') !== -1)) &&
ua.indexOf('Mobile Safari') !== -1 &&
ua.indexOf('Chrome') === -1) {
return false;
}
return (window.history && 'pushState' in window.history);
}

module.exports = URLStore;

0 comments on commit 395a590

Please sign in to comment.