Datastar Observations
Source: Dev.to
Introduction
I’ve been very impressed, so far, with Datastar (https://data-star.dev), a tiny JavaScript library for front‑end work. I switched a personal side‑project from using Svelte for its UI to Datastar, and while Svelte is amazing, Datastar impressed me even more.
Core Concept
Datastar’s essential concept is to shift virtually all logic and markup rendering back to the server. Event handlers can succinctly call server endpoints, which return markup that is morphed into the running DOM. This makes the server‑side the system of record. Datastar provides a nice DSL based on data-* attributes, allowing you to do nearly anything you need in the client declaratively.
Server‑Sent Events
The server can start an SSE (Server‑Sent Events) stream and send down markup to morph into the DOM, or JavaScript to execute, over any period of time. For example, my project has a long‑running process, and it was a snap to create a modal progress dialog and keep it updated as the server‑side process looped through its inputs.
Performance and Morphing
The mantra of Datastar is to “trust the morph and the browser.” It’s surprisingly fast to update even when sending a fair bit of content. Updating a whole page just to change a few small things (e.g., marking a button as disabled) may feel wasteful, but it works, it’s fast, and it frees you from nearly all client‑side reactive updates—and the related edge cases and unforeseen consequences.
Language‑Agnostic Server
The server side is not bound to any particular language or framework. Datastar offers API implementations for Clojure, Java, Python, Ruby, and many others, and you could probably write your own API in an afternoon.
Data Model
I especially like side‑stepping the issue of needing multiple representations of data. The data lives server‑side; all that is ever sent to the client is markup. There’s no over‑the‑wire representation and no parallel client‑side data model. All exposed endpoints are intentional ones that do work and deliver markup—always use‑case based, never schema based.
Comparison to Tapestry
There’s a minimal amount of reactive logic in the client, but the essence of moving the logic to the server feels like home. Tapestry (circa 2005) had similar ideas but was far more limited due to factors such as JavaScript and browser maturity at that time.
Conclusion
I value simplicity, and Datastar fits my needs without being overly magical or hidden. I consider that a big win!