Async Rendering & Boundaries
WebComPy's rendering pipeline is asynchronous end to end (async def _render()): components, lifecycle hooks, Suspense fallbacks, and dynamic child refreshes all integrate natively with async/await while preserving deterministic DOM order.
Choose your async pattern based on what needs to happen on the server vs the browser:
| Tool | When to use it | Server behavior (SSR/SSG) | Browser behavior |
|---|---|---|---|
async def component setup |
A component needs data/resources before building its template | Awaited inline before HTML serialization | Awaited during initial render or under enclosing Suspense |
Suspense / suspense() |
Show a loading placeholder while one or more async children settle | Awaits children up to timeout (10s default); emits resolved HTML |
Shows fallback, resolves children via asyncio.gather(), swaps cleanly |
ClientOnly / client_only() |
Subtree depends on browser-only modules, DOM/canvas APIs, or shouldn't pre-render | Never calls children(); emits fallback or empty placeholder |
Hydrates by replacing fallback with children() once live |
use_async_result() |
Reactive data fetching with loading/error states and .refetch() |
Runs after initial render; drained via scheduler for transfer | Restores from transfer payload on initial boot, else runs LOADING → SUCCESS/ERROR |
ErrorBoundary |
Catch synchronous, async-setup, or re-render failures in a subtree | Renders fallback on SSR; fails fast on SSG |
Captures exceptions, runs fallback(error, reset) |
Async component setup and lifecycle hooks#
Decorate an async def function directly with @define_component():
from webcompy import load_text
from webcompy.components import (
ComponentContext,
define_component,
on_before_rendering,
)
from webcompy.elements import html
from webcompy.template import render_markdown
@define_component()
async def ReleaseNotesPanel(context: ComponentContext[dict[str, str]]):
source = await load_text(context.props["resource"])
@on_before_rendering
async def log_visit():
print("Rendering release notes")
return html.SECTION({"class": "notes"}, render_markdown(source))
Key execution contracts:
- Two-phase initialization: Calling
ReleaseNotesPanel(props)stores the unresolved coroutine on the component without touching its template yet, making pending async components discoverable by ancestorSuspenseboundaries before rendering starts. Once awaited, WebComPy extracts any hooks,use_*signals, anduse_async_resultentries registered inside theasync defbody cleanly viaComponentRenderState. - Sequential sibling rendering: Parent elements render their children sequentially (
for child in children: await child._render()). DOM order is deterministic,ContextVarstate flows cleanly along theawaitchain, and an unhandled exception short-circuits subsequent siblings without partial trailing nodes. - Async lifecycle hooks: Both
@on_before_renderingand@on_after_rendering(and theircontext.*methods) accept either synchronous functions orasync defcoroutines.
Coordinating async trees with Suspense#
Wrap one or more async def components in Suspense (or its helper suspense()) from webcompy.elements to display a shared loading state while unresolved async descendant setups finish:
from webcompy.components import ComponentContext, define_component
from webcompy.elements import Suspense, html
@define_component()
def DashboardSection(context: ComponentContext[None]):
return Suspense(
fallback=lambda: html.P({"class": "skeleton"}, "Loading dashboard metrics…"),
children=lambda: MetricsGrid(None),
error_fallback=lambda: html.P({"class": "error"}, "Failed to load dashboard."),
timeout=8.0,
)
Suspense (alias for SuspenseElement) and suspense(*, fallback=..., children=..., error_fallback=None, timeout=10.0) share four parameters, all lazy zero-argument callables (lambda: ...):
fallback: displayed while child async setups are unresolved (timeoutdefaults to10.0seconds).children: evaluated lazily whenSuspenseprepares resolution; all pendingasync defdescendant coroutines within this boundary are gathered concurrently viaasyncio.gather()(each isolated with its own component context), whereas siblingSuspenseboundaries render sequentially one after another.error_fallback: optional fallback rendered if a child raises during resolution. When omitted, the exception propagates outward to the nearestErrorBoundary(or parent caller).timeout: maximum seconds to await async child resolution on both server and browser.
SSR and hydration behavior#
- On the server (SSR/SSG):
Suspenseblocks rendering viaasyncio.wait_for(..., timeout=timeout)and emits the resolved children straight into HTML. Iftimeoutexpires, it logs a warning, destroys the discarded probe subtree, and rendersfallback. - On browser hydration:
Suspensecheckshas_resolved_data(transfer_id)for its async children. When all are already present in#__webcompy_data__(from SSR/SSG), it takes the fast path—adopting the pre-rendered DOM nodes and wiring reactivity with zero fallback flash. Otherwise it hydratesfallbackand schedules resolution inside a render-scoped task reusing the probed children so per-instance transfer IDs and parent DI scopes remain aligned. - Provisional transfer IDs: Fallbacks rendered for timeout, error, or unresolved hydration receive non-consuming provisional transfer IDs so they never shift ordinal
#0/#1counters for sibling components outside the boundary.
Skipping SSR with ClientOnly#
Wrap browser-only widgets (charts, canvas tools, WebGL/audio wrappers, components importing browser packages) inside ClientOnly or client_only from webcompy.elements:
from webcompy.elements import ClientOnly, client_only, html
chart = ClientOnly(
children=lambda: InteractiveChart(None),
fallback=lambda: html.DIV({"class": "chart-placeholder"}, "Loading chart…"),
)
# Functional helper with identical behavior:
analytics = client_only(
children=lambda: AnalyticsTracker(None),
fallback=None, # renders an empty placeholder text node on the server
)
- Server guarantee: During SSR and SSG, the
childrencallable is never invoked—components inside it do not even run__init__, preventingImportErroror DOM crashes from browser-only code. - No wrapper DOM node:
ClientOnlyextendsDynamicElement, mounting its children directly under the parent element without introducing an extra container tag. - Hydration swap: In the browser, hydration cleanly removes the SSR
fallback(or empty placeholder) and mounts the livechildrentree without mismatch warnings.
How AsyncSchedulerPort gates the reveal#
Framework background work (hydrating dynamic containers, Teleport post-hydration mounts, ErrorBoundary resets, use_async_result initial fetches, aio_run tasks) routes through AsyncSchedulerPort (ASYNC_SCHEDULER_PORT_KEY) rather than bare asyncio.ensure_future():
schedule(coro, *, render=True | False)
│
├─ Server (SSR/SSG)
│ Drains ALL tasks recursively (up to 20 passes) on `await scheduler.await_pending()`
│ before `collect_transfer_data()` and `ctx.dispose()` run.
│
└─ Browser (Hydration)
Initial render awaits `await scheduler.await_pending(only_render=True)`
before closing the hydration window and tearing down `#webcompy-loading`.
└─ Tasks with render=True finish before page reveal (routed views are present).
└─ Tasks with render=False (e.g. generic user `aio_run` calls) never delay reveal.
Async signal callbacks (@signal.on_after_updating async def ...) follow intentional environment-specific semantics via the framework scheduler: on the server/test, async callbacks execute to completion before returning so HTML snapshots are deterministic; in the browser, they dispatch asynchronously via aio_run() to keep user interaction responsive while synchronous Computed and text bindings still settle first.
Behavior notes & pitfalls#
- Always wrap
childrenandfallbackin lambdas:ClientOnly(InteractiveChart(None))would evaluateInteractiveChart(None)immediately during SSR beforeClientOnlyruns! Always passchildren=lambda: InteractiveChart(None)andfallback=lambda: .... - Keep
provide()scoped cleanly insideSuspense:Suspensesnapshots and restores the active DI scope after probing children, so aprovide()inside a child does not leak into sibling components. ErrorBoundaryvsSuspense(error_fallback=...): Useerror_fallbackwhen an async fetch/setup failure should stay local to thatSuspense; omit it when errors should escalate to an enclosingErrorBoundary(which providesreset()to retry and obeys SSG fail-fast rules).
Next steps#
- Learn how
use_*composables transfer resolvedSuspenseand async state across SSR in Hydration & Data Transfer. - Understand the full SSR/SSG lifecycle behind
await_pending()in SSR & SSG. - Configure grace periods and removal fades triggered after
await_pending(only_render=True)in Loading Screen. - Revisit
ErrorBoundaryparameters in Error Handling and typed RPC calls in RPC Contracts. - Full API: Elements, Routing & DI API and Data, Forms & i18n API.