diff --git a/src/core/session.js b/src/core/session.js index 1047d4463..d66146fe8 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -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 }) } } diff --git a/src/tests/fixtures/page_refresh_stream_action.html b/src/tests/fixtures/page_refresh_stream_action.html index 4fb80d1c8..2d8ae2e97 100644 --- a/src/tests/fixtures/page_refresh_stream_action.html +++ b/src/tests/fixtures/page_refresh_stream_action.html @@ -14,6 +14,7 @@
Hello + Regular link
diff --git a/src/tests/functional/page_refresh_stream_action_tests.js b/src/tests/functional/page_refresh_stream_action_tests.js index 0bc865922..d60853cae 100644 --- a/src/tests/functional/page_refresh_stream_action_tests.js +++ b/src/tests/functional/page_refresh_stream_action_tests.js @@ -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") @@ -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() @@ -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) +}