Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Updates | Use ETags and Status Code 304 #138

Closed
sirinath opened this issue Jul 9, 2020 · 9 comments
Closed

Reduce Updates | Use ETags and Status Code 304 #138

sirinath opened this issue Jul 9, 2020 · 9 comments

Comments

@sirinath
Copy link

sirinath commented Jul 9, 2020

Is it possible to implement a way to send ETags and also status code 304 is returned not to update the document?

@benpate
Copy link
Collaborator

benpate commented Jul 14, 2020

This sounds like an excellent suggestion. In REST APIs, this small step has gone a long way to minimizing bandwidth and speeding up my apps. From the outside, it seems like it might not be too difficult to implement in HTMX itself (but I’d defer to people who actually know)

I‘m guessing there would be extra technical challenges involved on the server side. For instance, if I merge data into an online form, my server would need to verify signatures for both the form and its contents. But, that’s a server-side concern that’s probably out of scope for HTMX itself.

@benpate
Copy link
Collaborator

benpate commented Jul 25, 2020

Hi Sirinath,

I think I misread your first note here. I had forgotten that ETags are already supported by browsers automatically, so there's nothing extra to do inside a framework like HTMX. I just tested a server update using ETag and If-None-Match headers, and everything worked great.

When I re-read your original note, I think you're saying that HTMX should skip the "swap" step if it recognizes that the content is coming from a cached value. Is this correct? If so, I have a counterexample of why this might not be a good idea.

What if I have two tabs, each loaded onto the page as a partial, and the content in each tab uses ETags. When I click back and forth between the two tabs, I need the page to change, even if the XHR request is returning a 304. Does this make sense?

In either case, I'm not sure this is something that could or should be changed.

@sirinath
Copy link
Author

If data is not previously sent to the client send it and then send 304. If something changes the server will know and can send the updated data. If the value has not changed and fully rendered not need to swap. If it is not currently fully rendered then render it with cached values if possible.

@benpate
Copy link
Collaborator

benpate commented Jul 25, 2020 via email

@sirinath
Copy link
Author

sirinath commented Jul 26, 2020

HTMX just send the hash back to the server, the server keeps track and sends 304. HTMX just handles the 304 or new data. If it is 304 then there is no updates, else the swap is done.

@1cg
Copy link
Contributor

1cg commented Sep 7, 2020

We already had support for the 204 response code, adding the 304 response code was easy:

d5f6ef2

@makew0rld
Copy link

How can I get htmx to work with Last-Modified and If-Modified-Since headers, which are similar to etags but use time? Compliant servers will return 304 if the document hasn't been modified, and I assume htmx can handle that now, but will htmx send the Last-Modified header? And remember it and use it from past requests? Or am I misunderstanding and that's something the browser takes care of.

@benpate
Copy link
Collaborator

benpate commented Jan 10, 2021

This should "just work" because the behavior is built in to your web browser. Your server will need to generate the appropriate caching headers, but then all browser AJAX requests (including those from htmx) will obey them.

If you're interested in caching, you might want to check out the preload extension (new in the latest release) which helps you to automatically load certain resources just before your users actually request them.

@makew0rld
Copy link

Ok, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants