Skip to content

Commit 63521df

Browse files
committed
handle history deeplink fallback in IE9 (close #342)
1 parent 416714c commit 63521df

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

src/index.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,22 @@ class Router {
6464
this._beforeEachHooks = []
6565
this._afterEachHooks = []
6666

67-
// feature detection
68-
this._hasPushState =
69-
typeof window !== 'undefined' &&
70-
window.history &&
71-
window.history.pushState
72-
7367
// trigger transition on initial render?
7468
this._rendered = false
7569
this._transitionOnLoad = transitionOnLoad
7670

7771
// history mode
72+
this._root = root
7873
this._abstract = abstract
7974
this._hashbang = hashbang
80-
this._history = this._hasPushState && history
8175

82-
// other options
83-
this._saveScrollPosition = saveScrollPosition
84-
this._linkActiveClass = linkActiveClass
85-
this._suppress = suppressTransitionError
76+
// check if HTML5 history is available
77+
const hasPushState =
78+
typeof window !== 'undefined' &&
79+
window.history &&
80+
window.history.pushState
81+
this._history = history && hasPushState
82+
this._historyFallback = history && !hasPushState
8683

8784
// create history object
8885
const inBrowser = Vue.util.inBrowser
@@ -93,14 +90,18 @@ class Router {
9390
: 'hash'
9491

9592
const History = historyBackends[this.mode]
96-
const self = this
9793
this.history = new History({
9894
root: root,
9995
hashbang: this._hashbang,
100-
onChange: function (path, state, anchor) {
101-
self._match(path, state, anchor)
96+
onChange: (path, state, anchor) => {
97+
this._match(path, state, anchor)
10298
}
10399
})
100+
101+
// other options
102+
this._saveScrollPosition = saveScrollPosition
103+
this._linkActiveClass = linkActiveClass
104+
this._suppress = suppressTransitionError
104105
}
105106

106107
// API ===================================================
@@ -263,6 +264,25 @@ class Router {
263264
// give it a name for better debugging
264265
Ctor.options.name = Ctor.options.name || 'RouterApp'
265266
}
267+
268+
// handle history fallback in browsers that do not
269+
// support HTML5 history API
270+
if (this._historyFallback) {
271+
const location = window.location
272+
const history = new HTML5History({ root: this._root })
273+
const path = history.root
274+
? location.pathname.replace(history.rootRE, '')
275+
: location.pathname
276+
if (path && path !== '/') {
277+
location.assign(
278+
(history.root || '') + '/' +
279+
this.history.formatPath(path) +
280+
location.search
281+
)
282+
return
283+
}
284+
}
285+
266286
this.history.start()
267287
}
268288

test/unit/specs/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ describe('Stringify Path', function () {
11131113

11141114
var router
11151115
beforeEach(function () {
1116-
router = new Router({ abstract: true })
1116+
router = new Router({ abstract: true })
11171117
})
11181118

11191119
it('plain string', function () {

0 commit comments

Comments
 (0)