
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
workers-swr
Advanced tools
Utility to add `stale-while-revalidate` and `stale-if-error` (see [rfc5861](https://httpwg.org/specs/rfc5861.html)) handling to [Cloudflare workers](https://workers.cloudflare.com/) via the [Workers Cache API](https://developers.cloudflare.com/workers/run
Utility to add stale-while-revalidate
and stale-if-error
(see rfc5861) handling to Cloudflare workers via the
Workers Cache API.
The library is very minimalistic and requires almost no code changes, just install the library via:
npm i workers-swr
Then import and wrap your fetch handler with the provided withSWR
function:
import { withSWR } from "workers-swr";
export default {
fetch: withSWR(
// your standard fetch handler goes here
(request, env, ctx) => {
return new Response("Hello World!");
}
),
};
The library will check incoming requests and outgoing responses and implement the swr caching for you.
Warning Note that the workers cache API, which this library uses, is not available in
*.workers.dev
deployments, in order to make use of it you need to deploy your worker to a custom domain.
Note Cloudflare adds a default 4-hour max-age caching to fetch responses, this means that this library by default will make your responses be cached for 4 hours, you can easily change this by changing accordingly the Browser Cache TTL option in the Cloudflare dashboard.
Demo on how this library works (source code): https://workers-swr-demo.dariopiot.net/
Note >
no-cache
,no-store
andmax-age
are already handled by the workers cache api
max-stale
min-fresh
only-if-cached
[] adding a config parameter can be passed to withSWR
to enable the must-understand
directive
[] adding the possibility to use a different caching storage (e.g. using KVs instead of the cache api, allowing caching also on workers.dev
subdomains) (important note: this would also need to make sure we handle all the cache control directives that now the workers api handles for us)
FAQs
Utility to add `stale-while-revalidate` and `stale-if-error` (see [rfc5861](https://httpwg.org/specs/rfc5861.html)) handling to [Cloudflare workers](https://workers.cloudflare.com/) via the [Workers Cache API](https://developers.cloudflare.com/workers/run
The npm package workers-swr receives a total of 0 weekly downloads. As such, workers-swr popularity was classified as not popular.
We found that workers-swr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.