Repository navigation
fix(vue): render an element's @if / @else as v-if and v-else elements - #1408
Merged
jonkwheeler merged 3 commits intoNov 19, 2026
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An
@if/@elsebeside 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:Clicking the counter and then replacing
user.valuewith a copy reset it fromonline:1toonline: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@ifwithout@else.Related #1402 (the
@if/@elseform 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 ifchains)Behaviour
Client-only and SSR-plus-hydration runs in jsdom, through
@tsrx/vueand vue-jsx-vapor 3.2.29 on Vue 3.6.0-rc.14 (probeagent-notes/wave15/H/ifelse/probe.mjs). "main" is d9c2a14. The steps are: mount or hydrate, click the counter, replaceuser.valuewith a copy, toggle, toggle back.<div>holding@if/@elseof components, then{user.value.name}; clientc:0 → c:1 →c:0→ o:0 → c:0c:0 → c:1 →c:1→ o:0 → c:0{user.value.name}first; client… → c:1 →c:0… → c:1 →c:1No current hydration node was found; never updates, or duplicates nodes on toggle@if/@elsealone, of elements or componentsVaporKeepAlive,VaporTransition,VaporTeleport; as slot content; with refs, keys, SVGEvery row matches main except the first three, including the server markup. The only DOM difference beside an expression child is Vue's
v-ifanchor comment, which an@ifalone already had.Beside other elements, main already made the choice a
createIf. "Beside an expression" includes an@foror a@switchsibling, 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:
isSingleRoot = true). It took the parent component's fallthroughclass,data-*and listeners, and a parent'sonClickfired twice. Now it fires once.VaporTeleportbranch 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>withprefixnull, 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@elsedoes 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 acreateIf: 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. Itstxt()anchor is also missing from the server markup, which is why hydration throws. Alone, or beside elements, the ternary is acreateIf.v-if/v-elseelements are acreateIfin either position. Alone, vue-jsx-vapor compiles them to the samecreateIfas the ternary, with the same flags:v-if/v-elsecreateIf(…, 261)createIf(…, 261)357357VaporKeepAlive389389{x.value}createIf(setNodes)createIf(…, 261)That's
agent-notes/wave15/H/ifelse/flags.mjs. It's whyVaporKeepAlivecaching, transitions and hydration of a lone@if/@elseare unchanged. #1399's@else ifissue came from the nested chain's unkeyed flags, and a single@if/@elsehas no nested chain.What changes
render_element_ifs_as_directives(from #1399) also takes a host element's child@ifwith@elsewhen both branches can take a directive (takes_directive). It emits the then-branch withv-ifand the else-branch withv-else:Every changed output
I compiled all 109 tracked
.tsrxfiles for Vue with main'stransform.jsand with this branch. Two files change, and each change is only an element's@if/@elsebecomingv-if/v-elseelements (ifelse/vue-diff.txt):packages/vite-plugin-solid/tests/runtime.test.tsrx(compiled for Vue): three choices, the generated<Show>sibling,static-if-branchandstatic-true.packages/vite-plugin-vue/tests/runtime.test.tsrx:static-if-branch, plus two of this PR's new fixtures (theVaporKeepAliveone is a component's child and stays a ternary).The other targets are untouched, since only
@tsrx/vuechanges.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 ifchains, because vue-jsx-vapor'sv-else-ifcreateIfis unkeyed andVaporKeepAlivedoesn't cache it;<template>,<slot>or<component>;v-attribute of its own, such asv-slot;v-elseorv-else-if.A component's child
@if/@else, such as slot content orKeepAlive's child, also keeps the ternary. vue-jsx-vapor's server build passesv-if/v-elseelements to a component as two children, and<KeepAlive>then throwsexpects exactly one child component.(Codex round 1). Slot content already compiles to acreateIfpluscreateNodesand keeps its state on main. TheVaporKeepAliveruntime test and theKeepAliveserver-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-elseoutput beside an expression. The ternary is kept for a component's child, av-slotelse-branch, a setup-statement else-branch, and a choice before an authoredv-else.packages/vite-plugin-vue/tests/vapor-for-ssr.test.js: a Vite server build of an@if/@elsebeside{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:@ifbeside an expression child renders its branch again, components and all, whenever what its condition reads changes #1402 case keepsonline:1across a copy ofuser.value;VaporKeepAlivekeeps each branch's count across toggles;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-vueandtsrx-vue-runtimeprojects):transform.jsback at d9c2a14, 4 tests fail: the compile test, the [Bug]: vue: an@ifbeside an expression child renders its branch again, components and all, whenever what its condition reads changes #1402 test and both hydration tests.transform.jsat the first commit, which converts under any parent, 2 fail: the compile test and theKeepAliveserver render.Full
pnpm test(after rebuilding@tsrx/eslint-parser): 233 files, 16538 passed, 103 skipped.pnpm typecheck,pnpm format:checkandpnpm changeset:checkpass.Codex (gpt-6.1-sol, xhigh):
<KeepAlive>around an@if/@elsefailed to server-render.Round 1 also checked the type-only output and Volar mappings: they're identical for all 109 tracked
.tsrxfiles. 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/@elsewith single-element branches becomesv-if/v-elsedirectives (not only@ifalone). That keeps a propercreateIfwhen 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_directivesnow usesif_directive_branchesandwith_directivefor 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.