Skip to main content

HTMX 4 migration

Gojang introduced the bundled HTMX 4.0.2 distribution in v0.5.0. Both v0.5.0 and v0.5.1 serve it locally from /static/vendor/htmx/4.0.2/htmx.min.js. The adjacent README records its source and SHA-256 checksum. Reuse your existing HTMX 4 templates and scripts when upgrading from v0.5.0 to v0.5.1.

Upgrade existing templates and scripts​

  1. Load the embedded HTMX script and /static/js/gojang-htmx.js once in each layout. Remove earlier CDN scripts and duplicate request hooks. If you set CSP_SCRIPT_SRC, keep 'self' so the browser can load both scripts.
  2. Put targets, swaps, and confirmations on the requesting element. Use :inherited on ancestor attributes that are intended to apply to descendants.
  3. Update custom JavaScript to use colon-separated lifecycle events and event.detail.ctx. Read Fetch response fields from ctx.response.
  4. Review validation and other error responses. HTMX 4 swaps error responses by default, so direct them to the form being corrected or a dedicated error region. Use HX-Redirect for an HTMX redirect that needs a full navigation.
  5. Review custom DELETE requests for form values they need to submit explicitly. Move request queuing from hx-trigger modifiers to hx-sync, and use hx-ignore for markup that HTMX should skip.
  6. Check custom extensions against the upstream HTMX 4 migration reference. Gojang uses core morphing and partials and needs no extension for either.

Gojang response behavior​

Both layouts load the shared bootstrap once. It supplies CSRF headers through htmx:config:request and event.detail.ctx.request.headers. Hidden form tokens remain available for native submissions. reCAPTCHA resets on htmx:finally:request. The CSP does not require unpkg or unsafe-eval.

Post mutations return <hx-partial> commands to morph the list and clear the modal. Admin mutations morph the grid, update the toolbar count, and clear the drawer from one query result. Cell updates use outerMorph; stable record and field IDs preserve identity. Grid GETs replace stale requests; cell mutations queue the latest edit rather than aborting a database write. Drawer saves use the current grid query even when its filters or pagination changed after opening. If the grid changes again while the write is pending, the client fetches new results for the latest selection once the write completes. Grid refreshes update the toolbar count, and deleting the last row on a final page returns to the remaining populated page.

Editor requests carry a client-side revision. Closing/opening an editor invalidates older responses for that editor while preserving successful list/grid updates. Use gojangCloseEditor('modal') or gojangCloseEditor('record-drawer') when closing an editor so pending GETs cannot reopen it.

Validation uses HTTP 422 and retains non-sensitive submitted values. Other HTTP errors, including CSRF 403 and rate-limit 429, swap escaped HTML into the persistent #request-errors region. Transport failures display a retry message. Authentication continues to use HX-Redirect. Only successful responses close editors.

Public page rendering honors HX-Request-Type: full; missing/partial retains fragment behavior for HTMX requests. Explicit partial rendering always returns a fragment. Responses vary on HX-Request and HX-Request-Type.

Defaults: same-origin Fetch, explicit inheritance, 60-second request timeout, server-refetched history. Target current Chrome/Edge, Firefox, and Safari.

Verification and release​

Run go test ./... with CGO_ENABLED=0 using the pure Go SQLite driver, then task build. Run npm ci, npx playwright install, and task test-ui for Chromium, Firefox, and WebKit browser coverage. For a direct npm run test:ui, set CGO_ENABLED=0 in the shell first.

Run the pinned read-only checker with npx htmx.org@4.0.2 upgrade-check -- ./app/views/templates ./app/posts/templates ./app/gojang/admin/views. Its line-based inheritance heuristics can flag nested requests that already have explicit targets/swaps and the user-delete button's own CSRF header. Review those warnings against the actual element; do not add inheritance indiscriminately. It also flags the new hx-disable request-disabling attributes as legacy syntax; these deliberately use the HTMX 4 meaning and must not become hx-ignore.

Deploy binary, templates, and assets together. Refresh existing pre-upgrade tabs. Smoke-test post CRUD, admin edits, validation, redirects, and rate limiting in staging. Monitor HTTP errors and browser errors. Roll back the entire release, including response templates, if necessary. HTMX itself requires no schema changes. If this release also replaces the bundled legacy SQLite migrations, follow the legacy SQLite upgrade guide.