Can I use the browser local cache with HTMX? #1531
Replies: 5 comments 2 replies
-
What advantage would this have over normal caching in browsers with the Cache-Control header? |
Beta Was this translation helpful? Give feedback.
-
@dz4k I think the problem is cache invalidation. Let's say you're caching fragments of a dashboard for 5 minutes to prevent unnecessary requests and keep things snappy while a user is navigating around. And then one of those cached fragments needs to be invalidated because the user has added new data. The only way (that I know of) for a server side webapp to clear the browser cache is using Clear-Site-Data. But that's not viable until it's supported in Safari. And knowing Apple that could be years away... But if you implement caching in JS via local storage you can clear it whenever you want. Eg. when a form is submitted. |
Beta Was this translation helpful? Give feedback.
-
@binaryfire / @hrstoyanov You should check out this article on caching https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching |
Beta Was this translation helpful? Give feedback.
-
Something like an hx-cache with parameters that would allow the three scenarios mentioned by @hrstoyanov in the first post would be great. This could also open up possibilities for building web and mobile applications with an easily configurable offline mode. Personally, I'm also very interested. |
Beta Was this translation helpful? Give feedback.
-
A service worker is probably the best option for implementing any of this (and opens up lots of other possibilities). It will intercept all requests before going out to the network and returning to the dom. So you can store the responses in any of the browser storage apis - cache, local storage, session storage, indexeddb etc. I'd suggest indexeddb as it offers the most possibilities. There's plenty of libraries made for working with all of this - Google workbox, idb, dexie, rxdb, etc |
Beta Was this translation helpful? Give feedback.
-
I am looking for some easy element attributes to swap out chunks of the Dom with something from the local browser cache. Also , some more sophisticated scenarios:
1."Swap out the inner/outer html with chunk from local cache with key X, or fetch it from the server"
"Swap out the inner/outer html with cached chunk under key X but only it is older than Y milliseconds"
"Swap out ... From http request, but then also store the response in local cache"
Note:
This is different from server response cache mechanisms in http.
It is like an "htmx magic dust" sprinkled over the browsers cache api
Beta Was this translation helpful? Give feedback.
All reactions