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

Don't preserve scroll position unless it's a page refresh #1096

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/drive/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class PageView extends View {
return !visit || (this.lastRenderedLocation.href === visit.location.href && visit.action === "replace")
}

shouldPreserveScrollPosition(visit) {
return this.isPageRefresh(visit) && this.snapshot.shouldPreserveScrollPosition
}

get snapshot() {
return PageSnapshot.fromElement(this.element)
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/drive/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export class Visit {
// Scrolling

performScroll() {
if (!this.scrolled && !this.view.forceReloaded && !this.view.snapshot.shouldPreserveScrollPosition) {
if (!this.scrolled && !this.view.forceReloaded && !this.view.shouldPreserveScrollPosition(this)) {
if (this.action == "restore") {
this.scrollToRestoredPosition() || this.scrollToAnchor() || this.view.scrollToTop()
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/tests/fixtures/page_refresh.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<body>
<h1>Page to be refreshed</h1>

<a href="/src/tests/fixtures/page_refresh.html" id="reload-link">Reload</a>

<turbo-frame id="refresh-morph" src="/src/tests/fixtures/frame_refresh_morph.html" refresh="morph">
<h2>Frame to be morphed</h2>
</turbo-frame>
Expand Down
12 changes: 12 additions & 0 deletions src/tests/functional/page_refresh_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ test("it preserves the scroll position when the turbo-refresh-scroll meta tag is
await assertPageScroll(page, 10, 10)
})

test("it does not preserve the scroll position on regular 'advance' navigations, despite of using a 'preserve' option", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")

await page.evaluate(() => window.scrollTo(10, 10))
await assertPageScroll(page, 10, 10)

await page.evaluate(() => document.getElementById("reload-link").click())
await nextEventNamed(page, "turbo:render", { renderMethod: "replace" })

await assertPageScroll(page, 0, 0)
})

test("it resets the scroll position when the turbo-refresh-scroll meta tag is 'reset'", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh_scroll_reset.html")

Expand Down