Skip unchanged polls: the ypy-ptag extension
An element that polls gets the same HTML again and again, even when nothing changed. With ypy-ptag the server gives
each answer a tag. The client sends the tag back on the next request, and the server answers 304 when the content is
still the one that tag stands for. YPY does not swap on 304, so the page, the focus and the scroll stay as they are.
Load it
<script src="ypy.min.js"></script>
<script src="ext/ypy-ptag.js"></script>The extension is not part of ypy.max.
Use it
<div ypy-get="/news" ypy-trigger="every 3s" ypy-ptag="start">Latest news...</div>-
The first request carries
YPY-PTag: start(the value of the attribute). Without an initial value no header is sent the first time. - The server answers with
YPY-PTag: <tag>and the HTML. - YPY stores the tag on the element and sends it with each next request.
- When the content still has that tag, the server answers
304with no body.
The tag belongs to the element, not to the page: two polling elements keep two tags.
Attributes
| Attribute | Value | Meaning |
|---|---|---|
ypy-ptag |
string | the first tag the element sends |
Headers
| Header | Direction | Value |
|---|---|---|
YPY-PTag |
request | the current tag of the element |
YPY-PTag |
response | a new tag, kept by the element and sent back next time |
The tag is opaque: a version number, a hash of the content, an update time. Only the server reads it.
Server
The server keeps a tag that changes when the content changes. When the request carries YPY-PTag with the current tag,
answer 304 with no body. Otherwise send the fragment with YPY-PTag set to the current tag.
GET /news
YPY-PTag: 42
HTTP/1.1 304 Not ModifiedNotes
304and204are in the core'snoSwaplist, so nothing is swapped.- It works with any request, not only polls, but polls are where the saving is.
- Pair it with
ypy-poll, which slows polling in a hidden tab and lets the server end it.