Skip to content

Avoid first popstate event with async guard together (fix #1508) #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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