Skip to content

Commit

Permalink
Prevent Refresh from interrupting ongoing Visit
Browse files Browse the repository at this point in the history
Closes [#1209]

Defers the `Session.refresh` calls while handling `<turbo-stream
action="refresh">` elements if there is a `Visit` that's already
initiated and being handled by the `Navigator`.

[#1209]: #1209
  • Loading branch information
seanpdoyle committed Mar 1, 2024
1 parent 00527e5 commit 04e5c65
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class Session {

refresh(url, requestId) {
const isRecentRequest = requestId && this.recentRequests.has(requestId)
if (!isRecentRequest) {
if (!isRecentRequest && !this.navigator.currentVisit) {
this.visit(url, { action: "replace", shouldCacheSnapshot: false })
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/page_refresh.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<h1 id="title">Page to be refreshed</h1>

<a href="/src/tests/fixtures/page_refresh.html" id="reload-link">Reload</a>
<a href="/__turbo/delayed_response" id="delayed_link">Navigate with delayed response</a>

<turbo-frame id="refresh-morph" src="/src/tests/fixtures/frame_refresh_morph.html" refresh="morph">
<h2>Frame to be morphed</h2>
Expand Down
17 changes: 12 additions & 5 deletions src/tests/functional/page_refresh_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
nextEventOnTarget,
noNextEventOnTarget,
noNextEventNamed,
getSearchParam
getSearchParam,
refreshWithStream
} from "../helpers/page"

test("renders a page refresh with morphing", async ({ page }) => {
Expand All @@ -26,16 +27,22 @@ test("async page refresh with turbo-stream", async ({ page }) => {

await page.evaluate(() => document.querySelector("#title").innerText = "Updated")
await expect(page.locator("#title")).toHaveText("Updated")

await page.evaluate(() => {
document.body.insertAdjacentHTML("beforeend", `<turbo-stream action="refresh"></turbo-stream>`)
})
await refreshWithStream(page)

await expect(page.locator("#title")).not.toHaveText("Updated")
await expect(page.locator("#title")).toHaveText("Page to be refreshed")
expect(await noNextEventNamed(page, "turbo:before-cache")).toBeTruthy()
})

test("async page refresh with turbo-stream does not interrupt an initiated Visit", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")
await page.pause()
await page.click("#delayed_link")
await refreshWithStream(page)

await expect(page.locator("h1")).toHaveText("One")
})

test("dispatches a turbo:before-morph-element and turbo:morph-element event for each morphed element", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")
await page.fill("#form-text", "Morph me")
Expand Down
4 changes: 4 additions & 0 deletions src/tests/helpers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ export function readMutationLogs(page, length) {
return readArray(page, "mutationLogs", length)
}

export function refreshWithStream(page) {
return page.evaluate(() => document.body.insertAdjacentHTML("beforeend", `<turbo-stream action="refresh"></turbo-stream>`))
}

export function search(url) {
const { search } = new URL(url)

Expand Down

0 comments on commit 04e5c65

Please sign in to comment.