bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.59k stars 1.31k forks source link

Proposal(?): adding stale-while-revalidate caching pattern? #1443

Open frederikhors opened 1 year ago

frederikhors commented 1 year ago

I just found out htmx and I would like to try it as soon as possible.

I also agree that SPAs are a real fatigue.

I'm using SPAs mainly for two features:

  1. I'm obsessed with perceived speed of rendering between pages transition. Especially with list > details workflows I'm using the stale-while-revalidate pattern a lot to increase the perceived speed of data fetching (using graphql normalized cache packages like Apollo and urql).

  2. I'm really productive with code like frontend calculation only, example: the sum of invoices rows; I mean, list of something to calculate on frontend only without backend calls at all.

I searched a lot on Google and here on GitHub about htmx and stale-while-revalidate but nothing showed up.

I was thinking about using service worker for this but found nothing ready.

Can you help me find a way?

What do you think?

UPDATE:

I better explained what I need here: https://github.com/bigskysoftware/htmx/discussions/1706#discussioncomment-6807043.

thomaseizinger commented 1 year ago

There is the preload extension which could help with the list > details workflows that you mentioned: https://htmx.org/extensions/preload/

thomaseizinger commented 1 year ago

2. I'm really productive with code like frontend calculation only, example: the sum of invoices rows; I mean, list of something to calculate on frontend only without backend calls at all.

I'd say the way to think about this is that the webserver serving your HTML is your frontend. The sum of invoice rows is not going to change without an invoice being deleted or added which needs a server-roundtrip anyway. That roundtrip sends you back the new HTML to render.

In a way, you need 0 backend calls and avoid writing an API to start with because your server can just render HTML based on what is in its database.

danielkeller commented 1 year ago

I had a similar idea of an "hx-local" or "hx-template" attribute: it acts like hx-get, except instead of a url it takes an element reference, and the "ajax response" instead comes from the innerHTML of the element. This would work well with