Debian Codegraph @

www/src/content/extensions/10-hx-ptag.md

raw symbols SCIP
1 ---
2 title: "hx-ptag"
3 description: "Skip swaps when polled content hasn't changed"
4 category: "Performance"
5 icon: "icon-[mdi--tag-outline]"
6 keywords: ["ptag", "polling", "etag", "conditional", "caching"]
7 ---
8
9 The `ptag` extension provides per-element polling tags (metadata) that let your server skip swaps when content hasn't changed. It works like ETags in HTTP, but at the application and element level rather than the protocol and URL level.
10
11 Useful in polling scenarios where responses depend on what content has already been shipped to the client.
12
13 ## Installing
14
15 ```html
16 <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/htmx.min.js"></script>
17 <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/ext/hx-ptag.js"></script>
18 ```
19
20 ## How It Works
21
22 When a response includes an `HX-PTag` header, the extension stores the value on the source element. On subsequent requests from that element, the stored value is sent as an `HX-PTag` request header.
23
24 The server can use the incoming `HX-PTag` value to determine the appropriate response. If nothing has changed, the server responds with `304 Not Modified` and htmx skips the swap.
25
26 ## Usage
27
28 ### Basic Polling
29
30 ```html
31 <div hx-get="/news" hx-trigger="every 3s">
32 Latest News...
33 </div>
34 ```
35
36 The server returns content along with a ptag header:
37
38 ```
39 HTTP/1.1 200 OK
40 HX-PTag: "v42"
41
42 <div>Breaking: htmx 4 released!</div>
43 ```
44
45 On the next poll, htmx sends:
46
47 ```
48 GET /news HTTP/1.1
49 HX-PTag: "v42"
50 ```
51
52 If nothing changed, the server responds with `304` and no body.
53
54 ### Server-Side Example
55
56 ```python
57 @app.get("/news")
58 def news(request):
59 current_tag = compute_news_hash()
60 client_tag = request.headers.get("HX-PTag")
61
62 if client_tag == current_tag:
63 return Response(status_code=304)
64
65 return Response(
66 content=render_news(),
67 headers={"HX-PTag": current_tag}
68 )
69 ```
70
71 ### Setting an Initial PTag
72
73 Use the `hx-ptag` attribute to set an initial polling tag so the very first request includes it:
74
75 ```html
76 <div hx-get="/news" hx-trigger="every 3s" hx-ptag="v42">
77 Latest News...
78 </div>
79 ```
80
81 Useful when the server renders the initial page and knows the current version.
82
83 ### Using a Timestamp PTag
84
85 If the ptag is a timestamp, the server can use it to determine *what* new content to send, not just *whether* to send it.
86
87 A chat endpoint can use the ptag to only return messages since the last poll:
88
89 ```html
90 <div hx-get="/messages" hx-trigger="every 2s" hx-ptag="2026-03-13T00:00:00Z">
91 </div>
92 ```
93
94 ```python
95 @app.get("/messages")
96 def messages(request):
97 since = request.headers.get("HX-PTag")
98 now = datetime.utcnow().isoformat() + "Z"
99 new_messages = get_messages_between(since, now)
100 if not new_messages:
101 return Response(status_code=304)
102
103 return Response(
104 content=render_messages(new_messages),
105 headers={"HX-PTag": now}
106 )
107 ```