The best datastar
I have to start this essay by reminding what every Datastar dev will tell you:
The best Datastar is the less Datastar.
We say that because:
- You should try to build a static website first (or an MPA). Think about it: 90% of your browsing time is reading things! I assure you, trying hard to write something interesting and putting it out there is way harder that importing 10 npm libraries to make a 3d fidget spinner that spells your name.
- Secondly, you should lean on the browser as much as possible. These fuckers are crazy fast, crazy optimized and crazy powerful.
This is why Red Dwarf gives you access to query parameters, cookies, headers and etags.
Use them.
In case of Datastar
With that being said, Red Dwarf is a Datastar server.
That is, Red Dwarf does two things:
- Parse request signals.
- Allow to patch elements.
And that's all you need to be a Datastar server.
Now, if you go take a look at the official Python SDK, you will see that they are a lot more ergonomic, typed, and that they allow you to do more stuff.
However, as I wrote in the real framework essay, Red Dwarf is a teaching tool, and by introducing limitations we hope to make you understand Datastar better.
More often than not, patch is all you need.
- Want to append or remove an element? Prepare it in your HTML. Wrap them in a div with an ID. If they're in a list, persist them in your backend.
-
Need to execute a script? Same deal: prepare a
<script>tag in your HTML. You can morph these tags with Datastar — and much more. - Redirect from the backend? It's just a script tag with a setTimeout. Nothing fancy, no magic, just works.
- Patch signals? Make a signals div, patch that, and you'll be fine.
Embrace what true reactivity looks like.
Embrace what putting state in the right place looks like.
Use Datastar. Read the Tao. Don't overcomplicate it.