@@ -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
0 commit comments