Skip to content

feat(serialize): return asset tags apart from the HTML with deps_position="separate" - #801

Merged
JuroOravec merged 4 commits into
mainfrom
feat/serialize-separate-deps
Nov 19, 2026
Merged

JuroOravec merged 4 commits into
mainfrom
feat/serialize-separate-deps

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

(Opus 5.5)

Adds deps_position="separate" and CitryRender.needs_browser_runtime(), both Experimental, for citry 1.1.0. They are items C1 and C2 of the citry-django Vue redesign. citry-django will render each interactive region of a Django page as its own Vue app and write each shared asset tag once per page.

What changes

  • serialize_result(deps_position="separate") returns the HTML with no asset tags. The tags come back in three new fields of SerializedRender, each a new SerializedAssets(css, js):
    • runtime_assets: Citry's own code that every app shares (the runtime, extension scripts, preload hints, and the inline component code when no web integration is mounted);
    • app_assets: one app's stylesheets, configuration data and start script;
    • component_assets: every other tag.
  • Vue adopts the server HTML with "separate" exactly as it does with "smart".
  • deps_strategy="fragment" with "separate" raises ValueError. With "ignore", every group is empty. A hook that changes a returned tag makes serialize raise RuntimeError.
  • CitryRender.needs_browser_runtime() returns whether a render needs the browser runtime. Its docs state the contract: a child component's render may be serialized as its own app before its parent, as long as the child holds no slot content that a component outside it wrote. Such a child raises TypeError with a message that says so.
  • Docs: a new "Combine several renders" section in Place JavaScript and CSS, the Experimental list on the versioning page, an Unreleased changelog line, and design-doc updates.

Plan

Prior art (line numbers on origin/main, 497da50):

  • serialize.py:651 serialize_render_result runs one Vue analysis per call, and serialize.py:1148 returns SerializedRender(html, security) with no asset data.
  • Hydration requires deps_position == "smart" at serialize.py:247, :769 and :785, and at _vue/serialization.py:1284 and :1522.
  • Tags are placed in two places: ext/dependencies/emission.py:450 (_place_dependency_html, through _insert_default at :1334) and _vue/serialization.py:638 (VueSerializationPlan.finalize, with preload hints from _insert_script_preloads at :237).
  • The per-call notes in context.extra are cleared and restored through SERIALIZATION_NOTE_KEYS (emission.py:75).
  • analyze_vue_serialization (_vue/serialization.py:903) already gives the same "needs Vue" test that serialize uses.
  • Nothing returned tags apart from the HTML before this change.

Chosen design. "separate" places every tag exactly where "smart" would. Each tag is wrapped in a comment pair that carries a random value chosen for that one call. The wrapped tags stay in the page through the Vue host check, the nonce and integrity pass, and the CSP and JavaScript checks. Only after script_security.finalize are they cut out and grouped. So the returned tags, their attributes and result.security are identical to what "smart" writes, and the hydration checks accept "separate" wherever they accept "smart".

Alternatives rejected.

  1. Insert the tags as today and let the host find them by parsing Citry's data-citry-vue-* attributes. The design rejects this because those attributes are not covered by the 1.x promise.
  2. Collect the tags into lists and never insert them. The CSP and JavaScript checks and the integrity pass would then never see them, so the returned tags could differ from what "smart" writes and checks. Every check would need a second code path, and a check on one path only is a hole on the other (Mechanism 7 step 3).

Result that would show the design is wrong. Either of these:

  • a tag that "smart" writes is missing from the groups or differs from them, or result.security differs;
  • a separate island does not hydrate, or two islands with one shared runtime fail in a browser.

test_returned_tags_are_the_tags_smart_inserts checks the first case with a nonce, with integrity and under strict CSP. The reviewer also compared 576 combinations of options. The browser test checks the second case.

Benchmark

Decision: does this slow existing pages? Threshold: flag any existing serialize scenario more than 3% slower. Budget: about 10 minutes. Each scenario renders and serializes with the default position, timing the median of N calls in a new process. Each one ran 11 times, alternating origin/main and this branch. Machine: Apple Silicon, Python 3.12, citry-core 2.1.2 wheel from PyPI.

Scenario origin/main (ms) branch (ms) Change
large benchmark page (test_benchmark_citry.py) 66.999 67.142 +0.2%
small benchmark page (test_benchmark_citry_small.py) 0.137 0.135 -1.3%
one Vue island with 50 rows, mounted 1.349 1.364 +1.1%
Vue document with 100 interactive rows, hydrated 6.323 6.101 -3.5%

An earlier run that compared main with an identical copy of itself varied by up to 2.8%. No scenario is slower by more than that noise. For comparison, serializing the island with "separate" took 0.44 ms against 0.57 ms with "smart". With "smart" and no <head>, Citry still parses the page to look for one before writing the preload hints.

Security (Mechanism 7)

This change touches:

  • JavaScript and JSON: the configuration block and start script are now handed to the host;
  • the CSP nonce on every returned tag;
  • markers for Citry's browser code: the new wrapper comments and app ids;
  • the opt-in output checks.

An independent reviewer tried to break each route:

  • A forged wrapper in trusted HTML stays visible as text, because it cannot carry the 128-bit per-call value.
  • A wrapper inside raw text gives back the bytes the checks saw. In the worst case it leaves the value behind, and serialize raises.
  • Nonce, integrity and CSP hashes match "smart". omit, forbid, warn findings and strict refusals match too. Two apps ran under a real strict CSP in Chromium and Firefox.

No hole was found. The reviewer also ran the Mechanism 8 advisory-survey pass and found no bug kind introduced.

Versioning

New Experimental API, so it ships in a minor release (1.1.0). DepsPosition gains a value, and SerializedRender gains three trailing fields with defaults. Construction with two arguments, keyword construction and match patterns keep working. Two things do change:

  • dataclasses.fields() and astuple() now return five items.
  • A SerializedRender pickled under 1.0.x and unpickled under 1.1.0 lacks the new fields.

An extension that branches on OnSerializeContext.deps_position sees "separate"; tags it inserts stay in the HTML, as the docs say.

Tests

  • test_serialize_separate_assets.py: 25 tests. They cover parity with "smart" (default, nonce, integrity, strict CSP), the groups, hydration, a document's placement tags, static pages, hook scripts, the path without mounted routes, ignore, fragment, JavaScript warn/omit/forbid, a forged wrapper, a hook that edits a tag, needs_browser_runtime(), a child serialized before its parent, and a child with slot content from outside.
  • e2e/test_serialize_separate_assets_e2e.py: two separate islands share one runtime on one page. Both hydrate (the elements the server wrote stay) and run their own handlers. It passes in Chromium, Firefox and WebKit.
  • test_vue_migration_coverage.py matched the old internal text of the slot-content error; it now matches the new message.
  • The existing suite (15,197 passed) passes, apart from two failures that also fail on origin/main in this environment (a pydantic and a tzdata test). The related Vue/serialize e2e suites pass in Chromium. ruff, mypy, mypy --platform linux and python -m docs_site build-check are clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom

JuroOravec and others added 4 commits November 19, 2026 16:16
…tion="separate"

Code that puts several Citry renders on one page (citry-django renders each
interactive region as its own Vue app) can now ask serialize_result() for the
HTML without asset tags and get the tags back in three groups on
SerializedRender: runtime_assets, app_assets and component_assets. Each group
is a SerializedAssets with css and js tuples. The tags are placed exactly as
"smart" places them, wrapped in comments that carry a per-call random value,
and cut out only after the nonce, integrity, CSP and JavaScript checks, so
they match the tags "smart" writes. Vue hydrates the HTML as with "smart".

CitryRender.needs_browser_runtime() tells whether a render becomes a Vue app
when serialized on its own, and its docs state that a child component's
render may be serialized on its own before its parent.

Both are Experimental and listed on the versioning page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
…eview findings

A child component's render that holds slot content written outside it cannot
be the root of its own Vue app, because Vue has no owner for that content
there. The contract now says so, and serialize raises TypeError with a message
that names the case instead of an internal ownership error.

Without mounted routes, Citry's inline component code goes to runtime_assets
with the runtime, so component_assets holds only ordinary tags. The docs page,
docstrings and changelog line are rewritten after an independent review and a
cold read, and new tests cover hook scripts, the path without mounted routes,
JavaScript forbid mode and the slot-content refusal.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
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