diff --git a/lib/router/router.js b/lib/router/router.js index 654576ee..df49e4b6 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -1,4 +1,4 @@ -import { parse } from 'url' +import { parse, format } from 'url' import evalScript from '../eval-script' import shallowEquals from '../shallow-equals' @@ -18,6 +18,10 @@ export default class Router { this.onPopState = this.onPopState.bind(this) if (typeof window !== 'undefined') { + // in order for `e.state` to work on the `onpopstate` event + // we have to register the initial route upon initialization + this.replace(format({ pathname, query }), getURL()) + window.addEventListener('popstate', this.onPopState) } } @@ -131,7 +135,7 @@ export default class Router { console.error(err) } - if (getURL() !== url) { + if (method !== 'pushState' || getURL() !== url) { window.history[method]({ route }, null, url) }