mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-20 22:35:09 +00:00
handle hashchange correctly
This commit is contained in:
@@ -57,7 +57,9 @@ const app = {
|
|||||||
hydrate: !!component
|
hydrate: !!component
|
||||||
});
|
});
|
||||||
|
|
||||||
window.scrollTo(scroll.x, scroll.y);
|
if (scroll) {
|
||||||
|
window.scrollTo(scroll.x, scroll.y);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,11 +121,7 @@ const app = {
|
|||||||
const url = new URL(href);
|
const url = new URL(href);
|
||||||
|
|
||||||
// Don't handle hash changes
|
// Don't handle hash changes
|
||||||
if (url.pathname === window.location.pathname && url.search === window.location.search) {
|
if (url.pathname === window.location.pathname && url.search === window.location.search) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const scroll = scroll_state();
|
|
||||||
|
|
||||||
if (navigate(url, null)) {
|
if (navigate(url, null)) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -147,10 +145,15 @@ const app = {
|
|||||||
window.addEventListener('mouseover', preload);
|
window.addEventListener('mouseover', preload);
|
||||||
|
|
||||||
window.addEventListener('popstate', event => {
|
window.addEventListener('popstate', event => {
|
||||||
if (!event.state) return; // hashchange, or otherwise outside sapper's control
|
|
||||||
scroll_history[cid] = scroll_state();
|
scroll_history[cid] = scroll_state();
|
||||||
|
|
||||||
navigate(new URL(window.location), event.state.id);
|
if (event.state) {
|
||||||
|
navigate(new URL(window.location), event.state.id);
|
||||||
|
} else {
|
||||||
|
// hashchange
|
||||||
|
cid = ++uid;
|
||||||
|
history.replaceState({ id: cid }, '', window.location.href);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const scroll = scroll_history[uid] = scroll_state();
|
const scroll = scroll_history[uid] = scroll_state();
|
||||||
|
|||||||
Reference in New Issue
Block a user