Skip to content

Commit

Permalink
Prevent refresh with stale URL
Browse files Browse the repository at this point in the history
  • Loading branch information
klevo committed Jun 29, 2024
1 parent 14284e6 commit 0598f10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export class Session {

refresh(url, requestId) {
const isRecentRequest = requestId && this.recentRequests.has(requestId)
if (!isRecentRequest && !this.navigator.currentVisit) {
const isCurrentUrl = url === document.baseURI
if (!isRecentRequest && !this.navigator.currentVisit && isCurrentUrl) {
this.visit(url, { action: "replace", shouldCacheSnapshot: false })
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/page_refresh_stream_action.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<div id="content">
<span>Hello</span>
<a id="regular-link" href="/src/tests/fixtures/one.html">Regular link</a>
</div>
</body>
</html>
20 changes: 19 additions & 1 deletion src/tests/functional/page_refresh_stream_action_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "@playwright/test"
import { assert } from "chai"
import { nextPageRefresh, readEventLogs } from "../helpers/page"
import { nextPageRefresh, readEventLogs, pathname } from "../helpers/page"

test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh_stream_action.html")
Expand Down Expand Up @@ -54,6 +54,20 @@ test("debounce stream page refreshes", async ({ page }) => {
assert.equal(requestLogs.length, 2)
})

test("debounced refresh of stale URL does not hijack new location navigated to", async ({ page }) => {
await setLongerPageRefreshDebouncePeriod(page)
const urlBeforeVisit = page.url()

await page.click("#refresh button")
await page.click("#regular-link")
await nextPageRefresh(page)

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
const expectedPath = "/src/tests/fixtures/one.html"
assert.equal(pathname(urlAfterVisit), expectedPath)
})

async function textContent(page) {
const messages = await page.locator("#content")
return await messages.textContent()
Expand All @@ -65,3 +79,7 @@ async function fetchRequestId(page) {
return response.text()
})
}

async function setLongerPageRefreshDebouncePeriod(page, period = 500) {
return page.evaluate((period) => window.Turbo.session.pageRefreshDebouncePeriod = period, period)
}

0 comments on commit 0598f10

Please sign in to comment.