Serving a web experience similar to React, through Alpine.js, HTMX served by a Go-chi server.
Server bootstrapped from.
-
Fully CSP compliant.
-
Authentication and Authorization flows with JWT.
-
Switch between
jsonandhtmlresponse types based onAcceptheader. Keeps the API interoperable with other clients. -
Caching for
htmlresponses in addition tojsonresponses.
Base features are carried over from the boilerplate.
Run make to see all available commands.
cd server
make packages_installBefore running, make sure generate the tailwindcss styles.
cd server/templates
yarn install
yarn buildAlternatively, you can run
yarn watchto watch for changes during development.
cd server
make run-
All
htmxresponse logic goes into thehtmxdirectory, while thehandlersdirectory contains thejsonresponse logic and invokeshtmxhandlers if the request expectshtmxresponses. -
Create helper functions to return specific
errortypes. This will help you to handle errors in a more granular way. Example:NotFound,Unauthorized,Forbidden,BadRequest,InternalServerErrorcould trigger rendering of a toast notification or a banner. -
Utilize your language's native templating engine to render HTML more dynamically. Example:
Go'shtml/templatepackage orPython'sjinja2package.
-
Lit - A library for building fast, lightweight web components. (Requires a build step, and relies on node.js ecosystem)
-
spf.js - A lightweight JS framework used by YouTube to do a lot of things that HTMX does, while relying on JSON responses from the server.
This approach is not a replacement for React and co. But it can be used when you are looking for the following set of pros, while being aware of the cons.
-
Potentially higher RPS (Requests per second) if coupled with a strong backend, with caching/CDNs and other optimizations.
-
Server Driven UIs. (SEO, Easier A/B testing, etc.)
-
Co-opt (incrementally) with existing applications.
-
Simpler DevOps. (No need to deploy a separate frontend app)
-
Skip expensive JSON serialization and deserialization on both ends.
-
CSP (Content Security Policy) can be a nightmare to configure.
-
More data transferred per Request (HTML is more verbose than JSON)
-
Slower/Constrained Developer Experience (Hard to Debug, Poor IDE support, syntax highlighting, autocomplete, etc.)
-
Code Sharing and Monorepo advantages from a stack like React+React Native are lost.