Skip to content
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

Properly handle hash URL changes. #996

Merged
merged 3 commits into from
Feb 5, 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
18 changes: 13 additions & 5 deletions lib/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ export default class Router extends EventEmitter {
}

async onPopState (e) {
// Older versions of safari and chrome tend to fire popstate event at the
// page load.
// We should not complete that event and the following check will fix it.
// Fixes:
if (!e.state) {
// We get state as undefined for two reasons.
// 1. With older safari (< 8) and older chrome (< 34)
// 2. When the URL changed with #
//
// In the both cases, we don't need to proceed and change the route.
// (as it's already changed)
// But we can simply replace the state with the new changes.
// Actually, for (1) we don't need to nothing. But it's hard to detect that event.
// So, doing the following for (1) does no harm.
const { pathname, query } = this
this.replace(format({ pathname, query }), getURL())
return
}

Expand Down Expand Up @@ -276,7 +283,8 @@ export default class Router extends EventEmitter {
}

function getURL () {
return window.location.pathname + (window.location.search || '') + (window.location.hash || '')
const { href, origin } = window.location
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have just # on the URL, window.location.hash simply shows a empty string.
So, we can't generate the correct URL.

But this method does that.

return href.substring(origin.length)
}

function toRoute (path) {
Expand Down