handle hashchange correctly

This commit is contained in:
Rich Harris
2017-12-16 08:53:42 -05:00
parent 10ddaeb7a3
commit b3b5d9f352

View File

@@ -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();