Datastar
SSE LiveRegion, loading indicators, SDK script.
# Datastar SDKScript
Injects the Datastar runtime as an ES module. Load once per page:
# Datastar LiveRegion
SSE-powered region that auto-connects on page load and self-heals across server restarts (retry: 'always'). The server pushes datastar-patch-elements events to patch the content in real-time (no polling). The stream also sends invisible 15s keep-alive comment frames (: ping) so proxies and firewalls keep the connection open — the "streamed at" timestamps below are the visible proof of liveness:
Waiting for SSE stream...
# Datastar Indicator
Shows/hides based on a Datastar indicator signal. Place data-indicator:fetching on any element that triggers a backend action:
# Datastar LoadingButton
The label swaps to a busy state while the action is in flight, driven entirely by the data-indicator:saving signal — click and watch (the mock endpoint delays 1.5s):
# Datastar PolledRegion
Auto-refreshing region fetching /api/datastar/tick every 5s via data-on-interval — the Datastar twin of the HTMX PolledRegion. The region self-patches (response root id = region id), the timestamp ticks forward on every patch, and the demo settles after 3 ticks:
Requests served: 48210
# Datastar Signals
Server-pushed values without any HTML round-trip: the endpoint streams datastar-patch-signals SSE events (JSON merge-patch) and the data-text bindings below re-render on every tick. Recipe: docs/recipes/datastar-signals-live-data.md. The live numbers bind as plain data-text nodes on purpose — StatCard keeps its present API (todo #16 verdict: recipe-first; a ValueAttrs-style slot would be a separate owner-gated API change).
- Users online
- 1204
- Requests / s
- 37
# Datastar Reactive Builders
Typed builders for the reactive plugins (datastar.Show, Text, Bind, Signals, Computed, On) — pure client-side reactivity, no backend round-trip. Scope note: the builders exist for consumers' own glue; the library's components stay native-first/htmx-first and are never re-ported to signals (JS decision ladder). Signal names in attribute suffixes stay lowercase (the HTML parser lowercases attribute names — camelCase keys belong in value-borne JSON).
Count exceeds 3 (data-show binding).
# Datastar SignalChip
Filter chips bound to one signal: a click assigns the chip's value to $taskFilter, and every chip's pressed look and aria-pressed follow that signal with no server round-trip. The pressed chip declares the signal's initial value, so the server-rendered state is also the no-JS state. The same signal drives the data-show bindings on the task list:
- Open — Review pull request #42
- Open — Write the release notes
- Done — Upgrade the Go toolchain
@datastar.SignalChipGroup(datastar.SignalChipGroupProps{
AriaLabel: "Filter tasks by status",
Signal: "taskFilter",
Selected: "open",
Items: []datastar.SignalChipItem{
{Label: "All"},
{Label: "Open", Value: "open"},
{Label: "Done", Value: "done"},
},
})# Datastar Action Expressions
Typed helpers for building Datastar action expressions:
datastar.Get("/api/search") // @get('/api/search')
datastar.Post("/api/save") // @post('/api/save')
datastar.Delete("/api/items/1") // @delete('/api/items/1')# Datastar SSE Error Handling
Global listener for Datastar stream failures (datastar-fetch events with type error or retries-failed) that surfaces them as accessible toasts — the Datastar counterpart to HTMX GlobalErrorHandling:
@feedback.ToastContainer("")
@datastar.SSEErrorHandling(datastar.DefaultSSEErrorHandlingConfig())HTMX vs Datastar
Both work side-by-side. HTMX uses hx-* attributes for hypermedia exchanges. Datastar uses data-* attributes for SSE streaming and reactive signals.
Use HTMX for request-response (forms, navigation, CRUD). Use Datastar for real-time push (dashboards, live feeds, collaborative editing).