Preserve omitted head metadata during promoted frame visits

Merged
EM
18d ago
+438-39

Edit:

Fixes #1237.

A promoted frame visit keeps the surrounding document, but its response may contain only a fragment. Applying the normal page head replacement can remove the current title, CSRF token, Turbo settings, and document language. Simply appending supplied metadata also leaves stale values first in the head.

Preserve omitted head elements and lang/dir during visits that do not render the document body, while applying values explicitly supplied by the response. Replace supplied metadata groups, title, base, canonical and manifest links, and matching icon variants. Preserve response order, repeated metadata, and existing nodes when unchanged. Normal Drive visits retain their existing head replacement behavior.

Allow these visits to omit tracked assets already loaded by the page. An explicitly supplied new or changed tracked asset triggers a native reload before activation. Skip the synthetic redirect follow-up when that reload has already been requested.

Regression coverage checks omitted and complete heads, replacement metadata and a real POST using the new CSRF token, metadata order and node identity, document attributes, compatible tracked subsets, and reloading before executing changed tracked code.

The existing failed-form reload test now waits for each document load before accessing the next document.

Summary

  • Fix promoted frame visits pruning the document head and removing lang/dir when a response omits them
  • Fix tracked-element mismatches marking the page as force-reloaded and corrupting Back/Forward history
  • Merge head elements supplied by partial responses while preserving omitted metadata and replacing <title>
  • Preserve normal Drive behavior and support promoted frame responses containing complete documents
  • Preserve page language, text direction, and title information that supports accessibility during promoted frame navigation

Fixes #1237

Problem

A frame navigation with data-turbo-action="advance" or replace first renders the frame, then starts a synthetic page Visit with willRender: false. The response passed to PageRenderer describes the frame response, not the page that remains on screen.

The current control flow has two failure modes:

Current pagePageRenderer.shouldRenderResult
No tracked elementtrue because the empty signatures matchThe head merge removes provisional elements absent from the frame response, and #setLanguage() removes lang/dir
Has a tracked elementfalse because the frame response omits itPageView sets forceReloaded = true and skips the valid-history path

The first branch can remove the title, CSRF token, viewport and charset metadata, icons, manifest, module preload hints, and Turbo's own meta settings. The lang loss persists across Back restoration because it lives on <html>, outside the restored body snapshot.

Adding data-turbo-track="reload" therefore exchanges head loss for an inconsistent history entry. The history failure becomes visible after leaving through a Turbo Drive link and navigating Back twice.

Accessibility impact

This is not only a metadata or history issue. Promoted frame navigation leaves the surrounding page visible, but can remove information users rely on to understand and navigate it:

  • Page language: removing <html lang> can prevent screen readers from selecting the appropriate pronunciation rules for content without its own language declaration. This loss can persist after navigating Back.
  • Text direction: removing an explicit dir can change the presentation of right-to-left or mixed-direction content when the inherited or default direction differs.
  • Page identification: removing <title> can leave browser tabs and assistive technology without the descriptive page title, making pages harder to identify and distinguish.
  • Navigation predictability: inconsistent Back/Forward history can make it harder to return to previously viewed content and maintain orientation.

Preserving omitted metadata keeps a partial frame response from silently removing accessibility information belonging to the surrounding document.

Fix

For a Visit that will not render the document body, PageRenderer now treats the response head as additive rather than authoritative:

  • A tracked-element mismatch does not make the synthetic Visit unrenderable
  • Omitted provisional head elements are preserved when the Visit will not render the document, except that a supplied <title> replaces the current title
  • Omitted lang and dir values are removed only when the Visit will render the document
  • Supplied head elements and document attributes are still applied

For normal Drive visits, willRender remains true, so head replacement, attribute removal, and tracked-element reload behavior are unchanged.

Promoted frame responses containing complete documents remain supported: supplied head elements are merged, a supplied title replaces the current title, and supplied lang/dir values are applied. Omitted elements and attributes are preserved because the Visit does not replace the document body.

Relation to turbo-rails#534

hotwired/turbo-rails#534 proposes rendering full documents for requests with a Turbo-Frame header because a minimal layout can differ sharply from the requesting document's head.

Full-document frame responses remain supported by this PR. The additive behavior makes them optional for correctness: an empty or partial frame head updates only what it provides instead of silently deleting unrelated page metadata.

This also avoids requiring server integrations to decide which application-specific head elements a minimal frame layout must repeat. The partial turbo-rails frame layout, Laravel Blade fragments, and the minimal Symfony UX Turbo frame layout can all preserve the page head without giving up partial responses.

Test plan

Regression coverage includes:

  • Preserving title, metadata, icons, and lang when a promoted frame response has an empty head
  • Adding partial head content and replacing the title without removing omitted elements
  • Applying a complete response head and its supplied language
  • Restoring URL and frame contents through Drive navigation, Back/Back, and Forward/Forward
  • Replacing history entries correctly with data-turbo-action="replace"

Local validation completed before the latest merge from main:

  • Frame navigation and rendering tests passed in Chrome and Firefox
  • Chromium unit tests and lint passed
  • Manual Laravel checks covered tracked and untracked heads, including a Drive exit followed by Back/Back

CI passed after merging the latest main, including build, lint, unit tests, and browser tests in Chrome and Firefox.