Skip to content

Commit dda0f71

Browse files
authored
Stabilize pagination scroll restoration (#40)
Prevent native browser history restoration from overriding Orbit's pagination scroll-to-top marker across hosted Chromium versions.
1 parent 360a738 commit dda0f71

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

‎src/components/Pagination.astro‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ const olderPage = currentPage < totalPages ? currentPage + 1 : undefined;
4646
});
4747

4848
if (sessionStorage.getItem(scrollKey) === 'true') {
49+
const previousScrollRestoration = history.scrollRestoration;
50+
const scrollToTop = () => window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
51+
4952
sessionStorage.removeItem(scrollKey);
50-
requestAnimationFrame(() => window.scrollTo({ top: 0, left: 0, behavior: 'instant' }));
53+
history.scrollRestoration = 'manual';
54+
scrollToTop();
55+
requestAnimationFrame(scrollToTop);
56+
57+
window.addEventListener('pageshow', () => {
58+
scrollToTop();
59+
requestAnimationFrame(() => {
60+
scrollToTop();
61+
history.scrollRestoration = previousScrollRestoration;
62+
});
63+
}, { once: true });
5164
}
5265
</script>

0 commit comments

Comments
 (0)