Problem
The reactive conditional directives (:if, :else, :else-if, x-if, x-else, x-else-if, :show, x-show) currently emit ALL branches to the browser. The SSR processor doesn't analyze whether the branch condition is resolvable server-side; it bakes the directive attribute into the HTML and lets the client-side bindIf / bindIfChain runtime decide.
This is the single architectural decision behind the largest class of bugs and DX gaps in stx:
- The FOUC class (#1736) — every conditional branch flashes visible until the runtime catches up
- The scope-walk class (#1737) —
disposeSubtreeScopesrunning during transientbindIfdetach deletes nested-scope entries - The over-subscription class (#1738) — chain effects re-run on every signal in scope because the picker has to be defensive about which branches get evaluated
- The "ship more HTML than needed" class — every reader of a published review downloads the markup for the "loading" and "not found" branches too
- The "hydration mismatch" risk class — every divergence between server-rendered HTML and client-runtime evaluation has to be reconciled at runtime
The vast majority of :if conditions in real apps are server-resolvable. In bench-review (audited just now): 67 of 89 (~75%) :if / :else-if expressions reference only data already in scope at SSR time — judge.status, currentUser.is_admin, review.anonymized, c.author?.id, etc. The remaining 25% are genuinely client-reactive (waiting on an async fetch, derived from a client-only signal).
The framework already has the engine for this — @if(cond) ... @else ... @endif is the Blade-style server-side conditional that eliminates the false branch at render time. v-if / v-else-if / v-else compile down to @if at build time (process.ts ~line 2586's kK function). What's missing is the same treatment for the reactive family when the expression is server-resolvable.
Proposed approach
Two-pass SSR analysis on each :if / :else / :else-if / :show:
Pass 1 — Resolvability check. Parse the expression. Walk the AST. For each identifier:
- If it's in the server-resolvable scope (variables defined in
<script server>,@includeparameters, parent loop variables, request context), mark resolvable. - If it's in the client-only scope (state from
<script client>, store signals, derived values from client signals), mark NOT resolvable. - If mixed, mark NOT resolvable.
Pass 2 — Branch elimination. For chains where every branch's expression is server-resolvable:
- Evaluate each branch's condition at SSR time using the server-scope values.
- Emit ONLY the chosen branch's HTML.
- Strip the
:if/:else/:else-ifattributes from the emitted element.
For chains where any branch references client-only state:
- Behave exactly as today (emit all branches with directive attributes, let the runtime pick).
Concrete repro reference
Bench-review's article body has this pattern repeated dozens of times:
<article :if="reviewsStore.current"> <!-- client-reactive — keep as runtime :if -->
<span :if="review.author && review.author.id"> <!-- server-resolvable when @include'd with review — could SSR-eliminate -->
<StxLink :to="`/user/${review.author.id}`">{{ review.author.name }}</StxLink>
</span>
<span :else>Anonymous reviewer</span>
</article>The outer :if="reviewsStore.current" legitimately needs runtime evaluation (waiting on fetch). The inner :if="review.author && review.author.id" could be evaluated server-side because review is in @include scope at SSR. Today both go through the runtime.
Prior art
- Vue 3 SSR —
compileSSRevaluatesv-ifwith the bound context at server-render time and emits only the active branch - Next.js (App Router) — every
ifin a server component runs server-side, only result reaches the client - Astro — SSR-only by default; client interactivity is opt-in via
client:*directives. False branches never enter the HTML. - SvelteKit — compiler analyzes which branches reference reactive vs static state
- Nuxt 3 — Vue-equivalent treatment
- Phoenix LiveView — server-side diffing; only changed branches stream
Impact
| Surface | Today | After fix |
|---|---|---|
| FOUC for server-known conditions | Flashes both branches until runtime detaches | Only active branch in HTML; no flash possible |
| HTML bytes for nested conditionals | All branches ship | Only chosen branch ships |
| Runtime work (effect runs, processElement walks) | Every conditional gets bound | Only client-reactive conditionals get bound |
| Hydration mismatch risk | Whenever runtime evaluates a different answer than SSR expected | Eliminated for server-resolved conditions |
| Cognitive load on adopters | Must understand SSR vs reactive distinction | Framework picks the right strategy per expression |
Compatibility
- Today's reactive runtime stays exactly as-is — the new pass only ELIMINATES branches that don't need runtime treatment
- Adopters don't change anything; the SSR processor opts in transparently
@if/v-ifare unaffected (they already SSR-eliminate):ifon elements withdata-stx-scope(component boundaries) gets treated as runtime-resolvable by default to avoid scope-init surprises
Phasing
- Phase 1: AST walker + resolvability marker (no behavior change yet, just instrument and log "would have eliminated N of M conditionals on this page")
- Phase 2: Eliminate single-branch
:if(no:else) - Phase 3: Eliminate full chains (
:if/:else-if/:else) - Phase 4: Eliminate
:show(set inlinedisplay:noneinstead of emitting both states)
Happy to PR Phase 1 as a starting point — pure instrumentation, no behavior change, gives a real adoption number to debate Phase 2+ from.