by hombre_fatal · 2y ago · 0 comments
If you have a client that's going to make a request to your server, why not just expose the necessary data at a json route and let the client do what it wishes with that data? It's already the simple, general solution.
With Htmlx, you expose an html route that must be synchronized with the html already sent over the wire so that it can be inlined into the existing html. This means that if the surrounding html/classes change, you may have to update a bunch of fragments because they depend on the existing html. Even if the data itself never changes.
Sending just the data is simpler and more versatile.
If your JSON API is guided by your UI then you are gonna have a fun time keeping with the changes (been there, done that). Sure you know that to add a new field on your page you will need to add it on the API's JSON, then on the JS client code, then on your client template... Compare this to adding it on the HTML generated on the server and... done!
There is value in differentiating between Data API and Hypermedia (UI) API, you can read more about it here https://htmx.org/essays/hypermedia-apis-vs-data-apis/
And note that is not an universal truth applicable on all cases because "it depends"
with a frontend framework i add it to the html template in the frontend, and i add it to the function/class that generates the json output. and again to the database if not already there.
as far as i can tell, that's the same amount of code that needs to be changed.
yes, there is one piece of extra code to generate json, but that is either a standard library or something you write once. either way, the json generator should not need to be changed.
even for a single website i can create multiple frontends (like a mobile client for example) without having to change the backend, because all frontends can use the same json api.