Skip to content

`stale-while-revalidate` caching stratergy for vanilla web-apps

License

Notifications You must be signed in to change notification settings

Arcrueid/vanilla-swr

 
 

Repository files navigation

Vanilla SWR

stale-while-revalidate caching strategy for vanilla web-apps

GitHub Workflow Status Coverage Status

Installation

npm

npm install vanilla-swr

OR

yarn

yarn add vanilla-swr

OR

Import it from a CDN

jsDelivr

<script src="https://cdn.jsdelivr.net/npm/vanilla-swr/dist/index.min.js"></script>

unpkg

<script src="https://unpkg.com/vanilla-swr/dist/index.js"></script>

SWR variable is present in the window object.

Quick Start

import SWR from "vanilla-swr";

const fetcher = (...args) => fetch(...args).then((res) => res.json());

const observable = SWR("/api/data", fetcher);
// Use window.SWR("/api/data", fetcher) instead when using from the script tag

// Add a watcher to start fetching data
const watcher = observable.watch(({ data, error }) => {
  // data - data for given key resolved by fetcher
  // error - error thrown by fetcher
});

// Remove watcher when not needed
watcher.unwatch();

Documentation

https://vanilla-swr.js.org/

License

MIT

About

`stale-while-revalidate` caching stratergy for vanilla web-apps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 96.8%
  • JavaScript 2.0%
  • Shell 1.2%