Skip to content

Lazy / streaming rendering #19

Open

Description

@JuroOravec

Support deferring/streaming render output instead of producing the whole HTML string synchronously.

Why: django-components#1337. Large pages could start emitting before the whole tree finishes rendering.

Current state: the pipeline is eager. Rendering is already deferred and depth-unbounded (stack-driven), but there is no streaming/lazy part type: RenderPart = str | CitryRender | DeferredComponent | Placeholder (packages/py/citry/citry/citry_render.py).

Upstream: django-components#1337.

Parked: design first, not yet scheduled.

Activity

  1. added
    enhancement
    on Jul 1, 2026
  2. JuroOravec commented on Sep 23, 2026

    @JuroOravec
    MemberAuthor

    (GPT Astra 5.6 medium)

    Findings from the September 2026 performance experiments

    Streaming is worth exploring as a general Citry feature. We are deferring production implementation and excluding this experiment from the main framework benchmark ranking. The prototype uses an application-owned Django ASGI route; any future comparison would need comparable streaming configurations for other frameworks and explicit disclosure of application-level optimizations. Route ownership is a separate question tracked in #143.

    Measured opportunity

    On the local 1,400-output page, one measured warm second-load pair compared the same experimental partial-SSR output delivered buffered versus streamed:

    Measurement Buffered Streamed Difference
    Request start to framework-ready callback 392.2 ms 372.8 ms −19.4 ms
    Request start to layout-inclusive endpoint 404.3 ms 385.3 ms −19.0 ms
    Server request start to completed serialization 222.1 ms 230.0 ms +7.9 ms

    The streamed HTML started arriving about 52 ms before server serialization finished. Both responses were 2,482,204 uncompressed bytes and passed page/interaction checks. Their live IDs differ; byte parity was checked separately with fixed IDs.

    These are one-pair observations, not a repeatable performance guarantee, a comparison against ordinary Citry rendering, or savings to add to earlier SSR results. The layout-inclusive endpoint schedules animation-frame callbacks directly from readiness, forces the fixed page/last-row geometry in the first callback, and records entry to the next callback. It includes pending layout but does not prove physical display paint. Server time above ends when the serializer finishes constructing the response. It includes scheduling and coordination/send waits, excludes final suffix delivery, and is not CPU time. Network conditions, compression and buffering can change the benefit.

    What the prototype actually does

    1. Render the entire Python component tree and produce the page HTML.
    2. During serialization, identify and validate a document prefix containing the complete Vue-owned HTML host.
    3. Send that prefix through the Django streaming response.
    4. Finish preparing the Vue component definitions, occurrence data, dependencies and startup output while the browser can parse the prefix.
    5. Send the remaining document.

    This is two-stage serialization, not yielding each component as it renders. The prototype still constructs the complete final HTML string and checks that it begins with the prefix already sent; it does not establish a memory reduction. It covers the main-page GET, not Events responses.

    The serializer supplies a private prefix-publishing callback. A worker publishes the prefix and waits for acknowledgement that ASGI sent it; a genuine async response iterator yields the prefix, lets serialization continue, then yields the suffix. Production should not inherit that research coordination machinery without a separate API design.

    Illustrative desired API, not an existing Citry API:

    # Citry provides chunks; the application owns URLs and responses.
    async def page_view(request):
        tree = Page(user=request.user).render()
        chunks = iter_serialized_chunks(tree)
        return StreamingHttpResponse(chunks, content_type="text/html")

    Conceptual serializer:

    async def iter_serialized_chunks(tree):
        plan = prepare_output(tree)  # validate what must precede sending
        yield plan.initial_html
        yield await plan.finish_browser_data_and_document()

    The actual sync/async interface remains undecided. A generic iterator must preserve callback order and run application code once; it must not rerender the tree to recover from a rejected streaming path.

    Work needed for production

    • Define which bytes are final before sending: head content, dependencies, root attributes, extension hooks, CSP/nonces/integrity and security validation must not later change emitted output. The prototype admits only a tightly controlled page/configuration.
    • Decide whether extensions need explicit declarations of what they inspect or mutate, and when unsupported cases use buffered output.
    • Define failures: before sending, buffered fallback may reuse the rendered tree; after sending, status/headers cannot be replaced and the response may need to terminate. No silent rerender or pretend rollback.
    • Handle disconnects, cancellation, backpressure and worker cleanup. Keep async streaming truly incremental; an adapter that buffers an iterator can erase the benefit.
    • Test compression, proxies and middleware that buffer responses. Buffered delivery must remain correct; early delivery is an optional benefit.
    • Generalize beyond this exact partial-SSR page, including dependency ordering, normal component callbacks, client startup and later server updates. Component-by-component streaming during rendering is a further capability, not proved here.
    • Measure repeated end-to-end outcomes, time to first content, memory and throughput before choosing a public API or default.

    A useful implementation detail: the prototype initially validated the same four HTML regions per row before sending and again while preparing Vue data. Reusing validation only for the exact same immutable region strings within that serialization removed the second scan. If the objects differ, normal validation runs. This preserves validation rather than merely skipping it for speed.

  3. added
    next-sprint
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementnext-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions