-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Comments
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
What I could come up with is the hydrate option, which for the https://axios-cache-interceptor.js.org/config/request-specifics#cache-hydrate |
I was just waiting for #510 to release it, but I can release it now if you're in a hurry :) |
Hi 👋, Looking more at the The thing that will not work is the 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
We do not need it right now, if a release is planned in the next couple of days/weeks its ok for us. |
Then using only 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.
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
}
}
}); |
Servers can send stale-while-revalidate in Cache-Control.
This allows client to do the following simultaneously:
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?
The text was updated successfully, but these errors were encountered: