-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces the concept of page refresh. A page refresh happens when Turbo renders the current page again. We will offer two new options to control behavior when a page refresh happens: - The method used to update the page: with a new option to use morphing (Turbo currently replaces the body). - The scroll strategy: with a new option to keep it (Turbo currently resets scroll to the top-left). The combination of morphing and scroll-keeping results in smoother updates that keep the screen state. For example, this will keep both horizontal and vertical scroll, the focus, the text selection, CSS transition states, etc. We will also introduce a new turbo stream action that, when broadcasted, will request a page refresh. This will offer a simplified alternative to fine-grained broadcasted turbo-stream actions. Co-Authored-By: Jorge Manrubia <[email protected]>
- Loading branch information
1 parent
0ec99ae
commit f5edb5e
Showing
31 changed files
with
619 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export class LimitedSet extends Set { | ||
constructor(maxSize) { | ||
super() | ||
this.maxSize = maxSize | ||
} | ||
|
||
add(value) { | ||
if (this.size >= this.maxSize) { | ||
const iterator = this.values() | ||
const oldestValue = iterator.next().value | ||
this.delete(oldestValue) | ||
} | ||
super.add(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import Idiomorph from "idiomorph" | ||
import { nextAnimationFrame } from "../../util" | ||
import { Renderer } from "../renderer" | ||
|
||
export class MorphRenderer extends Renderer { | ||
async render() { | ||
if (this.willRender) await this.#morphBody() | ||
} | ||
|
||
get renderMethod() { | ||
return "morph" | ||
} | ||
|
||
// Private | ||
|
||
async #morphBody() { | ||
this.#morphElements(this.currentElement, this.newElement) | ||
this.#reloadRemoteFrames() | ||
|
||
this.#dispatchEvent("turbo:morph", { currentElement: this.currentElement, newElement: this.newElement }) | ||
} | ||
|
||
#morphElements(currentElement, newElement, morphStyle = "outerHTML") { | ||
Idiomorph.morph(currentElement, newElement, { | ||
morphStyle: morphStyle, | ||
callbacks: { | ||
beforeNodeMorphed: this.#shouldMorphElement, | ||
beforeNodeRemoved: this.#shouldRemoveElement, | ||
afterNodeMorphed: this.#reloadStimulusControllers | ||
} | ||
}) | ||
} | ||
|
||
#reloadRemoteFrames() { | ||
this.#remoteFrames().forEach((frame) => { | ||
if (this.#isFrameReloadedWithMorph(frame)) { | ||
this.#renderFrameWithMorph(frame) | ||
} | ||
frame.reload() | ||
}) | ||
} | ||
|
||
#renderFrameWithMorph(frame) { | ||
frame.addEventListener("turbo:before-frame-render", (event) => { | ||
event.detail.render = this.#morphFrameUpdate | ||
}, { once: true }) | ||
} | ||
|
||
#morphFrameUpdate = (currentElement, newElement) => { | ||
this.#dispatchEvent("turbo:before-frame-morph", { currentElement, newElement }, currentElement) | ||
this.#morphElements(currentElement, newElement, "innerHTML") | ||
} | ||
|
||
#shouldRemoveElement = (node) => { | ||
return this.#shouldMorphElement(node) | ||
} | ||
|
||
#shouldMorphElement = (node) => { | ||
if (node instanceof HTMLElement) { | ||
return !node.hasAttribute("data-turbo-permanent") | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
#reloadStimulusControllers = async (node) => { | ||
if (node instanceof HTMLElement && node.hasAttribute("data-controller")) { | ||
const originalAttribute = node.getAttribute("data-controller") | ||
node.removeAttribute("data-controller") | ||
await nextAnimationFrame() | ||
node.setAttribute("data-controller", originalAttribute) | ||
} | ||
} | ||
|
||
#isFrameReloadedWithMorph(element) { | ||
return element.getAttribute("src") && element.getAttribute("refresh") === "morph" | ||
} | ||
|
||
#remoteFrames() { | ||
return document.querySelectorAll("turbo-frame[src]") | ||
} | ||
|
||
#dispatchEvent(name, detail, target = document.documentElement) { | ||
const event = new CustomEvent(name, { bubbles: true, cancelable: true, detail }) | ||
target.dispatchEvent(event) | ||
return event | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { uuid } from "../util" | ||
|
||
const originalFetch = window.fetch | ||
|
||
window.fetch = async function(url, options = {}) { | ||
const modifiedHeaders = new Headers(options.headers || {}) | ||
const requestUID = uuid() | ||
window.Turbo.recentRequests.add(requestUID) | ||
modifiedHeaders.append("X-Turbo-Request-Id", requestUID) | ||
|
||
return originalFetch(url, { | ||
...options, | ||
headers: modifiedHeaders | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<turbo-frame id="refresh-morph"> | ||
<h2>Loaded morphed frame</h2> | ||
</turbo-frame> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<turbo-frame id="refresh-reload"> | ||
<h2>Loaded reloadable frame</h2> | ||
</turbo-frame> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html id="html" data-skip-event-details="turbo:submit-start turbo:submit-end turbo:fetch-request-error"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="turbo-refresh-method" content="morph"> | ||
<meta name="turbo-refresh-scroll" content="preserve"> | ||
|
||
<title>Turbo</title> | ||
<link rel="icon" href="data:;base64,iVBORw0KGgo="> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
<script src="/src/tests/fixtures/test.js"></script> | ||
|
||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
|
||
/* Ensure the page is large enough to scroll */ | ||
width: 150vw; | ||
height: 150vh; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Page to be refreshed</h1> | ||
|
||
<turbo-frame id="refresh-morph" src="/src/tests/fixtures/frame_refresh_morph.html" refresh="morph"> | ||
<h2>Frame to be morphed</h2> | ||
</turbo-frame> | ||
|
||
<turbo-frame id="refresh-reload" src="/src/tests/fixtures/frame_refresh_reload.html" refresh="reload"> | ||
<h2>Frame to be reloaded</h2> | ||
</turbo-frame> | ||
|
||
<div id="preserve-me" data-turbo-permanent> | ||
Preserve me! | ||
</div> | ||
|
||
<div id="stimulus-controller" data-controller="test"> | ||
<h3>Element with Stimulus controller</h3> | ||
</div> | ||
|
||
<p><a id="link" href="/src/tests/fixtures/one.html">Link to another page</a></p> | ||
|
||
<form id="form" action="/__turbo/refresh" method="post" class="redirect"> | ||
<input type="text" name="text" value=""> | ||
<input type="hidden" name="path" value="/src/tests/fixtures/page_refresh.html"> | ||
<input type="hidden" name="sleep" value="50"> | ||
<input id="form-submit" type="submit" value="form[method=post]"> | ||
</form> | ||
</body> | ||
</html> |
Oops, something went wrong.