feat: preserve home scroll position
This commit is contained in:
parent
99a028a64f
commit
185a671920
1 changed files with 10 additions and 2 deletions
|
@ -64,28 +64,36 @@ const {postDivSelector, skipSave = false} = Astro.props;
|
||||||
link.href = url;
|
link.href = url;
|
||||||
link.onclick = async (e) => {
|
link.onclick = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
localStorage.setItem('scrollPosition', window.scrollY.toString());
|
||||||
scroll(0,0);
|
scroll(0,0);
|
||||||
renderPost(responseText, url, postDivSelector?.value)
|
renderPost(responseText, url, postDivSelector?.value)
|
||||||
}
|
}
|
||||||
const item = document.createElement('li');
|
const item = document.createElement('li');
|
||||||
item.appendChild(link);
|
item.appendChild(link);
|
||||||
list?.appendChild(item);
|
list?.appendChild(item);
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('popstate', async (data) => {
|
window.addEventListener('popstate', async (data) => {
|
||||||
let url = data.state?.url;
|
let url = data.state?.url;
|
||||||
|
let isHome = false;
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
url = window.location.href;
|
url = window.location.href;
|
||||||
|
isHome = true;
|
||||||
|
} else {
|
||||||
|
// replace scrollPosition
|
||||||
|
localStorage.setItem('scrollPosition', window.scrollY.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const fullResponse = await cache.match(url)
|
const fullResponse = await cache.match(url)
|
||||||
fullResponse?.text().then(data => {
|
fullResponse?.text().then(data => {
|
||||||
const responseText = data;
|
const responseText = data;
|
||||||
renderPost(responseText, url, postDivSelector?.value, true);
|
renderPost(responseText, url, postDivSelector?.value, true);
|
||||||
|
if (isHome) {
|
||||||
|
const scrollPosition = localStorage.getItem('scrollPosition');
|
||||||
|
scrollTo(0, scrollPosition ? parseInt(scrollPosition) : 0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue