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

Don't break out of frames when frame missing #863

Merged
merged 3 commits into from
Feb 7, 2023
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/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class TurboFrameMissingError extends Error {}
20 changes: 12 additions & 8 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { VisitOptions } from "../drive/visit"
import { TurboBeforeFrameRenderEvent } from "../session"
import { StreamMessage } from "../streams/stream_message"
import { PageSnapshot } from "../drive/page_snapshot"
import { TurboFrameMissingError } from "../errors"

type VisitFallback = (location: Response | Locatable, options: Partial<VisitOptions>) => Promise<void>
export type TurboFrameMissingEvent = CustomEvent<{ response: Response; visit: VisitFallback }>
Expand Down Expand Up @@ -181,15 +182,9 @@ export class FrameController
session.frameLoaded(this.element)
this.fetchResponseLoaded(fetchResponse)
} else if (this.willHandleFrameMissingFromResponse(fetchResponse)) {
console.warn(
`A matching frame for #${this.element.id} was missing from the response, transforming into full-page Visit.`
)
this.visitResponse(fetchResponse.response)
this.handleFrameMissingFromResponse(fetchResponse)
}
}
} catch (error) {
console.error(error)
this.view.invalidate()
} finally {
this.fetchResponseLoaded = () => {}
}
Expand Down Expand Up @@ -264,7 +259,6 @@ export class FrameController
}

async requestFailedWithResponse(request: FetchRequest, response: FetchResponse) {
console.error(response)
await this.loadResponse(response)
this.resolveVisitPromise()
}
Expand Down Expand Up @@ -432,6 +426,16 @@ export class FrameController
return !event.defaultPrevented
}

private handleFrameMissingFromResponse(fetchResponse: FetchResponse) {
this.view.missing()
this.throwFrameMissingError(fetchResponse)
}

private throwFrameMissingError(fetchResponse: FetchResponse) {
const message = `The response (${fetchResponse.statusCode}) did not contain the expected <turbo-frame id="${this.element.id}">`
throw new TurboFrameMissingError(message)
}

private async visitResponse(response: Response): Promise<void> {
const wrapped = new FetchResponse(response)
const responseHTML = await wrapped.responseHTML
Expand Down
4 changes: 2 additions & 2 deletions src/core/frames/frame_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { View, ViewRenderOptions } from "../view"
export type FrameViewRenderOptions = ViewRenderOptions<FrameElement>

export class FrameView extends View<FrameElement> {
invalidate() {
this.element.innerHTML = ""
missing() {
this.element.innerHTML = `<strong class="turbo-frame-error">Content missing</strong>`
}

get snapshot() {
Expand Down
46 changes: 24 additions & 22 deletions src/tests/functional/frame_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { assert, Assertion } from "chai"
import {
attributeForSelector,
hasSelector,
innerHTMLForSelector,
listenForEventOnTarget,
nextAttributeMutationNamed,
noNextAttributeMutationNamed,
Expand Down Expand Up @@ -142,47 +141,50 @@ test("test submitting a form[data-turbo-frame=_top] does not toggle the frame's
)
})

test("test successfully following a link to a page without a matching frame dispatches a turbo:frame-missing event", async ({
test("successfully following a link to a page without a matching frame dispatches a turbo:frame-missing event", async ({
page,
}) => {
await page.click("#missing-frame-link")
await nextEventOnTarget(page, "missing", "turbo:before-fetch-request")
const { response } = await nextEventOnTarget(page, "missing", "turbo:frame-missing")

assert.ok(await noNextEventNamed(page, "turbo:before-fetch-request"))
assert.equal(200, response.status)
})

await nextEventNamed(page, "turbo:load")
test("successfully following a link to a page without a matching frame shows an error and throws an exception", async ({
page,
}) => {
let error: Error | undefined = undefined
page.once("pageerror", (e) => (error = e))

assert.ok(response, "dispatches turbo:frame-missing with event.detail.response")
assert.equal(pathname(page.url()), "/src/tests/fixtures/frames/frame.html", "navigates the page")
await page.click("#missing-frame-link")

await page.goBack()
await nextEventNamed(page, "turbo:load")
assert.match(await page.innerText("#missing"), /Content missing/)

assert.equal(pathname(page.url()), "/src/tests/fixtures/frames.html")
assert.ok(await innerHTMLForSelector(page, "#missing"))
assert.exists(error)
assert.equal(error!.message, `The response (200) did not contain the expected <turbo-frame id="missing">`)
})

test("test failing to follow a link to a page without a matching frame dispatches a turbo:frame-missing event", async ({
test("failing to follow a link to a page without a matching frame dispatches a turbo:frame-missing event", async ({
page,
}) => {
await page.click("#missing-page-link")
await nextEventOnTarget(page, "missing", "turbo:before-fetch-request")
const { response } = await nextEventOnTarget(page, "missing", "turbo:frame-missing")

assert.ok(await noNextEventNamed(page, "turbo:before-fetch-request"))
assert.ok(await noNextEventNamed(page, "turbo:load"))
assert.equal(404, response.status)
})

await nextEventNamed(page, "turbo:render")
test("failing to follow a link to a page without a matching frame shows an error and throws an exception", async ({
page,
}) => {
let error: Error | undefined = undefined
page.once("pageerror", (e) => (error = e))

assert.ok(response, "dispatches turbo:frame-missing with event.detail.response")
assert.equal(pathname(page.url()), "/missing.html", "navigates the page")
await page.click("#missing-page-link")

await page.goBack()
await nextEventNamed(page, "turbo:load")
assert.match(await page.innerText("#missing"), /Content missing/)

assert.equal(pathname(page.url()), "/src/tests/fixtures/frames.html")
assert.ok(await innerHTMLForSelector(page, "#missing"))
assert.exists(error)
assert.equal(error!.message, `The response (404) did not contain the expected <turbo-frame id="missing">`)
})

test("test the turbo:frame-missing event following a link to a page without a matching frame can be handled", async ({
Expand Down