1.0.2 • Published 2 years ago

edge-swr v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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)));
});
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago