skip to content

During a Turbo 8 morphing refresh of a chores page, what does data-turbo-permanent protect, and what client-side state can a morph still reset?

level: juniorimportance: should knowfreq 28%

answer

  1. the DOM is made to match the HTML
  2. permanent elements skipped entirely
  3. give the element an id
  4. classes added in the browser are lost
  5. turbo:before-morph-attribute can veto

basics

~20 s

data-turbo-permanent tells Turbo to leave an element untouched, so morphing skips it entirely. Anything else is made to match the server's HTML, so classes or attributes changed in the browser, such as an open menu, can be reset.

solid answer

~40 s

A morph makes the live DOM match the newly rendered HTML. Elements that did not change keep their identity, listeners and focus, but any attribute or class the browser added after the page loaded, like an `open` state or a CSS class toggled by JavaScript, is compared against the server's HTML and reset. `data-turbo-permanent` opts an element out: Turbo's morph callbacks skip it and its children, and refuse to insert a duplicate of a permanent element that already exists by id. Give it an `id`, which ordinary body-swap renders also need to carry it over. Typical candidates are an "add chore" popover the user has open, a running timer or an audio player. For finer control, cancel `turbo:before-morph-attribute` for one attribute or `turbo:before-morph-element` for one element instead of freezing a whole region.

code

erb · 10 lines
erb
<%# app/views/chores/index.html.erb %>
<% turbo_refreshes_with method: :morph, scroll: :preserve %>

<div id="new_chore_popover" data-turbo-permanent>
  <%= render "chores/quick_form" %>
</div>

<ul>
  <%= render @chores %>
</ul>

go deeper

for a junior

Know that data-turbo-permanent with an id keeps an element exactly as it is while the rest of the page morphs.

for a middle

Explain that a morph makes the DOM match the server's HTML, so browser-only classes and attributes are reset unless protected.

for a senior

Decide between permanence, attribute or element vetoes and server-rendered state, and avoid freezing content that must stay fresh.

for a principal

Keep the server's HTML the single source of truth and treat browser-only state as a short, explicit list of exceptions.

## What a morph keeps and what it changes When a page refresh morphs, Turbo passes the current `<body>` and the new one to idiomorph, which edits the live DOM until it matches the new HTML. That has two sides: - **Kept:** elements whose markup is unchanged stay the same DOM nodes. Event listeners attached to them, focus, and the scroll position inside them survive, and Stimulus controllers on them stay connected. - **Changed:** anything in the live DOM that differs from the new HTML is brought into line. That includes changes the server never knew about: a class added by a click handler, an `open` attribute set on a `<details>` element, an `aria-expanded` toggled by a menu script. The server's HTML is the source of truth. If it renders a menu closed, a morph closes it, even if the user had just opened it. ## data-turbo-permanent Marking an element with `data-turbo-permanent` tells Turbo to leave it alone during a morph: 1. idiomorph asks Turbo before morphing each element; Turbo answers no for an element carrying `data-turbo-permanent`, so neither it nor its children change. 2. Turbo also declines to add a node from the new page when it is permanent and an element with the same `id` already exists, so the element is not duplicated. The same attribute already existed for ordinary renders: when Turbo swaps the body, it carries permanent elements across by matching `id`. An element needs an `id` for that to work, so the rule of thumb is simple: **always pair `data-turbo-permanent` with a unique `id`.** Candidates in a household app: - the "add chore" popover the user is typing into; - a running countdown timer for the current chore; - an audio element playing while the list refreshes; - a third-party widget that keeps internal state in the DOM. What permanence costs: the element no longer receives server updates during refreshes. A permanent element that shows data (a count, a name) will go stale. Keep permanence for elements whose state lives in the browser, not for content. ## Finer-grained control with events Turbo dispatches cancelable events during a morph: | Event | Target | Cancel to | |---|---|---| | `turbo:before-morph-element` | each element about to change | keep that element as it is | | `turbo:before-morph-attribute` | the element; `detail.attributeName` names the attribute | keep that one attribute | | `turbo:morph-element` | each element after it changed | (not cancelable; react to the change) | | `turbo:morph` | after the whole page morph | (not cancelable) | For example, keeping a dropdown open across refreshes without freezing its contents: ```javascript addEventListener("turbo:before-morph-attribute", (event) => { const { target, detail } = event if (target.matches("[data-dropdown]") && detail.attributeName === "aria-expanded") { event.preventDefault() } }) ``` This keeps the open state while the menu's items still update. ## Designing for morphs - **Render state on the server when you can.** If "show completed chores" is a toggle, put it in a URL param or a cookie so the server renders the right state; then a morph keeps it naturally. - **Use stable ids.** idiomorph matches elements by id and by the ids of their descendants; `dom_id(chore)` on each row keeps matches precise and avoids rebuilding nodes that would lose state. - **Keep browser-only state small and marked.** A few permanent elements or attribute vetoes are easier to reason about than a page full of exceptions. ## Debugging an unexpected reset 1. Log `turbo:before-morph-attribute` events with `event.target` and `event.detail.attributeName` while reproducing the reset. 2. Find the attribute that flips, such as `class`, `open` or `aria-expanded`, and the element it belongs to. 3. Decide whether the server should render that state (best), whether the attribute should be vetoed, or whether the whole element is browser-owned and belongs under `data-turbo-permanent`. Logging in a listener costs nothing in production if it is limited to development builds. ## Common mistakes - Adding `data-turbo-permanent` without an `id`, then wondering why the element is lost on ordinary navigations. - Freezing a content region to stop a flicker, then shipping stale numbers. - Expecting `data-turbo-temporary` to help: it removes elements before caching and has nothing to do with morphing.

  • Why should a data-turbo-permanent element always have an id?
    For ordinary body-swap renders Turbo carries permanent elements across by matching ids between the old and the new page, and during a morph it uses the id to avoid inserting a duplicate. Without an id the element is replaced like any other on normal navigations.
  • A user's half-typed note in the popover vanished after a refresh. What are your options?
    Mark the popover `data-turbo-permanent` with an id so the morph skips it, or cancel `turbo:before-morph-element` for that form. If the note must survive more than a refresh, save it as a draft the server renders, so the new HTML already contains it.

  • A morph only adds new nodes and never resets attributes changed in the browser.
  • data-turbo-permanent keeps the element updated with fresh server content.
  • data-turbo-temporary keeps an element unchanged during a morph.
  • data-turbo-permanent works on normal navigations even without an id.
  • turbo:morph-element can be cancelled to stop an element from changing.

Assumes Turbo 8 (8.0.23 source), where data-turbo-permanent elements are also skipped by morphing refreshes.