@@ -64,25 +64,22 @@ class Router {
64
64
this . _beforeEachHooks = [ ]
65
65
this . _afterEachHooks = [ ]
66
66
67
- // feature detection
68
- this . _hasPushState =
69
- typeof window !== 'undefined' &&
70
- window . history &&
71
- window . history . pushState
72
-
73
67
// trigger transition on initial render?
74
68
this . _rendered = false
75
69
this . _transitionOnLoad = transitionOnLoad
76
70
77
71
// history mode
72
+ this . _root = root
78
73
this . _abstract = abstract
79
74
this . _hashbang = hashbang
80
- this . _history = this . _hasPushState && history
81
75
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
86
83
87
84
// create history object
88
85
const inBrowser = Vue . util . inBrowser
@@ -93,14 +90,18 @@ class Router {
93
90
: 'hash'
94
91
95
92
const History = historyBackends [ this . mode ]
96
- const self = this
97
93
this . history = new History ( {
98
94
root : root ,
99
95
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 )
102
98
}
103
99
} )
100
+
101
+ // other options
102
+ this . _saveScrollPosition = saveScrollPosition
103
+ this . _linkActiveClass = linkActiveClass
104
+ this . _suppress = suppressTransitionError
104
105
}
105
106
106
107
// API ===================================================
@@ -263,6 +264,25 @@ class Router {
263
264
// give it a name for better debugging
264
265
Ctor . options . name = Ctor . options . name || 'RouterApp'
265
266
}
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
+
266
286
this . history . start ( )
267
287
}
268
288
0 commit comments