Skip to content

Commit ed72ba1

Browse files
committed
Create Frame Snapshot from Fetch Response HTML
After the changes made in [@hotwired/turbo#867][] and changes made in [@hotwired/turbo-rails#428][] (the canonical server-side implementation), Turbo expects full HTML documents in response to requests with `Turbo-Frame:` headers. Prior to this commit, the `FrameController` compensated for missing pieces of an HTML document by taking an HTML "snapshot" of the current page through the `<html>` element's [outerHTML][]. This commit changes the `fetchResponseLoaded` callback to read the `responseHTML` directly from the `FetchResponse`, since that will be a fully formed HTML document in Turbo v7.3.0 and later. To support that change, this commit also updates various `src/test/fixtures` files to render fully-formed HTML documents. [@hotwired/turbo#867]: #867 [@hotwired/turbo-rails#428]: hotwired/turbo-rails#428 [outerHTML]: https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML
1 parent c44664d commit ed72ba1

22 files changed

+113
-68
lines changed

src/core/frames/frame_controller.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { PageSnapshot } from "../drive/page_snapshot"
2626
import { TurboFrameMissingError } from "../errors"
2727

2828
export class FrameController {
29-
fetchResponseLoaded = (_fetchResponse) => {}
29+
fetchResponseLoaded = (_fetchResponse) => Promise.resolve()
3030
#currentFetchRequest = null
3131
#resolveVisitPromise = () => {}
3232
#connected = false
@@ -140,7 +140,7 @@ export class FrameController {
140140
}
141141
}
142142
} finally {
143-
this.fetchResponseLoaded = () => {}
143+
this.fetchResponseLoaded = () => Promise.resolve()
144144
}
145145
}
146146

@@ -315,7 +315,7 @@ export class FrameController {
315315
this.complete = true
316316
session.frameRendered(fetchResponse, this.element)
317317
session.frameLoaded(this.element)
318-
this.fetchResponseLoaded(fetchResponse)
318+
await this.fetchResponseLoaded(fetchResponse)
319319
} else if (this.#willHandleFrameMissingFromResponse(fetchResponse)) {
320320
this.#handleFrameMissingFromResponse(fetchResponse)
321321
}
@@ -354,10 +354,10 @@ export class FrameController {
354354
const pageSnapshot = PageSnapshot.fromElement(frame).clone()
355355
const { visitCachedSnapshot } = frame.delegate
356356

357-
frame.delegate.fetchResponseLoaded = (fetchResponse) => {
357+
frame.delegate.fetchResponseLoaded = async (fetchResponse) => {
358358
if (frame.src) {
359359
const { statusCode, redirected } = fetchResponse
360-
const responseHTML = frame.ownerDocument.documentElement.outerHTML
360+
const responseHTML = await fetchResponse.responseHTML
361361
const response = { statusCode, redirected, responseHTML }
362362
const options = {
363363
response,
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<!DOCTYPE html>
22
<html>
3-
4-
<head>
5-
<meta charset="utf-8">
6-
<title>Page With Preloading Frame</title>
7-
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
8-
</head>
9-
10-
<body>
11-
<turbo-frame id="menu" src="/src/tests/fixtures/frames/preloading.html"></turbo-frame>
12-
</body>
13-
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Frame Preloading</title>
6+
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
7+
<script src="/src/tests/fixtures/test.js"></script>
8+
</head>
9+
<body>
10+
<turbo-frame id="menu" src="/src/tests/fixtures/frames/preloading.html"></turbo-frame>
11+
</body>
1412
</html>

src/tests/fixtures/frames/body_script.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Frame</title>
5+
<title>Frames: Body Script</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
7+
<script src="/src/tests/fixtures/test.js"></script>
78
</head>
89
<body>
910
<turbo-frame id="body-script" data-loaded-from="/src/tests/fixtures/frames/body_script.html">

src/tests/fixtures/frames/body_script_2.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Frame</title>
5+
<title>Frames: Body Script 2</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
7+
<script src="/src/tests/fixtures/test.js"></script>
78
</head>
89
<body>
910
<turbo-frame id="body-script" data-loaded-from="/src/tests/fixtures/frames/body_script_2.html">

src/tests/fixtures/frames/eval_false_script.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Frame</title>
5+
<title>Frames: Eval False Script</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
7+
<script src="/src/tests/fixtures/test.js"></script>
78
</head>
89
<body>
910
<turbo-frame id="eval-false-script" data-loaded-from="/src/tests/fixtures/frames/eval_false_script.html">

src/tests/fixtures/frames/form-redirect.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Frame</title>
5+
<title>Frames: Form Redirect</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
77
<script src="/src/tests/fixtures/test.js"></script>
88
</head>

src/tests/fixtures/frames/form-redirected.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Frame</title>
5+
<title>Frames: Form Redirected</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
77
<script src="/src/tests/fixtures/test.js"></script>
88
</head>

src/tests/fixtures/frames/form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Form</title>
5+
<title>Frames: Form</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
77
<script src="/src/tests/fixtures/test.js"></script>
88
</head>

src/tests/fixtures/frames/frame.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Frame</title>
5+
<title>Frames: Frame</title>
66
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
7+
<script src="/src/tests/fixtures/test.js"></script>
78
</head>
89
<body>
910
<h1>Frames: #frame</h1>
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
<turbo-frame id="eager-loaded-frame" >
2-
<h2>Eager-loaded frame: Loaded</h2>
3-
</turbo-frame>
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Frames: Frame for Eager</title>
6+
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
7+
<script src="/src/tests/fixtures/test.js"></script>
8+
</head>
9+
<body>
10+
<turbo-frame id="eager-loaded-frame" >
11+
<h2>Eager-loaded frame: Loaded</h2>
12+
</turbo-frame>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)