Debian Codegraph @

src/ext/hx-ptag.js

raw symbols SCIP
1 //==========================================================
2 // hx-ptag.js
3 //
4 // An extension for per-element polling tags (ptags) that
5 // allow servers to skip swaps when content hasn't changed.
6 //
7 // Usage:
8 // <div hx-get="/news" hx-trigger="every 3s" hx-ptag="initial-value">
9 // Latest News...
10 // </div>
11 //
12 // The server responds with an HX-PTag header. On subsequent
13 // requests, the stored ptag is sent as an HX-PTag request
14 // header. If nothing changed, the server returns 304.
15 //==========================================================
16 (() => {
17 let api;
18 htmx.registerExtension('ptag', {
19 init(internalAPI) { api = internalAPI },
20
21 htmx_after_init(elt) {
22 let ptag = api.attributeValue(elt, "hx-ptag");
23 if (ptag) api.htmxProp(elt).ptag = ptag;
24 },
25
26 htmx_config_request(elt, {ctx}) {
27 let ptag = elt._htmx?.ptag;
28 if (ptag) ctx.request.headers["HX-PTag"] = ptag;
29 },
30
31 htmx_after_request(elt, {ctx}) {
32 let ptag = ctx.response?.headers?.get?.("HX-PTag");
33 if (ptag) api.htmxProp(elt).ptag = ptag;
34 }
35 });
36 })();