Skip to content

Commit 5b2854d

Browse files
arunodankzawa
authored andcommitted
Properly handle hash URL changes. (vercel#996)
* Properly handle hash URL changes. * Make sure we replace origin correctly. * Get rid of RegExp for getUrl().
1 parent a8731d0 commit 5b2854d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/router/router.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ export default class Router extends EventEmitter {
3434
}
3535

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

@@ -276,7 +283,8 @@ export default class Router extends EventEmitter {
276283
}
277284

278285
function getURL () {
279-
return window.location.pathname + (window.location.search || '') + (window.location.hash || '')
286+
const { href, origin } = window.location
287+
return href.substring(origin.length)
280288
}
281289

282290
function toRoute (path) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy