Stale While Revalidated Resources — keeps data fast

Overview

swrr

Stale While Revalidate Resource

npm add swrr makes resources fast

downloads size size

Caveat ~ Currently cache backplane is assumed to be Cloudflare Workers KV. Before we hit 1.0 release, we will aim to support a generic and abstract backplane for the likes of Redis, Memcached, or other layers.

🚀 Usage

// file: my-worker.esm.ts

import * as swr from 'swrr';

const getPostsInCategorySince = async ({ category, since }) => {
  const posts = await cms.getPostsInCategory(category);

  return posts.filter((post) => post.updatedAt > since);
};

export default {
  async fetch(req, env, ctx) {
    // ⬇️️ create "container", all resources will batch in this boundary.
    //   ~> you'd probably want this in a middleware.
    const makeResource = swr.make(env.KV_NAMESPACE, ctx);

    // ⬇️ create a resource connected to a handler and name it
    const getLatestPosts = makeResource('posts', getPostsInCategorySince);

    // ... whatever elese

    // ⬇️ run the resource, passing in whatever arguments you'd like
    const posts = await getLatestPosts({ category: 'foobar', since: '2022-01-01' });

    // ~> and one would now find a KV entry for
    //      'posts__cdbdf4617568dc29453d3fee5f5ca79a7713b15f'

    return new Response(posts, { headers: { 'content-type': 'application/json' } });
  },
};

License

MIT © Marais Rossouw

You might also like...

Minty is an example of how to mint non-fungible tokens (NFTs) while storing the associated data on IPFS

Minty is an example of how to mint non-fungible tokens (NFTs) while storing the associated data on IPFS. You can also use Minty to pin your data on an IPFS pinning service such as nft.storage and Pinata.

Nov 12, 2022

This application displays scores submitted by different players from an API service. It also allows a user to submit his/her score while saving the data on the API.

Leaderboard This application displays scores submitted by different players from an API service. It also allows a user to submit his/her score while p

Jul 15, 2022

It redirects the website request from facebook to any blog while keeping the meta data for the each link.

Vercel Redirect It redirects the website request from facebook to any blog while keeping the meta data for the each link. This app uses Next.js and th

Dec 4, 2022

Generally free coding Resources for all! Check it out and don't forget to give it a star ⭐️

Generally free coding Resources for all! Check it out and don't forget to give it a star ⭐️

A-Z Coding Resources This website is built using Docusaurus 2, a modern static website generator. Installation yarn install Local Development yarn sta

Jan 2, 2023

Github Repository for the resources shown in my 8 JavaScript Pro Tips Tutorial in Youtube

Github Repository for the resources shown in my 8 JavaScript Pro Tips Tutorial in Youtube

Part 1 - JavaScript Pro Tips - Learn with Sumit Table of Contents How to run Contact How to run Different lessons taught in the Youtube Tutorial are o

Dec 28, 2022

An ongoing curated list of frameworks, books, articles, talks, screencasts, recordings, libraries, learning tutorials and shiny resources about Javascript Development.

An ongoing curated list of frameworks, books, articles, talks, screencasts, recordings, libraries, learning tutorials and shiny resources about Javascript Development.

Javascript Frameworks Development Welcome to the world of Javascript Frameworks. An ongoing curated list of frameworks, books, articles, talks, screen

Jul 31, 2022

This project uses JavaScript to request resources from PokeApi and Involvement API to display a stunning website about Pokemons.

This project uses JavaScript to request resources from PokeApi and Involvement API to display a stunning website about Pokemons.

POKEDEX CAPSTONE Web page used to retrieve information about Pokemons using Pokeapi v2 This project implements the involvement API and the Pokeapi to

Jun 13, 2022

Manually curated collection of resources for frontend web developers.

Frontend Development Manually curated collection of resources for frontend web developers. You are viewing a browseable version, split by category in

Jan 1, 2023

🐢 A collection of awesome browser-side JavaScript libraries, resources and shiny things.

Awesome JavaScript A collection of awesome browser-side JavaScript libraries, resources and shiny things. Awesome JavaScript Package Managers Componen

Dec 29, 2022
Owner
Marais Rossouw
Marais Rossouw
Incredible resources (with links) to help up-skill yourselves on various fields. Resources like programming, designing, engineering and much more and completely Open Source.

Shiryoku Incredible resources (with links) to help up-skill yourselves on various fields. Resources like programming, designing, engineering and much

Kunal Keshan 22 Dec 15, 2022
Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormation.

CDK Github Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormatio

Pepperize 8 Nov 25, 2022
Website that keeps monitoring status of WAX account and TLM balance from alienworlds

✔️ Server is now live! [21-5-2021 10:30 GMT+7] ?? Updates on Server ‼️ Bandwidth limit reaches on morning of 21 May 2021 (+7) I want to thank you to e

Patiphol Pussawong 21 Nov 24, 2022
A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

A free and open source resume builder. Go to App What is this app all about? Reactive Resume is a free and open source resume builder that’s built to

Reactive Resume 9.7k Jan 3, 2023
Remarkable is a simple extension that automatically keeps your bookmarks clean & up-to-date.

Remarkable Remarkable is a simple extension that automatically keeps your bookmarks clean & up-to-date. Installation (Other browsers coming soon - sor

Samuel Dobbie 116 Dec 21, 2022
SafeCycle—a tool that keeps cyclists safe. Gone are days of weaving through busy city streets, SafeCycle finds all the bike routes for you to ensure a smooth ride wherever you want to go.

Inspiration Biking—an everyday form of travel for students and professionals across the globe. On-campus, back home, and with the people that we know

Ryan Hu 2 May 2, 2022
An enchanced useState hook which keeps track of the states history, allowing you to undo and redo states.

useTimeline An enchanced useState hook which keeps track of the states history, allowing you to undo and redo states. useTimeline is a simple hook bas

null 13 Apr 22, 2022
Simple news reader that keeps the noise low.

Thud. Read news without the fuss · Report Bug · Request Feature Table of Contents About The Project Built With Getting Started Prerequisites Contribut

Samuel Bazaga 137 Jan 1, 2023
This is a single page web application that keeps tracks of books. Book details captured are the book title, author and ISBN. User can add a book, view a list of books and also remove any un wanted books.

Project Name This is a single page web application that keeps tracks of books. Book details captured are the book title, author and ISBN. User can add

Olivier 6 Nov 20, 2022
This is a simple app that keeps track of count. It also has a reset button that takes the count back to zero.

Counter This is a simple app that keeps track of count. It also has a reset button that takes the count back to zero. Built With HTML SCSS CSS Javascr

Abubakar Ummar 5 Oct 17, 2022