Fragments & SSR IPC Reference¶
API reference for UI component fragments (litestar_vite.fragments) and the Server-Side Rendering IPC transport and circuit breaker subsystem (litestar_vite.ipc).
UI Component Fragments & Islands¶
FragmentEngine
¶
Orchestrates rendering UI component fragments over IPC transports.
get_component_css_urls
¶
Extract all CSS file URLs for a component from manifest or dev server.
In production mode, looks up the component in the parsed manifest.json, collects its direct 'css' array, and recursively traverses 'imports' chunks to collect imported CSS dependencies with cycle detection. Normalizes paths using POSIX formatting to support Windows environments.
In development mode, returns the direct component CSS URL or Vite dev server URL only for dedicated stylesheet files, omitting component entries such as .vue and .tsx to prevent browser MIME type rejections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
str
|
Relative path to the component, e.g. 'components/UserProfile.vue'. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of resolved CSS URLs. |
generate_component_css_tags
¶
Generate HTML link tags for a component's scoped styles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
str
|
Relative path to the component entrypoint. |
required |
newline
|
str
|
Line ending character to join multiple tags. |
'\n'
|
Returns:
| Type | Description |
|---|---|
str
|
String of HTML link tags, or empty string if no CSS is associated. |
render_fragment
¶
Render a UI component fragment to HTML asynchronously.
Sends render request to the background IPC worker, retrieves the rendered markup, and prepends scoped CSS link tags resolved from manifest.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
str
|
Path to the component entrypoint, e.g. 'components/Counter.vue'. |
required |
props
|
dict[str, Any] | None
|
Optional dictionary of component props. |
None
|
mode
|
Literal['static', 'island']
|
Rendering mode, either 'static' (zero JS) or 'island' (interactive). |
'static'
|
Returns:
| Type | Description |
|---|---|
str
|
Rendered HTML string with scoped CSS links prepended. |
Raises:
| Type | Description |
|---|---|
ImproperlyConfiguredException
|
If the IPC worker reports a render error. |
render_fragment_sync
¶
Render a UI component fragment synchronously for template engines.
Executes the async render_fragment method on the worker loop using anyio. Falls back to anyio.run when no event loop is active on the current thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
str
|
Path to the component entrypoint. |
required |
props
|
dict[str, Any] | None
|
Optional dictionary of component props. |
None
|
mode
|
Literal['static', 'island']
|
Rendering mode. |
'static'
|
Returns:
| Type | Description |
|---|---|
str
|
Rendered HTML string with scoped CSS links prepended. |
ComponentResponse
¶
HTTP Response that renders a server-side component fragment.
Renders UI component fragments (React, Vue, Svelte, Astro) into HTML markup for HTMX partial swaps and micro-frontend architectures.
to_asgi_response
¶
to_asgi_response(
app,
request,
*,
background=None,
cookies=None,
encoded_headers=None,
headers=None,
is_head_response=False,
media_type=None,
status_code=None,
type_encoders=None,
)
Construct the ASGI response that renders the component fragment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Any
|
The Litestar application instance. |
required |
request
|
Request[Any, Any, Any]
|
The incoming request. |
required |
background
|
BackgroundTask | BackgroundTasks | None
|
Optional background tasks. |
None
|
cookies
|
Any
|
Optional cookies. |
None
|
encoded_headers
|
Any
|
Optional encoded headers. |
None
|
headers
|
dict[str, str] | None
|
Optional headers dictionary. |
None
|
is_head_response
|
bool
|
Whether the request is a HEAD request. |
False
|
media_type
|
Any
|
Response media type. |
None
|
status_code
|
int | None
|
Optional status code override. |
None
|
type_encoders
|
Any
|
Type encoders mapping. |
None
|
Returns:
| Type | Description |
|---|---|
ASGIResponse
|
An ASGIResponse callable that executes the fragment rendering. |
vite_fragment
¶
Render a component fragment within a Jinja2 template.
Merges explicit props with keyword arguments passed to the template callable.
Example in Jinja template
{{ vite_fragment("components/UserProfile.vue", user=user) }} {{ vite_fragment("components/Counter.tsx", props={"initial": 0}, mode="island") }}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Mapping[str, Any]
|
The Jinja2 template context containing the request. |
required |
component
|
str
|
Relative path to the component entrypoint. |
required |
props
|
dict[str, Any] | None
|
Optional dictionary of component props. |
None
|
mode
|
Literal['static', 'island']
|
Rendering mode ('static' for HTML only or 'island' for interactive). |
'static'
|
**kwargs
|
Any
|
Additional props passed as keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
Markup
|
Markup-safe rendered HTML string with scoped styles. |
SSR IPC Transports¶
BaseIPCTransport
¶
Bases: Protocol
Protocol defining the asynchronous IPC transport contract.
Attributes:
| Name | Type | Description |
|---|---|---|
is_running | bool | Return True if the transport is running and ready to handle requests. |
send_request
¶
Send a JSON payload and return the response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict[str, Any]
|
Request dictionary payload. |
required |
timeout
|
float
|
Maximum duration in seconds to wait for a response. |
10.0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Decoded response payload dictionary from the worker. |
StdioIPCTransport
¶
Bases: BaseIPCTransport
Asynchronous process transport communicating across stdin and stdout using NDJSON.
Attributes:
| Name | Type | Description |
|---|---|---|
command | list[str] | Return the worker command list. |
cwd | Path | None | Return the configured working directory for the worker process. |
is_running | bool | Return True if the transport is running and ready to handle requests. |
send_request
¶
Send an NDJSON request to the worker and await the correlated response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict[str, Any]
|
Request dictionary payload. |
required |
timeout
|
float
|
Maximum time in seconds to await response. |
10.0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Worker response dictionary. |
Raises:
| Type | Description |
|---|---|
IPCTimeoutError
|
When response is not received within timeout. |
IPCWorkerCrashError
|
When child worker exits unexpectedly. |
IPCError
|
When worker reports an execution failure. |
WasmIPCTransport
¶
Bases: BaseIPCTransport
In-process SSR transport executing self-contained SSR bundles via QuickJS.
Attributes:
| Name | Type | Description |
|---|---|---|
bundle_path | Path | Return the configured SSR bundle path. |
is_running | bool | Return True if the in-process SSR context is initialized and ready. |
send_request
¶
Serialize a request, dispatch it to the in-process SSR worker, and decode the response.
Dispatches are serialized on a single engine thread. timeout bounds the
execution of this request only; time spent waiting for an earlier request
to finish is not counted, so a queued request never resets a healthy engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict[str, Any]
|
Outbound IPC request dictionary. |
required |
timeout
|
float
|
Maximum duration in seconds to wait for a response once dispatched. |
10.0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Decoded response dictionary from the SSR worker. |
TCPStreamIPCTransport
¶
Bases: BaseIPCTransport
Send development SSR RPC requests through a pooled HTTP client.
Attributes:
| Name | Type | Description |
|---|---|---|
host | str | Return the target hostname or IP address. |
port | int | Return the target TCP port. |
path | str | Return the configured HTTP endpoint path. |
scheme | str | Return the endpoint protocol. |
is_running | bool | Return True because connections are established lazily on the next request. |
send_request
¶
Send an RPC request within a total deadline and validate its response.
Raises:
| Type | Description |
|---|---|
IPCTimeoutError
|
The request exceeded its total deadline. |
IPCError
|
The connection failed or the server returned an invalid response. |
resolve_ssr_transport
¶
Resolve the production SSR IPC transport according to runtime configuration and host capabilities.
Resolution order
runtime.ssr_transport == "wasm"->WasmIPCTransport.runtime.ssr_transport == "stdio"->StdioIPCTransportusingresolve_ssr_command.runtime.ssr_transport == "auto"(default): a. Explicitssr_config.command->StdioIPCTransport. b. Configured JS runtime binary available onPATHor venv ->StdioIPCTransport. c.is_wasm_available()isTrue->WasmIPCTransportfallback. d. Default toStdioIPCTransportusingresolve_ssr_command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ViteConfig | None
|
Optional active |
None
|
ssr_config
|
InertiaSSRConfig | None
|
Optional resolved |
None
|
Returns:
| Type | Description |
|---|---|
BaseIPCTransport
|
Configured |
is_wasm_available
¶
Return True when the in-process QuickJS runtime (litestar-vite[wasm]) is importable.
IPCRequest
¶
Structured representation of an outbound JSON-RPC/NDJSON IPC request payload.
Attributes:
| Name | Type | Description |
|---|---|---|
id | int | Monotonically increasing request correlation identifier. |
method | str | RPC method name ( |
params | dict[str, Any] | None | Optional JSON-serializable parameters dictionary passed to the worker method. |
IPCResponse
¶
Structured representation of an inbound JSON-RPC/NDJSON IPC response payload.
Attributes:
| Name | Type | Description |
|---|---|---|
id | int | Correlation identifier matching the originating |
result | Any | Decoded response payload returned by the worker when successful. |
error | str | None | Error message string returned by the worker when execution fails. |
Circuit Breaker & IPC Exceptions¶
SSRCircuitBreaker
¶
Three-state circuit breaker pattern for SSR worker stability.
Attributes:
| Name | Type | Description |
|---|---|---|
state | CircuitState | Return the current circuit state, evaluating cooldown if currently OPEN. |
failure_count | int | Return the current consecutive failure tally. |
failure_threshold | int | Return the threshold of failures required to open the circuit. |
cooldown | float | Return the configured cooldown period in seconds. |
reset_timeout | float | Return the configured reset timeout in seconds. |
can_execute
¶
Determine whether an outbound request is permitted through the breaker.
Returns:
| Type | Description |
|---|---|
bool
|
True if request is allowed, False if breaker is open. |
allow_request
¶
Return whether an outbound request is permitted through the circuit breaker.
Returns:
| Type | Description |
|---|---|
bool
|
True if request is allowed, False if breaker is open. |
record_success
¶
Record a successful execution, resetting failures and closing the circuit.
record_failure
¶
Record an execution failure, potentially tripping the circuit to OPEN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
BaseException | None
|
Optional exception that caused the failure. |
None
|