htmx is a hypermedia library that extends HTML with attributes like hx-get, hx-post, and hx-swap — returning HTML fragments from the server instead of JSON. UUID v7 primary keys integrate naturally: the server generates the UUID, persists the record, and embeds the UUID directly in the returned HTML. See UUID in Databases for storage context.
Server-side UUID v7 generation (Go + Chi)
package main
import (
"fmt"
"html/template"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)
var itemTemplate = template.Must(template.New("item").Parse(`
<li id="item-{{.ID}}" class="item">
<span>{{.Name}}</span>
<button hx-delete="/items/{{.ID}}"
hx-target="#item-{{.ID}}"
hx-swap="outerHTML">Delete</button>
</li>
`))
type Item struct {
ID string
Name string
}
func createItem(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id, _ := uuid.NewV7() // UUID v7 — encodes creation time
item := Item{ID: id.String(), Name: r.FormValue("name")}
// persist to database...
w.Header().Set("Content-Type", "text/html")
itemTemplate.Execute(w, item)
}
See UUID in Go for the full google/uuid API.
Cursor pagination with htmx infinite scroll
<!-- Initial page render -->
<ul id="items-list">
{{range .Items}}
<li id="item-{{.ID}}">{{.Name}}</li>
{{end}}
</ul>
<!-- Load-more trigger — cursor is the last UUID v7 -->
<button
hx-get="/items?cursor={{.NextCursor}}"
hx-target="#items-list"
hx-swap="beforeend"
hx-indicator="#spinner">
Load more
</button>
// Server: cursor pagination handler
func listItems(w http.ResponseWriter, r *http.Request) {
cursor := r.URL.Query().Get("cursor")
limit := 20
rows := db.Query(
"SELECT id, name FROM items WHERE id > $1 ORDER BY id LIMIT $2",
cursor, limit+1,
)
// UUID v7 lexicographic > cursor = created after cursor
hasMore := len(rows) > limit
items := rows
if hasMore { items = rows[:limit] }
nextCursor := ""
if hasMore { nextCursor = items[len(items)-1].ID }
renderFragment(w, items, nextCursor)
}
See UUID v7 for Cursor Pagination.
Out-of-band (OOB) swap with UUID v7
<!-- Server returns two fragments in one response -->
<!-- Main fragment: new item -->
<li id="item-018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f" hx-swap-oob="beforeend:#items-list">
New Item
</li>
<!-- OOB fragment: updated item count badge -->
<span id="item-count" hx-swap-oob="true">42 items</span>
The UUID v7 in id="item-..." ties the OOB fragment to the correct DOM node. Multiple concurrent users appending items don’t collide because each UUID v7 is unique.
Python (FastAPI + Jinja2)
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from uuid6 import uuid7
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.post("/items", response_class=HTMLResponse)
async def create_item(request: Request, name: str):
item_id = str(uuid7()) # UUID v7
# persist to DB...
# Return HTML fragment — htmx swaps it into the list
return templates.TemplateResponse("item.html", {
"request": request,
"id": item_id,
"name": name,
})
<!-- templates/item.html -->
<li id="item-{{ id }}">
{{ name }}
<button hx-delete="/items/{{ id }}"
hx-target="#item-{{ id }}"
hx-swap="outerHTML swap:200ms">Remove</button>
</li>
See UUID in Python for the uuid6 package.
Key patterns
| Pattern | UUID v7 role |
|---|---|
| DOM targeting | id="item-{uuid7}" — unique, stable node identity |
| Cursor pagination | hx-get="/items?cursor={uuid7}" — no timestamp needed |
| Optimistic delete | hx-delete="/items/{uuid7}" — server confirms, swap on 200 |
| OOB updates | UUID v7 as fragment ID for multi-target responses |
Further reading
- UUID v7 for Cursor Pagination
- UUID in Go — google/uuid Guide
- UUID in Python — Generation and Validation
- UUID in Databases — Storage and Indexing
- UUID v7 in Hono
External references
Frequently asked questions
How do I use UUID v7 as an hx-target selector in htmx?
Render the UUID v7 as a DOM id prefixed with a letter (HTML ids cannot start with a digit): id="item-018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f". Reference it in hx-target="#item-018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f" for swap targeting. The UUID v7 ties the server record to the live DOM node with no client state. See UUID in JavaScript.
How does UUID v7 cursor pagination work in an htmx infinite scroll?
Render a "Load more" button with hx-get="/items?cursor=<last-uuid-v7>". The server returns the next page fragment, with the button's hx-get updated to the new cursor UUID v7. Since UUID v7 is lexicographically ordered, WHERE id > cursor ORDER BY id is the only query needed — no created_at secondary sort. See UUID v7 for Cursor Pagination.
Should I generate UUID v7 on the server or client in an htmx app?
In htmx applications the server controls all state — generate UUID v7 server-side (Go, Python, Node.js, Ruby) before persisting, then embed the value in the returned HTML fragment. Client-side generation is only needed for optimistic UI patterns where you want an ID before the round-trip completes. For standard htmx usage, server-side generation is simpler and safer. See UUID in Go.