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
json
andhtml
response types based onAccept
header. Keeps the API interoperable with other clients. -
Caching for
html
responses in addition tojson
responses.
Base features are carried over from the boilerplate.
Run make
to see all available commands.
cd server
make packages_install
Before running, make sure generate the tailwindcss
styles.
cd server/templates
yarn install
yarn build
Alternatively, you can run
yarn watch
to watch for changes during development.
cd server
make run
-
All
htmx
response logic goes into thehtmx
directory, while thehandlers
directory contains thejson
response logic and invokeshtmx
handlers if the request expectshtmx
responses. -
Create helper functions to return specific
error
types. This will help you to handle errors in a more granular way. Example:NotFound
,Unauthorized
,Forbidden
,BadRequest
,InternalServerError
could trigger rendering of a toast notification or a banner. -
Utilize your language's native templating engine to render HTML more dynamically. Example:
Go
'shtml/template
package orPython
'sjinja2
package.
-
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.