Skip to content

Commit

Permalink
fix: avoid first popstate event with async guard together (fix #1508) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjiang authored and yyx990803 committed Oct 11, 2017
1 parent a06e0b2 commit 3cbc0f3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/history/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type Router from '../index'
import { History } from './base'
import { cleanPath } from '../util/path'
import { START } from '../util/route'
import { setupScroll, handleScroll } from '../util/scroll'
import { pushState, replaceState } from '../util/push-state'

Expand All @@ -16,9 +17,18 @@ export class HTML5History extends History {
setupScroll()
}

const initLocation = getLocation(this.base)
window.addEventListener('popstate', e => {
const current = this.current
this.transitionTo(getLocation(this.base), route => {

// Avoiding first `popstate` event dispatched in some browsers but first
// history route not updated since async guard at the same time.
const location = getLocation(this.base)
if (this.current === START && location === initLocation) {
return
}

this.transitionTo(location, route => {
if (expectScroll) {
handleScroll(router, route, current, true)
}
Expand Down

0 comments on commit 3cbc0f3

Please sign in to comment.