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

Prevent Refresh from interrupting ongoing Visit #1213

Merged
merged 1 commit into from
Jun 21, 2024
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
1 change: 1 addition & 0 deletions src/core/drive/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class Navigator {

visitCompleted(visit) {
this.delegate.visitCompleted(visit)
delete this.currentVisit
}

locationWithActionIsSamePage(location, action) {
Expand Down
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
4 changes: 4 additions & 0 deletions src/tests/fixtures/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
"turbo:reload"
])

window.visitLogs = []

addEventListener("turbo:visit", ({ detail }) => window.visitLogs.push(detail))

customElements.define(
"custom-link-element",
class extends HTMLElement {
Expand Down
27 changes: 22 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,32 @@ 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 sequentially initiate Visits", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")
await refreshWithStream(page)
await nextEventNamed(page, "turbo:morph")
await nextEventNamed(page, "turbo:load")

await refreshWithStream(page)
await nextEventNamed(page, "turbo:morph")
await nextEventNamed(page, "turbo:load")
})

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.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
10 changes: 8 additions & 2 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 Expand Up @@ -284,8 +288,10 @@ export function textContent(page, html) {
export function visitAction(page) {
return page.evaluate(() => {
try {
return window.Turbo.navigator.currentVisit.action
} catch (error) {
const lastVisit = window.visitLogs[window.visitLogs.length - 1]

return lastVisit.action
} catch {
return "load"
}
})
Expand Down
Loading