Skip to content

fix: let on_render pass its output to another component inside a template - #344

Merged
JuroOravec merged 1 commit into
mainfrom
fix/on-render-wrap-in-component
Oct 24, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/on-render-wrap-in-component

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Refs #173. This is the first of two PRs: it fixes pages rendered without Vue. The Vue path, including standalone str(Inner()) on an interactive page, follows in a second PR that closes #173.

Problem

An on_render hook can place the output it received inside another component:

class Inner(Component):
    def on_render(self):
        result, error = yield
        return Frame(body=result)

The output the hook received and the replacement around it both carry Inner's render id. When Inner sits inside another component's template, serialization gave both the same placeholder key and raised RuntimeError: The same rendered component id was encountered more than once during serialization. Two related paths failed the same way:

  • an on_component_rendered hook that returns Frame(body=ctx.render).render();
  • a cache hit of either case, with Cache.enabled on Inner or on a component around it.

Fix

serialize.py now tracks the frames around the one it is building (_EnclosingFrames). A child frame whose render id matches some of those frames gets its own placeholder key, numbered by how many of them match. A component's output can only sit inside that same component's output when a hook put it there, so the rule needs no flag on the render, and it still works after a cache replay rebuilds the frames.

What is still rejected:

  • the same output placed twice at one level, as in Pair(first=result, second=result), on its own or nested: both copies get the same key;
  • a render that contains itself: it raises instead of looping forever.

The received output keeps Inner's data-cid-* marker and its other root markers, such as the data-ccss-* attribute that css_data variables target.

Tests

TestWrapReceivedOutputInAComponent in tests/test_on_render.py locks the exact HTML on both the Rust and Python render paths:

  • Inner alone and Inner inside Outer, with a check that the two agree (the same markup, ids shifted by one);
  • a yielded wrapper, two wraps in a row, and two Inners side by side;
  • a cache hit, with Inner cached and with Outer cached;
  • an on_component_rendered hook that wraps the output;
  • the same output placed twice still raising, alone and nested, and a render that contains itself raising;
  • Citry(mode="development") not reporting the kept id as repeated;
  • the css_data marker on the kept output.

16 of these cases fail on main and all pass with the change.

Evidence

  • packages/py/citry/tests and packages/py/citry_ui/tests (-m "not e2e"): 9,497 passed, 53 skipped, 1 xfailed.
  • Every non-cargo phase of scripts/check.py, run by hand: uv lock, ruff check, ruff format, mypy (and --platform linux), citry-client, protocol contracts, protocol Python copies, events protocol JavaScript, validators.
  • This change is Python only. PR CI runs the cargo phases.

Left for the second PR

  • The Vue path: the prepared view still rejects the received output with render occurrence appears more than once.
  • The /advanced/hooks/ page still advises wrapping where the component is used. That advice stays correct until the Vue path works, so it changes with the second PR.

🤖 Generated with Claude Code

…late

An on_render hook that places the output it received inside its
replacement, as in `return Frame(body=result)`, leaves two pieces of
output marked with the component's render id: the output the hook
received, and the replacement around it. When the component sat inside
another component's template, serialization gave both the same
placeholder key and raised "The same rendered component id was
encountered more than once". An on_component_rendered hook that wraps
ctx.render the same way hit the same error, as did either one once the
component, or one around it, was cached.

Serialization now gives a child its own placeholder key when frames
around it have the same render id, numbered by how many of them do. A
component's output can only sit inside that same component's output
when a hook put it there, so this needs no flag on the render and
survives a cache replay. The same output placed twice at one level, as
in `Pair(first=result, second=result)`, still gets one key twice and is
rejected, and a render that contains itself is rejected instead of
looping forever. The received output keeps the component's root
markers, such as the data-ccss-* attribute that css_data variables
target.

The tests lock the HTML of the component rendered on its own and inside
another template and show the two agree. They also cover two wraps in a
row, two components side by side, the cache, on_component_rendered, the
development-mode id check, css_data, and the rejected cases.

This covers pages rendered without Vue. The Vue path follows in its own
change.

Refs #173

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 50db1bb into main Oct 24, 2026
23 of 29 checks passed
@JuroOravec
JuroOravec deleted the fix/on-render-wrap-in-component branch October 24, 2026 21:59
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.

on_render: wrapping the hook's result in another component fails when nested

1 participant