columbo
Provides SSR suspense capabilities. Render a placeholder for a future, and stream the replacement elements.
Called columbo because Columbo always said, "And another thing..."
For the purposes of this library, the verb
suspendgenerally means "defer the rendering and sending of an async workload", in the context of rendering a web document.
Overview
The entrypoint for the library is the [new()] function, which returns a
[SuspenseContext] and a [SuspendedResponse]. The [SuspenseContext]
allows you to suspend() futures to be sent
down the stream when they are completed, wrapped with just enough HTML to be
interpolated into wherever the resulting [Suspense] struct was rendered
into the document as a placeholder. [SuspendedResponse] acts as a receiver
for these suspended results. When done rendering your document, pass it your
document and call into_stream() to get
seamless SSR streaming suspense.
So in summary:
- Use [
SuspenseContext] to callsuspend()and suspend futures. - Call
into_stream()to setup your response stream.
To spawn nested suspensions or access the [SuspenseContext] from within a
suspended future (e.g. to listen for cancellation), clone the context before
the async block and capture it by move.
Responses are streamed in completion order, not registration order, so the future that completes first will stream first.
Cancel Safety
By default, if [SuspendedResponse] or the type resulting from
into_stream() are dropped, the futures
that have been suspended will continue to run, but their results will be
inaccessible. If you would like for tasks to cancel instead, you can enable
auto_cancel in [ColumboOptions], or you can use
cancelled() or
is_cancelled() to exit early from within
the future.
Axum Example
use ;
async
Use [new_with_opts] to configure columbo behavior:
use Any;
use ;
use ;
async
Integrations
Both integrations are enabled by default. Disable them individually via
default-features = false and re-enable selectively with features = ["axum"]
or features = ["maud"].
Axum (feature = "axum")
The axum feature implements IntoResponse for HtmlStream, the type
returned by into_stream(). This means you
can return the stream directly from an Axum handler — no manual Response
construction required:
use IntoResponse;
async
The response is automatically sent with Content-Type: text/html; charset=utf-8
and X-Content-Type-Options: nosniff.
Maud (feature = "maud")
The maud feature adds two conveniences:
-
maud::Markup→Html:maud::MarkupimplementsInto<Html>, sohtml! { ... }blocks can be passed directly as futures' return values or as placeholders. -
Suspenseimplementsmaud::Render: [Suspense] values can be interpolated directly intohtml! { ... }macros with(suspense).
use ;
use IntoResponse;
async
Client-side Configuration
Columbo injects a small script that watches for streamed <template> elements
and swaps them into their placeholders. You can customize how swaps are
performed by setting window.__columboConfig before the columbo script
runs:
The swap function receives the placeholder <span> element and an array of
resolved nodes. The default behavior is placeholder.replaceWith(...nodes).
This hook covers any use case without further API surface:
// Opt into View Transitions for smooth swaps
window. = ;
// Use morphdom for minimal DOM diffing
window. = ;
// Add a CSS class for a fade-in animation
window. = ;
Architecture
Internally, [SuspenseContext] holds a channel sender. When
suspend() is called, it launches a task which
runs the given future to completion. The result of this future (or a panic
message if it panicked) is wrapped in a <template> tag and sent as a
message to the channel. A single global <script> is injected once into
the initial document chunk and handles swapping each <template> into its
placeholder.
[SuspendedResponse] contains a receiver. It just sits around until you
call into_stream(), at which point the
receiver is turned into a stream whose elements are preceded by the
document you provide.