Skip to content

Commit

Permalink
Fix: don't preserve scroll position unless it's a page refresh
Browse files Browse the repository at this point in the history
Fix #1081
  • Loading branch information
jorgemanrubia committed Dec 2, 2023
1 parent 5c48398 commit 7bdb1a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 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.shouldPreserveScrollPosition) {
if (this.action == "restore") {
this.scrollToRestoredPosition() || this.scrollToAnchor() || this.view.scrollToTop()
} else {
Expand All @@ -349,6 +349,10 @@ export class Visit {
}
}

get shouldPreserveScrollPosition() {
return this.view.isPageRefresh(this) && this.view.snapshot.shouldPreserveScrollPosition
}

scrollToRestoredPosition() {
const { scrollPosition } = this.restorationData
if (scrollPosition) {
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

0 comments on commit 7bdb1a1

Please sign in to comment.