Skip to content

Commit

Permalink
chore: stale while revalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Apr 4, 2024
1 parent 199f359 commit ec5b02a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ const template = html`
```

If you're using SWTL in a more SPA-like PWA app, you can also use the following strategies:

```js
import { networkFirst, networkOnly, cacheFirst, cacheOnly } from 'swtl';
import { networkFirst, networkOnly, cacheFirst, cacheOnly, staleWhileRevalidate } from 'swtl';

import { Router } from 'swtl';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swtl",
"version": "0.4.0",
"version": "0.4.1",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
15 changes: 14 additions & 1 deletion packages/core/strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ export const cacheFirst = (request) => caches.match(request).then(r => r || fetc
export const cacheOnly = (request) => caches.match(request);

/** @param {Request} request */
export const networkOnly = (request) => fetch(request);
export const networkOnly = (request) => fetch(request);

/** @param {Request} request */
export const staleWhileRevalidate = (request) =>
caches.open("swtl-cache").then((cache) =>
cache.match(request).then(
(response) =>
response ||
fetch(request).then((networkResponse) => {
cache.put(request, networkResponse.clone());
return networkResponse;
})
)
);

0 comments on commit ec5b02a

Please sign in to comment.