edge-swr
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

edgeSWR

Stale while revalidate for web worker environment

installation

npm i edge-swr

usage example cloudflare worker:

import edgeSWR from "edge-swr";

const html = `<!DOCTYPE html>
<body>
    <h1>Hello World</h1>
</body>`;

async function handleRequest(request) {
  return new Response(html, {
    headers: {
      'content-type': 'text/html;charset=UTF-8',
    },
  });
}

async function withSWRHandler(
  event: FetchEvent,
  requestHandler: () => Promise<Response>
) {
  let cache = await caches.open('swr_cache_example');

  return edgeSWR({
    request(request) {
      return new Request(request.url, {method: request.method});
    },
    handler: requestHandler,
    match(cacheKey) {
      return cache.match(cacheKey);
    },
    put(cacheKey, content) {
      return cache.put(cacheKey, content);
    },
    waitUntil(promise) {
      event.waitUntil(promise);
    },
  });
}

addEventListener('fetch', event => {
  return event.respondWith(withSWRHandler(event, () => handleRequest(event.request)));
});

Package Sidebar

Install

npm i edge-swr

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

19 kB

Total Files

6

Last publish

Collaborators

  • derhub