Skip to content

fix(vue): hydrate an element's @if without @else - #1399

Merged
jonkwheeler merged 2 commits into
tsrx-org:mainfrom
jonkwheeler:w15/H-vue-if-hydrate
Nov 19, 2026
Merged

jonkwheeler merged 2 commits into
tsrx-org:mainfrom
jonkwheeler:w15/H-vue-if-hydrate

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

A server-rendered Vue page with an @if that has no @else inside 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.

export function Status(props: { online: boolean }) @{
	<div>
		<span>
			@if (props.online) {
				<b>{'online'}</b>
			}
		</span>
	</div>
}

Related #1403 (the @if-without-@else part; the in-element @switch remains)
Related #1402 (partly: an @if without @else beside an expression child now keeps its branch's state)

Behaviour

Rendered with vue-jsx-vapor's server build, then hydrated with createVaporSSRApp in jsdom. The probe is agent-notes/wave15/H/hydrate/probe.mjs; it compiles through @tsrx/vue and 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:

Element's child Server markup main this PR
@if without @else, false on the server <!----> throws Cannot read properties of null (reading 'insertBefore') hydrates; shows the branch when true and removes it when false
the same, beside other elements <b>before</b><!----><b>after</b> throws, same error hydrates, follows each change
@if without @else, true on the server the branch hydrates (same node), follows same
@if / @else either branch hydrates (same node), follows same, with unchanged output
@if / @else if without @else, nothing matched <!--[--><!----><!--]--> throws, same error same as main (see "Not changed")
@switch, a case rendered the case throws … 'nodeType' same as main (see "Not changed")

Cause

An element's @if without @else lowers to cond ? <p /> : null, and vue-jsx-vapor turns the null branch into createNodes(null). While hydrating, that branch creates a new comment instead of taking the <!----> the server rendered, and setup throws Cannot read properties of null (reading 'insertBefore'). A createIf made from v-if without v-else hydrates both the <!----> and a rendered branch. agent-notes/wave15/H/hydrate/raw.mjs checks this by hand.

What changes

When the branch of an element's child @if without @else or @else if (an empty @else {} counts as none, since it lowers to null too) is one element or component, render_element_ifs_as_directives renders it as that element with v-if:

// main
<span>{props.online ? <b>{'online'}</b> : null}</span>

// this PR
<span><b v-if={props.online}>{'online'}</b></span>

A new Vue controlFlow.ifStatement hook marks the shared @if lowering, so a ternary written by hand is left alone. For that, @tsrx/core now exports if_statement_to_jsx_child as ifStatementToJsxChild, the same way it exports switchStatementToJsxChild for #1388's hook. This is an added export and changes no output. I compiled all 109 tracked .tsrx files 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:

Not changed

These keep the shared lowering. Those marked † still throw when they hydrate a false condition, as on main.

  • An @else if chain without @else †. vue-jsx-vapor gives a v-else-if branch an unkeyed createIf (flags 129). The nested ternary's createIf is keyed (641). VaporKeepAlive caches the keyed one only, so a v-else-if chain lost a kept-alive tab's state after the chain had matched nothing (tab 2 → 0 → 2 reset b:1 to b:0). Telling a KeepAlive parent apart reliably (aliases, KeepAlive vs VaporKeepAlive, a component holding the chain) isn't simple, so every @else if chain keeps the nested ternary. A test pins the cached state.
  • Branches that aren't one plain element †:
    • a branch with setup statements (an IIFE);
    • a host element with a spread (<p {...attrs}>, lowered to an IIFE for its ref);
    • a dynamic tag;
    • a <template> branch, because vue-jsx-vapor 3.2.29 server-renders <template v-if> empty;
    • a <slot> or <component> branch.
  • A branch element with any v- attribute of its own †, plain or namespaced: v-slot, v-slot:name, v-model:value, a custom v-focus, and so on. With v-if beside it, vue-jsx-vapor reads a component's only child with v-slot as the parent's slot declaration and drops the content.
  • An authored v-else after the choice †. If the choice's next element has its own v-else or v-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 new v-if instead of the authored one before it.
  • @if with a non-empty @else. Its createIf already hydrated either branch.
  • In-element @switch. Hydrating it still throws Cannot 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 with setNodes at a text node made by txt(), which the server markup doesn't have, so hydration hands setNodes the case's element as its anchor. Rendering the cases as v-if elements needs the case picked once per update and per render scope. A computed hoisted to the surrounding block escaped v-for and loop scopes, went stale when a parent @if re-created the element, and ran a self-writing discriminant twice on the server. Re-running the switch for each branch picked no branch for a getter discriminant. A sound fix needs vue-jsx-vapor's setNodes to hydrate, or a place for per-render-scope state. [Bug]: vue: a @switch inside 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-if test, 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 with createVaporSSRApp through 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.
  • packages/tsrx-vue/tests/basic.test.js: covers the v-if output, and that the shapes under "Not changed" keep the shared lowering. That includes an @else if chain, v-slot, v-slot:name, a custom directive, and an authored v-else or v-else-if past {null}, {false} and {''}.
  • packages/tsrx/tests/shared/compile.js: the shared >-escaping tests for an @if body now match the text after the tag (>a &gt; b</b>), since Vue's branch carries a v-if.

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

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

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 authored v-else after 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:

    • an empty @else {} counts as no @else;
    • the fallback change covers any child content made only of such @ifs;
    • the v-else guard 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 @if pattern; 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 @if with no @else (or empty @else) and the condition was false on the server. Output no longer uses cond ? <el /> : null (which vue-jsx-vapor lowers to a createIf that can’t adopt the server’s <!---->); for eligible single-element branches it now emits <el v-if={cond} />, which hydrates correctly.

@tsrx/vue adds a controlFlow.ifStatement hook that runs the shared lowering via newly exported ifStatementToJsxChild from @tsrx/core, tags those ternaries with vapor_if metadata, and runs render_element_ifs_as_directives after compile to rewrite only safe cases (@else if, setup/IIFE branches, v-slot, <template>, adjacent authored v-else/v-else-if, etc. still use the old lowering).

Client-visible differences: @if beside expression siblings can retain branch state when siblings re-render (#1402); all-false slot-only @if children can show slot fallback on the client (with a known duplicate-fallback edge case).

Tests cover compile output, SSR markup, and createVaporSSRApp hydration plus KeepAlive @else if behavior.

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

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
jonkwheeler marked this pull request as ready for review November 19, 2026 13:27
@jonkwheeler
jonkwheeler merged commit d9c2a14 into tsrx-org:main Nov 19, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
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
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