swimple 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -2
- package/example/index.html +51 -4
- package/example/logo.png +0 -0
- package/example/logo.svg +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -712,7 +712,57 @@ self.addEventListener("fetch", (event) => {
|
|
|
712
712
|
});
|
|
713
713
|
```
|
|
714
714
|
|
|
715
|
-
### Example 5:
|
|
715
|
+
### Example 5: Different Strategies for Different Scopes
|
|
716
|
+
|
|
717
|
+
You can compose multiple `handleRequest` methods to use different caching strategies for different scopes. For example, you might want `stale-while-revalidate` for static assets like fonts, and `cache-first` for API endpoints.
|
|
718
|
+
|
|
719
|
+
```javascript
|
|
720
|
+
// sw.js
|
|
721
|
+
import { createHandleRequest } from "swimple";
|
|
722
|
+
|
|
723
|
+
// Handler for fonts - use stale-while-revalidate for instant loading
|
|
724
|
+
const handleFontsRequest = createHandleRequest({
|
|
725
|
+
cacheName: "fonts-cache-v1",
|
|
726
|
+
scope: ["/fonts/"],
|
|
727
|
+
defaultStrategy: "stale-while-revalidate",
|
|
728
|
+
defaultTTLSeconds: 86400, // Fresh for 24 hours
|
|
729
|
+
defaultStaleTTLSeconds: 604800 // Stale for up to 7 days
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
// Handler for API - use cache-first for fast responses
|
|
733
|
+
const handleApiRequest = createHandleRequest({
|
|
734
|
+
cacheName: "api-cache-v1",
|
|
735
|
+
scope: ["/api/"],
|
|
736
|
+
defaultStrategy: "cache-first",
|
|
737
|
+
defaultTTLSeconds: 300 // Fresh for 5 minutes
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
self.addEventListener("fetch", (event) => {
|
|
741
|
+
// Try fonts handler first
|
|
742
|
+
let response = handleFontsRequest(event);
|
|
743
|
+
if (response) {
|
|
744
|
+
event.respondWith(response);
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Then try API handler
|
|
749
|
+
response = handleApiRequest(event);
|
|
750
|
+
if (response) {
|
|
751
|
+
event.respondWith(response);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
// Fall through to network for other requests
|
|
756
|
+
});
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
With this setup:
|
|
760
|
+
|
|
761
|
+
- Requests to `/fonts/*` will use `stale-while-revalidate` - returning cached fonts immediately while updating in the background
|
|
762
|
+
- Requests to `/api/*` will use `cache-first` - returning from cache if fresh, otherwise fetching from network
|
|
763
|
+
- Each scope uses its own cache (`fonts-cache-v1` and `api-cache-v1`), so they can be managed independently
|
|
764
|
+
|
|
765
|
+
### Example 6: Logout with Cache Clear
|
|
716
766
|
|
|
717
767
|
```javascript
|
|
718
768
|
// Client code
|
|
@@ -728,7 +778,7 @@ async function logout() {
|
|
|
728
778
|
}
|
|
729
779
|
```
|
|
730
780
|
|
|
731
|
-
### Example
|
|
781
|
+
### Example 7: Custom Request Handler Chain
|
|
732
782
|
|
|
733
783
|
Your service worker might have other "handlers" or "middlewares" that need to be called before the cache handler.
|
|
734
784
|
|
package/example/index.html
CHANGED
|
@@ -4,6 +4,38 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>swimple - Simple Service Worker Caching</title>
|
|
7
|
+
<link rel="icon" type="image/png" href="/logo.png" />
|
|
8
|
+
|
|
9
|
+
<!-- Open Graph / Facebook -->
|
|
10
|
+
<meta property="og:type" content="website" />
|
|
11
|
+
<meta
|
|
12
|
+
property="og:title"
|
|
13
|
+
content="swimple - Simple Service Worker Caching"
|
|
14
|
+
/>
|
|
15
|
+
<meta
|
|
16
|
+
property="og:description"
|
|
17
|
+
content="A simple service worker library for request caching with automatic invalidation."
|
|
18
|
+
/>
|
|
19
|
+
<meta property="og:image" content="/logo.png" />
|
|
20
|
+
<meta property="og:site_name" content="swimple" />
|
|
21
|
+
|
|
22
|
+
<!-- Twitter -->
|
|
23
|
+
<meta name="twitter:card" content="summary" />
|
|
24
|
+
<meta
|
|
25
|
+
name="twitter:title"
|
|
26
|
+
content="swimple - Simple Service Worker Caching"
|
|
27
|
+
/>
|
|
28
|
+
<meta
|
|
29
|
+
name="twitter:description"
|
|
30
|
+
content="A simple service worker library for request caching with automatic invalidation."
|
|
31
|
+
/>
|
|
32
|
+
<meta name="twitter:image" content="/logo.png" />
|
|
33
|
+
|
|
34
|
+
<!-- General meta -->
|
|
35
|
+
<meta
|
|
36
|
+
name="description"
|
|
37
|
+
content="A simple service worker library for request caching with automatic invalidation."
|
|
38
|
+
/>
|
|
7
39
|
<style>
|
|
8
40
|
* {
|
|
9
41
|
margin: 0;
|
|
@@ -42,6 +74,18 @@
|
|
|
42
74
|
gap: 1rem;
|
|
43
75
|
}
|
|
44
76
|
|
|
77
|
+
.header-logo {
|
|
78
|
+
display: flex;
|
|
79
|
+
align-items: center;
|
|
80
|
+
gap: 1rem;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.header-logo img {
|
|
84
|
+
width: 100px;
|
|
85
|
+
height: 100px;
|
|
86
|
+
flex-shrink: 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
45
89
|
header h1 {
|
|
46
90
|
font-size: 2.5rem;
|
|
47
91
|
color: #2563eb;
|
|
@@ -465,9 +509,12 @@
|
|
|
465
509
|
<header>
|
|
466
510
|
<div class="container">
|
|
467
511
|
<div class="header-content">
|
|
468
|
-
<div>
|
|
469
|
-
<
|
|
470
|
-
<
|
|
512
|
+
<div class="header-logo">
|
|
513
|
+
<img src="/logo.svg" alt="swimple logo" />
|
|
514
|
+
<div>
|
|
515
|
+
<h1>swimple</h1>
|
|
516
|
+
<p>A simple service worker library for request caching</p>
|
|
517
|
+
</div>
|
|
471
518
|
</div>
|
|
472
519
|
<a
|
|
473
520
|
href="https://github.com/wes-goulet/swimple"
|
|
@@ -496,7 +543,7 @@
|
|
|
496
543
|
<h2>Simple, Automatic Request Caching</h2>
|
|
497
544
|
<p>
|
|
498
545
|
swimple provides automatic caching with smart invalidation for your
|
|
499
|
-
service worker.
|
|
546
|
+
service worker.
|
|
500
547
|
</p>
|
|
501
548
|
</section>
|
|
502
549
|
|
package/example/logo.png
ADDED
|
Binary file
|
package/example/logo.svg
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<!-- Hexagon (like service worker icon shape) -->
|
|
3
|
+
<path d="M 100 17.5 L 175 58.75 L 175 141.25 L 100 182.5 L 25 141.25 L 25 58.75 Z"
|
|
4
|
+
fill="#3b82f6"
|
|
5
|
+
stroke="#2563eb"
|
|
6
|
+
stroke-width="3"/>
|
|
7
|
+
|
|
8
|
+
<!-- "swimple" text inside hexagon -->
|
|
9
|
+
<text x="100" y="100"
|
|
10
|
+
font-family="system-ui, -apple-system, sans-serif"
|
|
11
|
+
font-size="22"
|
|
12
|
+
font-weight="700"
|
|
13
|
+
fill="white"
|
|
14
|
+
text-anchor="middle"
|
|
15
|
+
dominant-baseline="middle">
|
|
16
|
+
swimple
|
|
17
|
+
</text>
|
|
18
|
+
</svg>
|