Repository navigation
fix(vue): hydrate an element's @if without @else - #1399
Merged
Merged
Conversation
An element's `@if` without `@else` lowers to `cond ? <p /> : null`.
vue-jsx-vapor makes the `null` branch `createNodes(null)`, which
creates a comment instead of taking the `<!---->` the server rendered
for a false condition, so hydrating it threw `Cannot read properties of
null (reading 'insertBefore')`. An `@else if` chain without `@else`
failed the same way when nothing matched.
A `createIf` from `v-if` without `v-else` hydrates both that `<!---->`
and a rendered branch. So `render_element_ifs_as_directives` gives
such a choice, when each branch is one element or component, as
`v-if` / `v-else-if` elements. A new `controlFlow.ifStatement` hook
marks the shared lowering, which `@tsrx/core` now exports (an additive
export, as `switchStatementToJsxChild` is for `@switch`), so a ternary
written by hand is left alone.
Branches with setup statements and `<template>`/`<slot>`/`<component>`
branches keep the shared lowering, and so does an `@if` with `@else`,
whose `createIf` already hydrates. A choice followed by an element with
its own `v-else` or `v-else-if` also keeps it, past any other children
such as `{null}`, since that element would join the new chain.
The shared `>`-escaping tests for an `@if` body now match the text after
the tag, since Vue's branch carries a `v-if`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
Review of the previous commit found: - a branch with `v-slot` (or `v-slot:name`) that is a component's only child was dropped: with `v-if` beside it, vue-jsx-vapor reads it as the parent's slot declaration and compiles `createComponent(Wrap, null, null, true)`. `takes_directive` now rejects any `v-` attribute of the branch's own, plain or namespaced, which is what the PR described; - an `@else if` chain inside `VaporKeepAlive` lost the `@else if` branch's cached state after the chain matched nothing: vue-jsx-vapor gives a `v-else-if` branch an unkeyed `createIf` (129) where the nested ternary's is keyed (641). Every `@else if` chain now keeps the shared lowering, so a chain that matched nothing still fails to hydrate, as before; only a single `@if` without `@else` becomes a `v-if` element (`if_directive_branch`, `with_v_if`). Tests pin the KeepAlive state, the `v-slot`/`v-slot:name`/custom directive branches, and the sibling-expression case of tsrx-org#1402 that the `v-if` now keeps. The changeset lists the client changes (that tsrx-org#1402 case, the `<slot>` fallback now shown on the client, and vue-jsx-vapor's duplicated fallback after hydrating it) and the shapes that keep the shared lowering. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
jonkwheeler
marked this pull request as ready for review
November 19, 2026 13:27
Merged
jonkwheeler
added a commit
that referenced
this pull request
Nov 19, 2026
…#1408) * fix(vue): render an element's @if / @else as v-if and v-else elements 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 (#1402), and hydrating the server's markup there threw `No current hydration node was found`. #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 #1399. Two tracked files change on Vue (the Vue and Solid runtime tests), each only in an element's `@if` / `@else`. * fix(vue): keep a component's @if / @else child as one choice 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. * docs(vue): describe what the @if / @else rewrite changes and leaves The #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.
A server-rendered Vue page with an
@ifthat has no@elseinside an element can't be hydrated when the condition was false on the server. Hydration throws in the component's setup, and the page stays as the server rendered it. With this change the page hydrates, keeps the server's nodes, and then follows the condition.Related #1403 (the
@if-without-@elsepart; the in-element@switchremains)Related #1402 (partly: an
@ifwithout@elsebeside an expression child now keeps its branch's state)Behaviour
Rendered with vue-jsx-vapor's server build, then hydrated with
createVaporSSRAppin jsdom. The probe isagent-notes/wave15/H/hydrate/probe.mjs; it compiles through@tsrx/vueand vue-jsx-vapor 3.2.29 on Vue 3.6.0-rc.14. "main" is d216f1c. For each row, the value was 1 or 0 on the server, then changed to 2, 1, 0 and back after hydration:@ifwithout@else, false on the server<!---->Cannot read properties of null (reading 'insertBefore')<b>before</b><!----><b>after</b>@ifwithout@else, true on the server@if/@else@if/@else ifwithout@else, nothing matched<!--[--><!----><!--]-->@switch, a case rendered… 'nodeType'Cause
An element's
@ifwithout@elselowers tocond ? <p /> : null, and vue-jsx-vapor turns thenullbranch intocreateNodes(null). While hydrating, that branch creates a new comment instead of taking the<!---->the server rendered, and setup throwsCannot read properties of null (reading 'insertBefore'). AcreateIfmade fromv-ifwithoutv-elsehydrates both the<!---->and a rendered branch.agent-notes/wave15/H/hydrate/raw.mjschecks this by hand.What changes
When the branch of an element's child
@ifwithout@elseor@else if(an empty@else {}counts as none, since it lowers tonulltoo) is one element or component,render_element_ifs_as_directivesrenders it as that element withv-if:A new Vue
controlFlow.ifStatementhook marks the shared@iflowering, so a ternary written by hand is left alone. For that,@tsrx/corenow exportsif_statement_to_jsx_childasifStatementToJsxChild, the same way it exportsswitchStatementToJsxChildfor #1388's hook. This is an added export and changes no output. I compiled all 109 tracked.tsrxfiles for React, Preact, Solid and Hono with the core files at main and on this branch: the code, maps and CSS are identical (if/targets.mjs).Client behaviour that changes with it:
v-ifanchor comment, wherecreateNodes(null)left an empty comment. While the condition stays true the branch keeps its components' state, as before.@ifbeside an expression child renders its branch again, components and all, whenever what its condition reads changes #1402). Thev-ifis acreateIfof its own rather than part of the effect around the element's other expression children. Its branch now keeps its components' state when an expression child beside it changes. An@ifwith a non-empty@elsethere still renders again, as [Bug]: vue: an@ifbeside an expression child renders its branch again, components and all, whenever what its condition reads changes #1402 describes.@ifs and all of them are false, the component's<slot>fallback now shows on the client too. That matches the server's markup and Vue's semantics for a falsev-if. Hydrating that fallback and then toggling the@ifleaves the server's fallback text beside the new one (fallbackfallback), a gap in vue-jsx-vapor's<slot>output. Main logged a hydration mismatch there instead.Not changed
These keep the shared lowering. Those marked † still throw when they hydrate a false condition, as on main.
@else ifchain without@else†. vue-jsx-vapor gives av-else-ifbranch an unkeyedcreateIf(flags 129). The nested ternary'screateIfis keyed (641).VaporKeepAlivecaches the keyed one only, so av-else-ifchain lost a kept-alive tab's state after the chain had matched nothing (tab 2 → 0 → 2 resetb:1tob:0). Telling a KeepAlive parent apart reliably (aliases,KeepAlivevsVaporKeepAlive, a component holding the chain) isn't simple, so every@else ifchain keeps the nested ternary. A test pins the cached state.<p {...attrs}>, lowered to an IIFE for its ref);<template>branch, because vue-jsx-vapor 3.2.29 server-renders<template v-if>empty;<slot>or<component>branch.v-attribute of its own †, plain or namespaced:v-slot,v-slot:name,v-model:value, a customv-focus, and so on. Withv-ifbeside it, vue-jsx-vapor reads a component's only child withv-slotas the parent's slot declaration and drops the content.v-elseafter the choice †. If the choice's next element has its ownv-elseorv-else-if, the choice keeps the shared lowering. Text and expression children such as{null},{false}or{''}in between don't count. Otherwise that element would join the newv-ifinstead of the authored one before it.@ifwith a non-empty@else. ItscreateIfalready hydrated either branch.@switch. Hydrating it still throwsCannot read properties of null (reading 'nodeType')when a case rendered on the server, and that has a different cause. vue-jsx-vapor renders the switch's IIFE withsetNodesat a text node made bytxt(), which the server markup doesn't have, so hydration handssetNodesthe case's element as its anchor. Rendering the cases asv-ifelements needs the case picked once per update and per render scope. Acomputedhoisted to the surrounding block escapedv-forand loop scopes, went stale when a parent@ifre-created the element, and ran a self-writing discriminant twice on the server. Re-running theswitchfor each branch picked no branch for a getter discriminant. A sound fix needs vue-jsx-vapor'ssetNodesto hydrate, or a place for per-render-scope state. [Bug]: vue: a@switchinside an element fails to hydrate (Cannot read properties of null (reading 'nodeType')) #1403 tracks it.The keyed-flag loss and the duplicated fallback are written up as a draft upstream report for vue-jsx-vapor (not filed). On the server the output is unchanged.
The source map is unchanged in practice. The
<of a rewritten branch element maps to the token before it, as the<of every other element child already does in@tsrx/vue's output. Its tag name,v-iftest, attributes and children map to the source.Tests
packages/vite-plugin-vue/tests/vapor-for-ssr.test.js: a Vite server build of an element holding two@ifs without@else, one around a counter component. It renders both empty and rendered, keeping comments, so the exact markup is pinned.packages/vite-plugin-vue/tests/runtime.test.tsrx: hydrates that same markup withcreateVaporSSRAppthrough Vite and vue-jsx-vapor. It checks for no error, no hydration warning, and the same nodes (toBe), then toggles both@ifs. The counter's branch keeps its count while it stays shown.VaporKeepAlive@else ifchain keepingb:1across tab 2 → 1 → 2 → 0 → 2.@ifbeside an expression child renders its branch again, components and all, whenever what its condition reads changes #1402 case keepinga:1when the sibling expression changes.packages/tsrx-vue/tests/basic.test.js: covers thev-ifoutput, and that the shapes under "Not changed" keep the shared lowering. That includes an@else ifchain,v-slot,v-slot:name, a custom directive, and an authoredv-elseorv-else-ifpast{null},{false}and{''}.packages/tsrx/tests/shared/compile.js: the shared>-escaping tests for an@ifbody now match the text after the tag (>a > b</b>), since Vue's branch carries av-if.Mutation checks (
tsrx-vue,vite-plugin-vueandtsrx-vue-runtimeprojects):v-ifcompile test, the hydration test that starts empty, and the [Bug]: vue: an@ifbeside an expression child renders its branch again, components and all, whenever what its condition reads changes #1402 test.transform.jsat the first commit (chains andv-attributes converted), 3 fail: thev-ifcompile test, the shared-lowering compile test, and theVaporKeepAlivetest.v-elseguard off, or skipping only whitespace, the shared-lowering compile test fails.Full
pnpm test(after rebuilding@tsrx/eslint-parser): 233 files, 16487 passed, 103 skipped.pnpm typecheck,pnpm format:checkandpnpm changeset:checkpass.Codex (gpt-6.1-sol, xhigh):
First commit, round 1: found nothing new. An earlier version of this work also converted
@switch, and its reviews found two problems that apply here; both are fixed. An authoredv-elseafter the choice joined the new chain, and{null}/{false}/{''}slipped past the guard.Second commit: found no code defect. It found three changeset inaccuracies, all fixed:
@else {}counts as no@else;@ifs;v-elseguard checks the next element only.A last round on the corrected changeset found nothing new.
🤖 Generated with Claude Code
https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
Note
Medium Risk
Changes Vue compile output and SSR/client hydration for a common
@ifpattern; many guards limit scope, but hydration and slot/fallback behavior are still sensitive runtime paths.Overview
Fixes Vue SSR hydration when an element’s child is an
@ifwith no@else(or empty@else) and the condition was false on the server. Output no longer usescond ? <el /> : null(which vue-jsx-vapor lowers to acreateIfthat can’t adopt the server’s<!---->); for eligible single-element branches it now emits<el v-if={cond} />, which hydrates correctly.@tsrx/vueadds acontrolFlow.ifStatementhook that runs the shared lowering via newly exportedifStatementToJsxChildfrom@tsrx/core, tags those ternaries withvapor_ifmetadata, and runsrender_element_ifs_as_directivesafter compile to rewrite only safe cases (@else if, setup/IIFE branches,v-slot,<template>, adjacent authoredv-else/v-else-if, etc. still use the old lowering).Client-visible differences:
@ifbeside expression siblings can retain branch state when siblings re-render (#1402); all-false slot-only@ifchildren can show slot fallback on the client (with a known duplicate-fallback edge case).Tests cover compile output, SSR markup, and
createVaporSSRApphydration plus KeepAlive@else ifbehavior.Reviewed by Cursor Bugbot for commit 8ba894e. Bugbot is set up for automated code reviews on this repo. Configure here.