ReviewOS

stacks/stx

Reactive @if/@else with a nested @if in the else branch duplicates <main> on SPA navigation

#1784
Closed glennmichael123 opened this 22 days ago · 0 comments
22 days ago

Summary

On a signals page, a reactive @if(sig()) … @else … @endif chain whose @else branch contains a nested reactive @if produces malformed markup. A full page load hides it (the browser normalizes the stray tags), but the SPA router's innerHTML swap reproduces it verbatim, nesting a second <main> inside the swap container and aborting the View Transition (InvalidStateError: Transition was aborted).

Found while debugging why SPA navigation "couldn't be nailed" in a real app (a forgot-password page: @if(sent()) … @else〈form with @if(error())〉@endif).

Root cause — three stacked bugs

  1. convertSignalDirectivesToAttributes — overlapping replacements. After matching the outer @if block, the top-level @if( scan also matched the nested @if inside the else branch and pushed a second replacement whose range overlaps the outer one. Applying the two end-to-start (with stale indices) drops the inner branch content and leaves an unbalanced </template>.
  2. Template-strip drops <template @else>. The <template> preserve-lists (two in bun-plugin/serve.ts, one in includes.ts) listed @if but not @else/@else-if, so the else arm's opening tag was stripped while its </template> survived — unbalanced markup → duplicate <main> on the router's innerHTML swap.
  3. bindIfChain can't render <template> branches. bindIf/bindFor clone template .content; bindIfChain inserted the inert <template> element directly, so a multi-element @else/@else-if branch rendered blank once the markup was well-formed.

Fix

  • Recurse into branch bodies + skip the consumed block in the converter (no overlapping replacements).
  • Add @else/:else/x-else to the <template> preserve-lists (shared constant to prevent drift).
  • Teach bindIfChain to clone <template> branch content into a stable node set it toggles (mirroring bindIf/bindFor).

Includes unit + real-DOM regression tests.

Sign in to comment on this issue.