Skip to content

Engine: Keep <Fragment> fallbacks in the tree while other visitors run - #2654

Merged
marcoroth merged 1 commit into
mainfrom
fragment-fallback-scope
Sep 14, 2026
Merged

marcoroth merged 1 commit into
mainfrom
fragment-fallback-scope

Conversation

@marcoroth

@marcoroth marcoroth commented Sep 14, 2026 •

Copy link
Copy Markdown
Owner

A <Fragment>'s <Fallback> came out of a compile without the scope attribute a <style scoped> block gives every other element, so the markup a client stands in while content is stale rendered unstyled.

<%# herb:slots client %>
<style scoped>.g { color: red; }</style>

<Fragment on="album">
  <b>Loaded</b>
  <Fallback><i class="g">Loading</i></Fallback>
</Fragment>

The <i> the client swaps in came back as <i class="g">, while the CSS it needs compiles to .g[data-herb-scope-…].

The fallback is the only markup that leaves the tree this early. Fragment#transform returns just the conditional it synthesizes and hands the fallback body to the visitor to keep, so from that moment the body is reachable only through @fragment_fallbacks. It comes back at finish, as an entry in the statics the client rebuilds from. The engine runs every visitor's pass before it runs any visitor's finish, so a visitor that walks the tree after the slots visitor never sees the fallback, and one that writes into markup leaves it out. ScopedStyle::Visitor writes the scope attribute and SourceAttributionVisitor writes data-herb-source, and both missed it.

A conditional's arms do not have this problem, which is what made it look like scoped styles and parked markup work together. park_branch serializes an arm during finish, by which point every visitor has already written what it was going to write. Only the fallback is taken out mid-pass.

So the fallback now stands in the document for as long as the other visitors run. The slots visitor appends it once its own pass is done, and takes it back out at the top of finish, before anything reads the document to compile it. Nothing about the output changes except that the fallback carries what the other visitors wrote. It still never renders inline, and it still reaches the client through the same statics entry, which holds the same nodes.

The test that covers it compiles the template above and snapshots it, so the parked arm has to show the attribute:

<!--herb-branch:0:1--><i class="g" data-herb-scope-2940ba8a>Loading</i>

It sits next to the test that already pins the same thing for a conditional's arms, and it fails without this change.

@github-actions github-actions Bot added ruby Ruby source for the gem and its libraries rbs RBS type signatures in sig/ engine Herb engine and Rails template compilation rubygem The herb RubyGem and its packaging labels Sep 14, 2026
@marcoroth
marcoroth enabled auto-merge (squash) September 14, 2026 19:16
@marcoroth
marcoroth merged commit a3edae4 into main Sep 14, 2026
34 checks passed
@marcoroth
marcoroth deleted the fragment-fallback-scope branch September 14, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine Herb engine and Rails template compilation rbs RBS type signatures in sig/ ruby Ruby source for the gem and its libraries rubygem The herb RubyGem and its packaging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant