Skip to content

fix(vue): render an element's @if / @else as v-if and v-else elements - #1408

Merged
jonkwheeler merged 3 commits into
tsrx-org:mainfrom
jonkwheeler:w15/H-vue-if-else-directive
Nov 19, 2026
Merged

jonkwheeler merged 3 commits into
tsrx-org:mainfrom
jonkwheeler:w15/H-vue-if-else-directive

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

An @if / @else beside another expression child in an element renders its branch again on Vue whenever what its condition reads changes, even when the condition's value doesn't. Its components lose their state:

export function Header() @{
	<div>
		@if (user.value.online) {
			<Counter label="online" />
		} @else {
			<i>{'away'}</i>
		}
		{user.value.name}
	</div>
}

Clicking the counter and then replacing user.value with a copy reset it from online:1 to online:0. Hydrating server markup in that position also threw. With this change the branch keeps its state, and the markup hydrates. #1399 already did this for an @if without @else.

Related #1402 (the @if / @else form whose branches are each one element. Branches that can't take a directive still render again beside an expression: setup statements, spreads, dynamic tags, v- attributes, and @else if chains)

Behaviour

Client-only and SSR-plus-hydration runs in jsdom, through @tsrx/vue and vue-jsx-vapor 3.2.29 on Vue 3.6.0-rc.14 (probe agent-notes/wave15/H/ifelse/probe.mjs). "main" is d9c2a14. The steps are: mount or hydrate, click the counter, replace user.value with a copy, toggle, toggle back.

<div> holding main this PR
@if / @else of components, then {user.value.name}; client c:0 → c:1 → c:0 → o:0 → c:0 c:0 → c:1 → c:1 → o:0 → c:0
the same, {user.value.name} first; client … → c:1 → c:0 … → c:1 → c:1
either of those, hydrating the server's markup throws No current hydration node was found; never updates, or duplicates nodes on toggle hydrates (same node), then as on the client
@if / @else alone, of elements or components hydrates and follows same; identical client and server runs
under VaporKeepAlive, VaporTransition, VaporTeleport; as slot content; with refs, keys, SVG as for alone same; identical runs (a component parent keeps the ternary, see "Not changed")

Every row matches main except the first three, including the server markup. The only DOM difference beside an expression child is Vue's v-if anchor comment, which an @if alone already had.

Beside other elements, main already made the choice a createIf. "Beside an expression" includes an @for or a @switch sibling, which are expressions too, so those layouts are fixed as well.

The probe's beside-an-expression runs also show two smaller fixes in that position:

  • Fallthrough. On main the component branch was created as the parent's single root (isSingleRoot = true). It took the parent component's fallthrough class, data-* and listeners, and a parent's onClick fired twice. Now it fires once.
  • Teleport. A VaporTeleport branch no longer leaves a <!--teleport start--> comment behind on each toggle.

These two come from the #1408 review's probes (agent-notes/wave15/review-1408/probes/).

One partial-hydration case remains. If an expression just before the choice rendered nothing on the server (<div>{prefix.value}@if (on.value) {…} @else {…}</div> with prefix null, markup <!----><b>yes</b>), hydration logs a mismatch and shows <b>yes</b><!--if--><b>yes</b>. Main throws there, and #1399's form without @else does the same as this. It's in the vue-jsx-vapor upstream draft (agent-notes/wave15/H/hydrate/if/upstream-vue-jsx-vapor.md, item 3).

Cause

vue-jsx-vapor merges adjacent expression children into one setNodes(anchor, …) call. When the @if / @else's ternary is one of them, it doesn't become a createIf: it is evaluated as a plain expression in that call's effect. The effect reads the condition's dependencies, so a change to any of them renders the branch again. Its txt() anchor is also missing from the server markup, which is why hydration throws. Alone, or beside elements, the ternary is a createIf.

v-if / v-else elements are a createIf in either position. Alone, vue-jsx-vapor compiles them to the same createIf as the ternary, with the same flags:

ternary v-if / v-else
components, alone createIf(…, 261) createIf(…, 261)
host elements, alone 357 357
under VaporKeepAlive 389 389
beside {x.value} no createIf (setNodes) createIf(…, 261)

That's agent-notes/wave15/H/ifelse/flags.mjs. It's why VaporKeepAlive caching, transitions and hydration of a lone @if / @else are unchanged. #1399's @else if issue came from the nested chain's unkeyed flags, and a single @if / @else has no nested chain.

What changes

render_element_ifs_as_directives (from #1399) also takes a host element's child @if with @else when both branches can take a directive (takes_directive). It emits the then-branch with v-if and the else-branch with v-else:

// main
<div>{user.value.online ? <Counter label="online" /> : <i>{'away'}</i>}{user.value.name}</div>

// this PR
<div><Counter v-if={user.value.online} label="online" /><i v-else>{'away'}</i>{user.value.name}</div>

Every changed output

I compiled all 109 tracked .tsrx files for Vue with main's transform.js and with this branch. Two files change, and each change is only an element's @if / @else becoming v-if / v-else elements (ifelse/vue-diff.txt):

  • packages/vite-plugin-solid/tests/runtime.test.tsrx (compiled for Vue): three choices, the generated <Show> sibling, static-if-branch and static-true.
  • packages/vite-plugin-vue/tests/runtime.test.tsrx: static-if-branch, plus two of this PR's new fixtures (the VaporKeepAlive one is a component's child and stays a ternary).

The other targets are untouched, since only @tsrx/vue changes.

Not changed

