Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Support stale-while-revalidate #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

paulo-raca
Copy link

@paulo-raca paulo-raca commented Apr 19, 2023

stale-while-revalidate allows resources that are accessed often to be refreshed asynchronously.

This is implemented by a new freshness state that returns the cached response, but triggers an asynchronous request that gets stored in the cache.

Example

If we need to call an external service to generate short-lived user JWT tokens (e.g., valid for a minute)

Imagine this request is slow and needs to be cached so the app is more responsive. However we don't want the app to hang every minute when the JWT token expires, so it needs to be refreshed asynchronously.

Instead of returning cache-control: max-age=60, it can now return cache-control: max-age=30, stale-while-revalidate=30, so that JWT tokens for users making frequent requests are regenerated every ~30s.

(If the user stays away for over a minute the next request will be slow)

@gregjones
Copy link
Owner

Please create your own fork to add any new functionality, I'm not interested in making more changes here.

stale-while-revalidate allows resources that are accessed often to be refreshed asynchronously.

This is implemented by a new freshness state that returns the cached response, but triggers an asynchronous request that gets stored in the cache.

## Example

If we need to call an external service to generate short-lived user JWT tokens (e.g., valid for a minute)

Imagine this request is slow and needs to be cached so the app is more responsive. However I we don't want the app to hang every minute when the JWT token expires, so it needs to be refreshed asynchronously.

Instead of returning `cache-control: max-age=60`, it can now return `cache-control: max-age=30, stale-while-revalidate=30`, so that JWT tokens for users making frequent requests are regenerated every ~30s.

(If the user stays away for over a minute the next request will be slower)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants