Key points
This is an internal audit report of HTMX usage in the zhichai.net forum project (HTMX 1.9.12), covering configuration, usage patterns, backend integration, events, security, and testing.
- Configuration: HTMX 1.9.12 is loaded globally via CDN with SRI integrity checks in
views/layouts/base.html. CSRF tokens are automatically injected intoX-CSRF-Tokenheaders for POST/PUT/DELETE requests via thehtmx:configRequestevent. A URL-correction handler instatic/js/app-main.jsrewrites root-path (/) requests to include the current origin and port. - Attribute usage statistics:
hx-get(35+),hx-post(12+),hx-put(3),hx-target(50+, mostly#content),hx-swap(15+),hx-push-url(25+),hx-indicator(20+),hx-confirm(3),hx-trigger(2). Unused:hx-delete,hx-patch,hx-boost,hx-swap-oob,hx-preserve. - Main usage scenarios:
- SPA-style page navigation:
hx-get+hx-target="#content"+hx-push-url="true"+hx-indicator="#loading"(navbar, topic lists, profiles) - Asynchronous form submission: login, registration, topic creation/editing, replies, targeting a
#messageelement - Dynamic content loading: popular-users widget with
hx-trigger="load, refresh-users from:window" - Confirmation dialogs for logout and cache-clearing actions
- Backend integration:
RequestContext::isHTMX()checks theHx-Request: trueheaderTemplateEnginereturns content-only templates for HTMX requests and full layout-wrapped pages otherwise- Controllers (
TopicController10+ call sites,UserController8+,NotificationController1) branch onisHTMX()to return HTML fragments vs. full pages, including HTMX-friendly error alerts - Event handling: Global listeners cover
htmx:configRequest,htmx:beforeRequest,htmx:afterRequest,htmx:responseError(auto-injects error responses into the target element),htmx:sendError, andhtmx:afterSettle(re-initializes Markdown rendering, MathJax typesetting, and notification badges). Page-level listeners handlehtmx:swapErrorandhtmx:timeout. - Known issues:
- A 502 error on back-navigation (caused by HTMX partial updates with unfilled cache) — fixed by reverting back buttons to normal
<a>links - No automatic fallback to full-page navigation when HTMX requests fail
- Some scripts initialize only on
DOMContentLoadedand were not re-initialized after HTMX swaps (partially addressed viahtmx:afterSettle) - Security: CSRF protection via automatic header injection plus backend validation; XSS mitigated with
htmlspecialchars()escaping and DOMPurify for Markdown. Missing: request rate limiting. - Testing: Basic coverage exists (
test/test_htmx.html,htmx_test.html,test/test_redirect.html) for GET/POST, errors, and events; concurrency, network-error, timeout, and large-response scenarios are untested. - High: global error fallback/degradation mechanism; expanded test coverage; a written HTMX usage/coding standard document (prefer normal links for page-level navigation, HTMX for forms and dynamic content; standardize
#content/#messagetargets and#loadingindicators) - Medium: request deduplication, request caching, improved loading UX
- Low: evaluate
hx-boost, server-sent events (SSE)/WebSocket integration, request monitoring and performance profiling
Recommendations (by priority)
Conclusion
HTMX is deeply and coherently integrated into the project, delivering a solid SPA-like experience with good UX and security foundations. Main gaps are the lack of unified usage conventions, error degradation, and edge-case test coverage.