Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#8625): smooth scrolling in SPA mode on iOS #10235

Merged
merged 5 commits into from
Mar 3, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 31 additions & 3 deletions packages/astro/src/transitions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ function onPopState(ev: PopStateEvent) {

// There's not a good way to record scroll position before a back button.
// So the way we do it is by listening to scrollend if supported, and if not continuously record the scroll position.
const onScroll = () => {
const onScrollEnd = () => {
updateScrollPosition({ scrollX, scrollY });
};

Expand All @@ -637,8 +637,36 @@ if (inBrowser) {
originalLocation = new URL(location.href);
addEventListener('popstate', onPopState);
addEventListener('load', onPageLoad);
if ('onscrollend' in window) addEventListener('scrollend', onScroll);
else addEventListener('scroll', throttle(onScroll, 350), { passive: true });
martrapp marked this conversation as resolved.
Show resolved Hide resolved
if ('onscrollend' in window) addEventListener('scrollend', onScrollEnd);
else {
// Keep track of state between intervals
let intervalId: number | undefined, lastY = scrollY, lastX = scrollX, lastIndex = history.state?.index;
const scrollInterval = () => {
// Check the index if a popstate event fired between intervals
if (lastIndex !== history.state?.index) {
// Cancel the interval because we're probably not scrolling
clearInterval(intervalId);
intervalId = undefined;
// Update index and return
lastIndex = history.state?.index;
return;
}
// Check if the user stopped scrolling *and* if the positions need to be updated
if (
lastY === scrollY && lastX === scrollX
&& (lastY !== history.state.scrollY || lastX !== history.state.scrollX)
) {
// Cancel the interval and update scroll positions
clearInterval(intervalId);
intervalId = undefined;
onScrollEnd();
martrapp marked this conversation as resolved.
Show resolved Hide resolved
}
martrapp marked this conversation as resolved.
Show resolved Hide resolved
// Update variables for the next interval
lastIndex = history.state?.index, lastY = scrollY, lastX = scrollX;
martrapp marked this conversation as resolved.
Show resolved Hide resolved
}
// We can't know when or how often scroll events fire, so we'll just use them to start intervals
addEventListener("scroll", () => intervalId ??= window.setInterval(scrollInterval, 200), { passive: true });
martrapp marked this conversation as resolved.
Show resolved Hide resolved
};
}
for (const script of document.scripts) {
script.dataset.astroExec = '';
Expand Down