Skip to content

fix: catch content printed by a component and its child before its components render twice - #333

Merged
JuroOravec merged 2 commits into
mainfrom
fix/deferred-parts-printed-twice
Oct 25, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/deferred-parts-printed-twice

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Fixes #295

What changes

Here a component prints a slot's result itself and also passes it to a child component tag that prints it too:

class Twice(Component):
    def template_data(self, kwargs, slots):
        return {"body": slots["default"]()}

    template = """
        <c-Show c-body="body" />
        <div>{{ body }}</div>
    """

That used to render the slot's components twice, then fail with "deferred part vanished". With the two prints in the other order, it failed at serialization with "The same rendered component id was encountered more than once". The check from #287 remembered the parts lists it had entered for one scan only, and a scan covers one component's output.

Now every scan in one run of the render queue shares one record of the parts lists it entered and the placeholders it queued (_QueueScans in component_render.py). A scan raises the "printed in more than one place" error, naming the component whose output prints the content the second time, in three cases:

  • It reaches a list again that still holds a component waiting to render (fix: render a nested template printed twice instead of raising #287's message).
  • It reaches a list again whose components already rendered, simple = "vue" output included, from any component's output. New message: "...the components inside it can appear only once on the page...".
  • It would queue a placeholder again from another list, as a copy of a parts list does.

What still works as before:

  • A list that holds no components can be printed anywhere.
  • A placeholder repeated within one list, as an extension hook may do, still renders at each position.
  • When an on_render hook returns, yields or wraps the output it received, the record forgets that output before the replacement is scanned, because the output moved rather than being printed twice. A replacement that holds the output twice still raises.

Known limit, stated in docs/design/component_rendering_defer.md: a nested run keeps its own record. One example is a component placed in an expression, {{ el }} where el = Show(body=body). Content printed both inside such a nested run and in the outer one is not caught. It still fails at serialization when it holds an ordinary component.

The errors say what to do (second commit)

The maintainer agreed to keep the error and asked for it to suggest the fix. The second commit adds that:

  • Shared advice. Every "printed in more than one place" error ends with the same advice (_PRINT_IN_MANY_PLACES_ADVICE in citry_render.py): "To show it in several places, pass the slot itself instead of calling it, or a component element such as MyComponent() instead of MyComponent().render(): each place that prints a slot or an element renders its own copy. In an on_render hook, place the output the hook received only once." A slot or an element renders again at each place that prints it, so each place gets its own copy of the components inside. I checked this with and without Vue.
  • Finished output printed twice. A finished output such as Row().render() printed twice is caught when the page is serialized, because each print wraps it in a new render. That serialization error now reads "One rendered output is printed in more than one place on the page, so the components in it would appear more than once." and carries the same advice. On a page that uses Vue, the Vue capture now raises that same RuntimeError. It used to raise a TypeError, "render occurrence appears more than once". Nothing catches that error by its type: every except UnsupportedPreparedView wraps a different call.
  • A render that contains itself gets its own message, since nothing is printed twice there.
  • Docs. /concepts/slots/ has a new section, "Show a fill twice". It shows the first try, why it fails, and the fix: pass the Slot itself. It also covers the dict form, typing a child's input as Slot, and passing an element instead of its rendered output. The rendering page's warning links to it.

Tests

New tests in tests/test_deferred_render.py, each run on both render paths:

  • the issue's example in both print orders, with the component path in the message;
  • text-only content printed by a component and its child, which renders;
  • on_render return, yield and wrap of the received output, which render;
  • a replacement holding the output twice, which raises;
  • simple = "vue" slot content printed by a component and its child, which raises.

The new cases that expect an error fail on main. The text-only and on_render cases guard against false positives; the on_render ones also caught a false positive in this PR's first version.

The second commit adds tests, also on both render paths:

  • the full text of both "printed in more than one place" messages;
  • a rendered component printed twice, with and without Vue, with the full message;
  • passing the slot, and passing the element, each rendering a copy in each place, with and without Vue.

The message tests fail without the second commit.

Existing tests changed:

Test Reason
test_call_run_tasks.py::test_a_row_list_printed_again_after_its_first_row_renders_raises_before_rendering_a_row_twice (was ..._renders_only_the_unplaced_rows) It locked the old "deferred part vanished" failure for a copied row list. As the issue asks, the copy now raises the new error before any row renders twice. Its expected message now ends with the advice.
test_on_render.py (two tests), test_ext_debug.py::test_decorated_component_root_cannot_be_reused_twice They matched serialization's old wording, "encountered more than once"; they now match the new message. The cycle test matches the new message for a render that contains itself.
test_vue_direct_relationships.py (the duplicate #c-key test) Its regex also accepted the old Vue message, which no longer exists.

Evidence

Run locally on this branch, rebased onto origin/main 2640d24, with a citry_core built from main b3491f0:

PR CI runs the cargo phases. This change has no Rust.

Review

An independent adversarial review ran twice on each commit, each time with a prose pass. A separate first-time reader also read the new docs section twice. All findings are folded in:

  • a false positive for on_render hooks that keep their output;
  • simple = "vue" output not counted as components;
  • docs and changelog limits;
  • a check that now also catches finished content printed twice by the same output;
  • the Vue path, which still gave the old message for a finished output printed twice;
  • advice that was wrong for a render containing itself and pointed nowhere for an on_render hook;
  • a docs example that broke for a component with a Slots class.

Known gap, already there before this PR: a simple="vue" component's Row().render() printed twice with deps_strategy="ignore" or "simple" raises no error and prints twice.

🤖 Generated with Claude Code

@JuroOravec
JuroOravec force-pushed the fix/deferred-parts-printed-twice branch from 5d8d31f to bd3f084 Compare October 24, 2026 22:25
JuroOravec and others added 2 commits October 25, 2026 01:43
…mponents render twice

A slot's result that a component printed and also passed to a child that
printed it rendered the slot's components twice and then failed with
"deferred part vanished", or, with the two prints in the other order,
failed at serialization with "The same rendered component id was
encountered more than once". #287's check remembered the parts lists it
entered only for one scan, which covers one component's output.

Every scan of one render queue run now shares a record (_QueueScans) of
the parts lists entered and the placeholders queued. A scan raises the
"printed in more than one place" error, naming the component whose
output prints the content the second time:
- when it reaches a list again that still holds a component waiting to
  render;
- when it reaches a list again whose components already rendered,
  simple="vue" output included, from any component's output (new
  message: they can appear only once on the page);
- when it would queue a placeholder again from another list, as a copy
  of a parts list does.
A list with no components is still skipped, and a placeholder repeated
within one list still renders at each position.
When an on_render hook returns or wraps the output it received, the
record forgets that output before the replacement is scanned, since the
output moved rather than being printed twice.

The locked test for a copied row list now expects the new error before
any row renders twice. The design doc says what the check covers and
what it does not (content printed inside a nested run).

Fixes #295

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The errors for content printed in more than one place now say how to
show it in several places: pass the slot itself instead of calling it,
or a component element such as `MyComponent()` instead of
`MyComponent().render()`. A slot or an element renders again at each
place that prints it, so each place gets its own copy of the components
inside. An on_render hook has no slot or element to pass, so the advice
also says to place the output the hook received only once.

A finished output such as `Row().render()` printed twice is caught
later, when the page is serialized, because each print wraps it in a new
render. Serialization's error for it now reads "One rendered output is
printed in more than one place on the page" with the same advice, and
the Vue capture raises that same RuntimeError on a page that uses Vue,
where it used to raise a TypeError, "render occurrence appears more than
once". A render that contains itself gets its own message, since nothing
is printed twice there.

The slots page gets a section that shows the first try, why it fails,
and the fix, and the rendering page's warning links to it.

Refs #295

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the fix/deferred-parts-printed-twice branch from bd3f084 to fdcde67 Compare October 25, 2026 00:06
@JuroOravec
JuroOravec merged commit 079e17e into main Oct 25, 2026
13 of 26 checks passed
@JuroOravec
JuroOravec deleted the fix/deferred-parts-printed-twice branch October 25, 2026 08:39
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.

A slot result printed by a component and by its child renders the slot's components twice, then fails with "deferred part vanished"

1 participant