Conversation
|
What use case is the scroll listener for? Can we get away with
for storing the scroll position(s)? |
|
Good question. The main reason I integrated it here is that after some research almost all similar routing libraries do it this way. I remember |
|
I had good success with I'd also say that for something like restoring scroll positions, it's enough if it works 90% of the time :) |
|
@hirasso I remember now. We need the scroll handler for history visits. In the Technically, it would be possible with the new Navigation API, but it's basically Chrome only. |
|
Couldn't we just do it on Line 276 in 034503e ...and then, inside // current:
// this.cachedScrollPositions[cacheKey] = positions;
// new: url is coming from `visit.from.url` so it's always the previous one, even during popstate events:
updateHistoryRecord(url, { scrollPositions: positions });To recap: the // Bail early on popstate if not animated: browser will handle it
if (visit.history.popstate && !visit.animation.animate) {
return;
}Two more thoughts:
Lines 371 to 374 in 034503e But with history.pushState({page: 1}, "", "/example");
history.pushState({page: 2}, "", "/example");
history.pushState({page: 3}, "", "/example");History keeps confusing me 🐚😄 If it turns out that preserving the scroll position on forward (link-click) navigation doesn't work with We could still use it for restoring the scroll positions of All these are only considerations, I'm happy to be proven wrong or right via an actual implementation :) |
|
Btw, feel free to ignore me until you find time :) |
|
This is all theoretical, so some of this might not make much sense 🤠 Haven't touched the branch in a while.
The main goal is to store the scroll positions in history so a) different visits to a URL can have different scroll positions and b) the positions survive a reload. If we put them into the visit object, I don't see much of an improvement there. The visit object is gone after the visit, right?
That's also a (minor) goal of this PR. Remembering arbitrary scroll container setups. That would require giving them IDs in those advanced use cases, but e.g. going from the homepage with one scroll container to a filter page with three containers would then be possible technically.
Good point! Haven't thought about that part too much. I assume it could work by actually preventing the reset while it's happening (or before), instead of removing the data or setting it to
A lot of libraries, including Astro, seem to do it this way, so I'm hopeful 🤠 |
That was a misunderstanding. I didn't mean to store the positions in the visit object. Rather to use the url of |
I'd like to discuss this in a call sometime. In my mind, it's not an issue, but it might still well be one 😇 |
|
Call sounds fantastic 📞 |
|
Notes from our call:
Another note from my current project: Restoring scroll positions after a reload needs to be done after one |
|
As discussed, I (re-)started work on this in another branch (not yet published). The problem I soon stumbled upon:
So this is the big decision we need to make: **Is the benefit of having different stored scroll positions for each history entry (vs. only for each unique URL) worth dropping support for Pro
Con
Scroll restoration after a page reload could still be implemented via history state on Happy to discuss. |
|
maybe there is still a way to keep
No, bad idea. |
|
Can you catch me up on what |
https://swup.js.org/plugins/scroll-plugin/#shouldresetscrollposition
|
|
Observation: calling EDIT: Only Arc, only in a deprecated version of blink. |
|
Note to future selves: We decided that managing scroll positions via history state is not worth the trouble and would even break features like import Swup from "swup";
import { restoreScroll } from "@hirasso/restore-scroll";
function initPage() {
document.querySelectorAll(".overflow-y-auto,.overflow-x-auto,.overflow-auto").forEach((el) => restoreScroll(el));
}
initPage();
new Swup({
hooks: {
"page:view": initPage,
},
}); |
Most routing libraries currently store the scroll position in the history state and manually restore it from there.
We decided to stick with browser auto-restoration for the core, but to have this functionality in the scroll plugin.
Work in progress
First draft, not working properly, doesn't even build 🤠
Description
scroll:storewhen saving position,scroll:restorewhen restoring positionscrollfunction to update position when scrolling stopsPrior art
To-do
Checks
nextbranchnpm run lint)All tests are passing (npm run test)New or updated tests are included