htmx brings server-driven HTML fragments to modern web apps. UUID v7 primary keys integrate at the template layer — rendered into element IDs, form action URLs, and query parameters. The server handles all UUID generation; the browser treats UUIDs as opaque strings. See UUID in Databases for storage context.
Go + Chi — full CRUD with UUID v7
package main
import (
"html/template"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
type Item struct {
ID string `db:"id"`
Name string `db:"name"`
}
// POST /items — create, return fragment
func createItem(db *sqlx.DB) http.HandlerFunc {
t := template.Must(template.New("row").Parse(`
<tr id="row-{{.ID}}">
<td>{{.Name}}</td>
<td>
<button hx-delete="/items/{{.ID}}"
hx-target="#row-{{.ID}}"
hx-swap="outerHTML swap:300ms">Delete</button>
</td>
</tr>`))
return func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id, _ := uuid.NewV7()
item := Item{ID: id.String(), Name: r.FormValue("name")}
db.MustExec("INSERT INTO items (id, name) VALUES ($1, $2)", item.ID, item.Name)
t.Execute(w, item)
}
}
// DELETE /items/{id} — delete, return empty string (outerHTML removes row)
func deleteItem(db *sqlx.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
db.MustExec("DELETE FROM items WHERE id = $1", id)
w.WriteHeader(http.StatusOK) // empty body = outerHTML swap removes the row
}
}
Cursor pagination — infinite scroll
// GET /items?cursor=<uuid-v7>
func listItems(db *sqlx.DB) http.HandlerFunc {
t := template.Must(template.New("page").Parse(`
{{range .Items}}
<tr id="row-{{.ID}}">
<td>{{.Name}}</td>
</tr>
{{end}}
{{if .NextCursor}}
<tr id="sentinel"
hx-get="/items?cursor={{.NextCursor}}"
hx-trigger="revealed"
hx-target="#sentinel"
hx-swap="outerHTML">
<td>Loading...</td>
</tr>
{{end}}`))
return func(w http.ResponseWriter, r *http.Request) {
cursor := r.URL.Query().Get("cursor")
limit := 20
var items []Item
if cursor != "" {
db.Select(&items,
"SELECT id, name FROM items WHERE id > $1 ORDER BY id LIMIT $2",
cursor, limit+1)
} else {
db.Select(&items,
"SELECT id, name FROM items ORDER BY id LIMIT $1", limit+1)
}
hasMore := len(items) > limit
if hasMore { items = items[:limit] }
nextCursor := ""
if hasMore { nextCursor = items[len(items)-1].ID }
t.Execute(w, map[string]any{"Items": items, "NextCursor": nextCursor})
}
}
Python (Flask + Jinja2)
from flask import Flask, request, render_template_string
from uuid6 import uuid7
app = Flask(__name__)
ROW_TEMPLATE = """
<tr id="row-{{ id }}">
<td>{{ name }}</td>
<td>
<button hx-delete="/items/{{ id }}"
hx-target="#row-{{ id }}"
hx-swap="outerHTML">Delete</button>
</td>
</tr>
"""
@app.post("/items")
def create_item():
item_id = str(uuid7())
name = request.form["name"]
# persist...
return render_template_string(ROW_TEMPLATE, id=item_id, name=name)
@app.delete("/items/<item_id>")
def delete_item(item_id):
# delete from DB...
return "", 200 # empty response removes the element via outerHTML swap
Out-of-band updates
<!-- Server returns two fragments: new row + updated count badge -->
<tr id="row-018fbe3a-..." hx-swap-oob="beforeend:#items-tbody">
<td>New Item</td>
</tr>
<span id="item-count" hx-swap-oob="true">43 items</span>
Summary of patterns
| Pattern | Template snippet |
|---|---|
| Element ID | id="row-{{.ID}}" |
| Delete target | hx-target="#row-{{.ID}}" |
| Cursor link | hx-get="/items?cursor={{.LastID}}" |
| OOB update | hx-swap-oob="beforeend:#list" |
Further reading
- UUID v7 in htmx — news overview
- UUID v7 for Cursor Pagination
- UUID in Go — google/uuid Guide
- UUID in Python
- UUID in Databases — Storage and Indexing
External references
Frequently asked questions
Do I need a JavaScript UUID library for htmx?
No. In htmx applications the server generates all UUIDs — the client only reads and submits them as form values or URL parameters. Generate UUID v7 server-side in Go (uuid.NewV7()), Python (uuid.uuid7()), or Ruby (SecureRandom.uuid is v4; use the uuidtools gem for v7). See UUID v7 in htmx for framework examples.
How do I use UUID v7 as an htmx element ID?
Prefix the UUID with a letter: id="row-{{.ID}}" — HTML IDs cannot start with a digit or hyphen. Use the same prefix in hx-target: hx-target="#row-{{.ID}}". The UUID v7 is unique across all rows, so targeted swaps on any row work without conflicting IDs. See UUID v7 in htmx.
How does UUID v7 cursor pagination work in htmx?
Render a sentinel element at the bottom of the list with hx-get="/items?cursor={{.LastID}}" and hx-trigger="revealed" (infinite scroll) or as a button. The server queries WHERE id > cursor ORDER BY id LIMIT 20 — UUID v7 lexicographic order is chronological. The response replaces the sentinel with new rows and a new sentinel carrying the next cursor. See UUID v7 for Cursor Pagination.