Performance: Stimulus and Turbo (Hotwire)
This affects page load speed by reducing full page reloads and improves interaction responsiveness by updating only parts of the page.
<button data-action="click->controller#update">Update</button> import { Controller } from '@hotwired/stimulus' export default class extends Controller { update() { fetch('/partial_update') .then(response => response.text()) .then(html => { this.element.outerHTML = html }) } }
document.querySelector('#button').addEventListener('click', () => { window.location.reload(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload on interaction | High (entire DOM replaced) | Multiple (full page) | High (full repaint) | [X] Bad |
| Turbo partial page update | Low (only changed fragments) | Single or few (targeted) | Low (partial repaint) | [OK] Good |
| Stimulus event-driven update | Low (targeted elements) | Single | Low | [OK] Good |
hello to a button element?data-controller attribute with the controller name.data-action defines event handlers, data-target defines targets inside controllers, data-connect is invalid.export default class extends Controller {
static targets = ["output"]
greet() {
this.outputTarget.textContent = "Hello, Stimulus!"
}
}and this HTML:<div data-controller="greeting"> <button data-action="click->greeting#greet">Say Hello</button> <span data-greeting-target="output"></span> </div>What happens when the button is clicked?
<turbo-frame id="messages"> <%= render @messages %> </turbo-frame>When a new message is created, the frame does not update automatically. What is the likely cause?
turbo-frame element with the same id as the frame on the page.turbo-frame wrapper, Turbo cannot replace the frame content automatically.Pass the test and this pattern moves unknown