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

Support stale-while-revalidate #512

Closed
richardgarnier opened this issue Mar 2, 2023 · 4 comments · Fixed by #514
Closed

Support stale-while-revalidate #512

richardgarnier opened this issue Mar 2, 2023 · 4 comments · Fixed by #514
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@richardgarnier
Copy link
Contributor

Servers can send stale-while-revalidate in Cache-Control.
This allows client to do the following simultaneously:

  • return the response from cache
  • update the cache in background (but avoid returning it to the client)

For data that is not updated often, this allow to always be fast while also returning stale data at most once.
So typically, hitting the refresh button on the browser would get the correct data.

This is not supported (yet) by this library, but could it be considered?

@arthurfiorette
Copy link
Owner

Hi @richardgarnier, thanks for your idea! This was thought a while ago and discussed in some PRs but the problem is that by not doing some kind of "double return" to the client it seems useless to the stale-while-revalidate purpose.

update the cache in background (but avoid returning it to the client)

What I could come up with is the hydrate option, which for the stale-while-revalidate use case can be combined with override: true.

https://axios-cache-interceptor.js.org/config/request-specifics#cache-hydrate

@arthurfiorette
Copy link
Owner

I was just waiting for #510 to release it, but I can release it now if you're in a hurry :)

@Embraser01
Copy link

Hi 👋,
Thanks for the quick response.
I'm in the same Team as @richardgarnier.

Looking more at the hydrate it may do the job. The override field doesn't seems to be useful in our case as we only want to make call when maxAge is expired.
The idea would be to return the first between hydrate callback and the axios response. This will update the cache in the background correctly or, if cache isn't expired, will return directly the value from cache. Will try that (

The thing that will not work is the stale-while-revalidate=3600s which is currently ignored.

IIUC a content that can stale will be kept indefinitely? Is there a way to set a max stale time?

Would it be possible to add a check on the stale-while-revalidate value in the canStale method?

Looking at #510, it seems easy to add a check there.

We do not need it right now, if a release is planned in the next couple of days/weeks its ok for us.

@arthurfiorette
Copy link
Owner

as we only want to make call when maxAge is expired

Then using only hydrate should do the job.

I do not have enough time this month or so to code this (I'm moving to another state). That's the reason why I created #507 and #506 and let the community do a PR.

stale-while-revalidate feature can be simulated by a check in your hydrate option. Although builtin support should be added (up to a pr?).

  import { parse } from 'cache-parser';

  axios.get('url', {
    cache: {
      hydrate(cache) {
        const { staleWhileRevalidate } = parse(cache.data.headers['cache-control']);

        // previous response returned a staleWhileRevalidate header and it the cache is older than that.
        if (typeof staleWhileRevalidate === 'number' && staleWhileRevalidate < (cache.createdAt + cache.ttl)) {
          return;
        }
 
       render() // your code
      }
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants