From 395a590539eaa804a0bf36ed6c00f6feec6c93cb Mon Sep 17 00:00:00 2001 From: Ryan Florence Date: Sun, 27 Jul 2014 00:46:44 -0600 Subject: [PATCH] [changed] fallback to window.location for history MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `` 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 --- modules/stores/URLStore.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/modules/stores/URLStore.js b/modules/stores/URLStore.js index 6cb3550d99..949a888736 100644 --- a/modules/stores/URLStore.js +++ b/modules/stores/URLStore.js @@ -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(); @@ -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') { @@ -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"' ); @@ -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;