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

Enable frame morphing for data-turbo-frame links and forms if the URL doesn't change #1316

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions src/core/frames/frame_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "../../util"
import { FormSubmission } from "../drive/form_submission"
import { Snapshot } from "../snapshot"
import { getAction, expandURL, urlsAreEqual, locationIsVisitable } from "../url"
import { getAction, expandURL, urlsAreEqual, pathnamesAreEqual, locationIsVisitable } from "../url"
import { FormSubmitObserver } from "../../observers/form_submit_observer"
import { FrameView } from "./frame_view"
import { LinkInterceptor } from "./link_interceptor"
Expand Down Expand Up @@ -91,10 +91,9 @@ export class FrameController {
}

sourceURLReloaded() {
const { refresh, src } = this.element

this.#shouldMorphFrame = src && refresh === "morph"
const { src } = this.element

this.#shouldMorphFrame = this.element.shouldReloadWithMorph
this.element.removeAttribute("complete")
this.element.src = null
this.element.src = src
Expand Down Expand Up @@ -234,6 +233,7 @@ export class FrameController {
const frame = this.#findFrameElement(formSubmission.formElement, formSubmission.submitter)

frame.delegate.proposeVisitIfNavigatedWithAction(frame, getVisitAction(formSubmission.submitter, formSubmission.formElement, frame))
frame.delegate.decideOnMorphingTo(response.response.url)
frame.delegate.loadResponse(response)

if (!formSubmission.isSafe) {
Expand Down Expand Up @@ -343,10 +343,17 @@ export class FrameController {
frame.delegate.proposeVisitIfNavigatedWithAction(frame, getVisitAction(submitter, element, frame))

this.#withCurrentNavigationElement(element, () => {
frame.delegate.decideOnMorphingTo(url)
frame.src = url
})
}

decideOnMorphingTo(url) {
if (this.element.src && pathnamesAreEqual(this.element.src, url) && this.element.shouldReloadWithMorph) {
this.#shouldMorphFrame = true
}
}

proposeVisitIfNavigatedWithAction(frame, action = null) {
this.action = action

Expand Down
4 changes: 4 additions & 0 deletions src/core/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function urlsAreEqual(left, right) {
return expandURL(left).href == expandURL(right).href
}

export function pathnamesAreEqual(left, right) {
return expandURL(left).pathname == expandURL(right).pathname
}

function getPathComponents(url) {
return url.pathname.split("/").slice(1)
}
Expand Down
36 changes: 34 additions & 2 deletions src/tests/functional/frame_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ test("following a link to a page with a matching frame does not dispatch a turbo
)
})

test("following a link within a frame which has a target set navigates the target frame without morphing even when frame[refresh=morph]", async ({ page }) => {
test("following a link within a frame which has a target set navigates the target frame without morphing even when frame[refresh=morph] if the url changes", async ({ page }) => {
await page.click("#add-refresh-morph-to-frame")
await page.click("#hello a")
await nextBeat()
Expand All @@ -277,7 +277,7 @@ test("following a link within a frame which has a target set navigates the targe
await expect(page.locator("#frame h2")).toHaveText("Frame: Loaded")
})

test("navigating from within replaces the contents even with turbo-frame[refresh=morph]", async ({ page }) => {
test("navigating from within replaces the contents even with turbo-frame[refresh=morph] if the url changes", async ({ page }) => {
await page.click("#add-refresh-morph-to-frame")
await page.click("#link-frame")
await nextBeat()
Expand All @@ -287,6 +287,38 @@ test("navigating from within replaces the contents even with turbo-frame[refresh
await expect(page.locator("#frame h2")).toHaveText("Frame: Loaded")
})

test("following a link that targets a frame with refresh morph morphs the page if the url doesn't change", async ({ page }) => {
await page.click("#add-refresh-morph-to-frame")
await page.click("#outside-frame-form")
await nextBeat()

expect(await nextEventOnTarget(page, "frame", "turbo:before-frame-render")).toBeTruthy()
expect(await noNextEventOnTarget(page, "frame", "turbo:before-frame-morph")).toBeTruthy()

await page.click("#outside-frame-form")
await nextBeat()

expect(await nextEventOnTarget(page, "frame", "turbo:before-frame-morph")).toBeTruthy()
expect(await noNextEventOnTarget(page, "frame", "turbo:before-frame-render")).toBeTruthy()
})

test("submitting a form that targets a frame with refresh morph morphs the page if the url doesn't change", async ({ page }) => {
test.setTimeout(3000)
expect(await noNextEventOnTarget(page, "frame", "turbo:before-frame-render")).toBeTruthy()
await page.click("#add-refresh-morph-to-frame")
await page.click("#button-frame-action-advance")
await nextBeat()

expect(await nextEventOnTarget(page, "frame", "turbo:before-frame-render")).toBeTruthy()
expect(await noNextEventOnTarget(page, "frame", "turbo:before-frame-morph")).toBeTruthy()

await page.click("#button-frame-action-advance")
await nextBeat()

expect(await nextEventOnTarget(page, "frame", "turbo:before-frame-morph")).toBeTruthy()
expect(await noNextEventOnTarget(page, "frame", "turbo:before-frame-render")).toBeTruthy()
})

test("calling reload on a frame replaces the contents", async ({ page }) => {
await page.click("#add-src-to-frame")

Expand Down
Loading