These keep the shared lowering, as in #1399. Beside an expression child they still render their branch again, which is why this PR only relates to #1402:

  • @else if chains, because vue-jsx-vapor's v-else-if createIf is unkeyed and VaporKeepAlive doesn't cache it;
  • a branch with setup statements, a spread host element, a dynamic tag, or a <template>, <slot> or <component>;
  • an element with any v- attribute of its own, such as v-slot;
  • a choice whose next element has its own v-else or v-else-if.

A component's child @if / @else, such as slot content or KeepAlive's child, also keeps the ternary. vue-jsx-vapor's server build passes v-if / v-else elements to a component as two children, and <KeepAlive> then throws expects exactly one child component. (Codex round 1). Slot content already compiles to a createIf plus createNodes and keeps its state on main. The VaporKeepAlive runtime test and the KeepAlive server-render test pin that this case keeps the ternary; both pass on main too.

Tests

  • packages/tsrx-vue/tests/basic.test.js: v-if / v-else output beside an expression. The ternary is kept for a component's child, a v-slot else-branch, a setup-statement else-branch, and a choice before an authored v-else.
  • packages/vite-plugin-vue/tests/vapor-for-ssr.test.js: a Vite server build of an @if / @else beside {name}, in both states, with comments kept. Another server-renders <KeepAlive>@if … @else …</KeepAlive> in both states.
  • packages/vite-plugin-vue/tests/runtime.test.tsrx:
  • packages/tsrx/tests/shared/compile.js: the element-valued attribute test matches >1</div>, since Vue's branches carry directives now.

Mutation checks (tsrx-vue, vite-plugin-vue and tsrx-vue-runtime projects):

Full pnpm test (after rebuilding @tsrx/eslint-parser): 233 files, 16538 passed, 103 skipped. pnpm typecheck, pnpm format:check and pnpm changeset:check pass.

Codex (gpt-6.1-sol, xhigh):

  • Round 1 found one regression, fixed in the second commit: <KeepAlive> around an @if / @else failed to server-render.
  • Round 2 found nothing new. It re-ran 52 runtime and hydration fixtures and 74 server renders through the real Vite plugin against d9c2a14. Those covered KeepAlive, Transition, Teleport, slots, other component parents and host parents.

Round 1 also checked the type-only output and Volar mappings: they're identical for all 109 tracked .tsrx files. A rewritten branch's < maps the way every element child's < already does.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


Note

Medium Risk
Changes Vue JSX lowering and runtime/hydration behavior for a common template pattern; mitigated by narrow eligibility rules and broad test coverage, with one known partial-hydration edge case unchanged from before.

Overview
Extends the Vue compile pass so a host element's @if / @else with single-element branches becomes v-if / v-else directives (not only @if alone). That keeps a proper createIf when the choice sits next to another expression child, fixing #1402 state loss, SSR hydration failures, erroneous fallthrough on component branches, and leftover teleport comments.

render_element_ifs_as_directives now uses if_directive_branches and with_directive for both branches; KeepAlive / component children still get the ternary so SSR does not pass two children. Chains, setup statements, v- attrs, and similar cases stay on shared lowering. Compile, runtime, hydration, and SSR tests cover the new output.

Reviewed by Cursor Bugbot for commit b3a930f. Bugbot is set up for automated code reviews on this repo. Configure here.

jonkwheeler and others added 3 commits November 19, 2026 08:38
Beside another expression child, vue-jsx-vapor evaluates an `@if` /
`@else` ternary inside that child's `setNodes` effect instead of making
it a `createIf`. The branch then rendered again, components and all,
whenever what the condition read changed (tsrx-org#1402), and hydrating the
server's markup there threw `No current hydration node was found`.

tsrx-org#1399 renders an `@if` without `@else` as a `v-if` element. This extends
`render_element_ifs_as_directives` to an `@if` with `@else` whose two
branches are each one element that can take a directive: the choice
becomes `v-if` / `v-else` elements (`if_directive_branches`,
`with_directive`). Alone, vue-jsx-vapor compiles both forms to the same
`createIf` with the same flags (261, 357 for host elements, 389 under
`VaporKeepAlive`); beside an expression it stays a `createIf`.

`@else if` chains, branches that can't take a directive and a choice
before an authored `v-else` keep the shared lowering, as in tsrx-org#1399. Two
tracked files change on Vue (the Vue and Solid runtime tests), each only
in an element's `@if` / `@else`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
Codex found that `<KeepAlive>@if (on) { <A /> } @else { <B /> }</KeepAlive>`
threw `<KeepAlive> expects exactly one child component.` in a server
render: vue-jsx-vapor's server build passes `v-if` / `v-else` elements
to a component as two children. `if_directive_branches` now takes an
`@else` branch only when the parent is a host element; a component's
child keeps the ternary. A Vite server render of that KeepAlive in both
states pins it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
The tsrx-org#1408 review found no code regression, only wording to fix:

- the changeset now lists the two other fixes beside an expression (a
  component branch no longer takes the parent's fallthrough attributes
  and listeners twice, and a VaporTeleport branch no longer leaves
  teleport comments behind), counts @for and @switch siblings as
  expressions, and notes the partial hydration that remains after an
  expression that rendered nothing;
- it says which branches still render again beside an expression, and
  no longer claims a component's child does: slot content already kept
  its state;
- the VaporKeepAlive runtime test and the KeepAlive server-render test
  say they pin that a component's child keeps the ternary, as on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler marked this pull request as ready for review November 19, 2026 15:23
@jonkwheeler
jonkwheeler merged commit 68592f4 into tsrx-org:main Nov 19, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant