Skip to content

Commit 395a590

Browse files
committed
[changed] fallback to window.location for 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
1 parent 2a3582e commit 395a590

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

modules/stores/URLStore.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ var URLStore = {
7070
if (path === _currentPath)
7171
return;
7272

73+
if (_location === 'disabledHistory')
74+
return window.location = path;
75+
7376
if (_location === 'history') {
7477
window.history.pushState({ path: path }, '', path);
7578
notifyChange();
@@ -87,7 +90,9 @@ var URLStore = {
8790
* to the browser's history.
8891
*/
8992
replace: function (path) {
90-
if (_location === 'history') {
93+
if (_location === 'disabledHistory') {
94+
window.location.replace(path);
95+
} else if (_location === 'history') {
9196
window.history.replaceState({ path: path }, '', path);
9297
notifyChange();
9398
} else if (_location === 'hash') {
@@ -143,10 +148,15 @@ var URLStore = {
143148
return; // Don't setup twice.
144149
}
145150

151+
if (location === 'history' && !supportsHistory()) {
152+
location = 'disabledHistory';
153+
return;
154+
}
155+
146156
var changeEvent = CHANGE_EVENTS[location];
147157

148158
invariant(
149-
changeEvent,
159+
changeEvent || location === 'disabledHistory',
150160
'The URL store location "' + location + '" is not valid. ' +
151161
'It must be either "hash" or "history"'
152162
);
@@ -185,4 +195,19 @@ var URLStore = {
185195

186196
};
187197

198+
function supportsHistory() {
199+
/*! taken from modernizr
200+
* https://github.com/Modernizr/Modernizr/blob/master/LICENSE
201+
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js
202+
*/
203+
var ua = navigator.userAgent;
204+
if ((ua.indexOf('Android 2.') !== -1 ||
205+
(ua.indexOf('Android 4.0') !== -1)) &&
206+
ua.indexOf('Mobile Safari') !== -1 &&
207+
ua.indexOf('Chrome') === -1) {
208+
return false;
209+
}
210+
return (window.history && 'pushState' in window.history);
211+
}
212+
188213
module.exports = URLStore;

0 commit comments

Comments
 (0)