swimple 0.1.0 → 0.2.0
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 +2 -1
- package/index.js +36 -3
- package/package.json +1 -1
- package/types.d.ts +1 -1
package/README.md
CHANGED
|
@@ -224,7 +224,7 @@ Creates a request handler function for your service worker fetch handler.
|
|
|
224
224
|
| Option | Type | Required | Default | Description |
|
|
225
225
|
| ------------------------ | ---------- | -------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
226
226
|
| `cacheName` | `string` | Yes | - | Name of the cache, used when calling `Cache.open(cacheName)` internally. Changing this name effectively clears the previous cache entries. |
|
|
227
|
-
| `scope` | `string[]` | No | `undefined` | URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header.
|
|
227
|
+
| `scope` | `string[]` | No | `undefined` | URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. **Note:** Cross-origin requests are never cached, regardless of scope or TTL headers. |
|
|
228
228
|
| `defaultStrategy` | `string` | No | `'cache-first'` | Default caching strategy: `'cache-first'`, `'network-first'`, or `'stale-while-revalidate'`. |
|
|
229
229
|
| `defaultTTLSeconds` | `number` | No | `300` | Maximum age for fresh content. Fresh content will be returned from cache for cache-first and stale-while-revalidate strategies, and also from network-first when offline. Fresh content does not get updated from the network. Since this defaults to `300`, caching is automatic by default for GET requests matching the scope. Set to `0` or `undefined` to disable automatic caching (individual requests can still enable caching with `X-SW-Cache-TTL-Seconds` header). |
|
|
230
230
|
| `defaultStaleTTLSeconds` | `number` | No | `3600` | Maximum age for stale content. Stale content will be returned from cache for cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies. That means responses past the fresh TTL but within stale TTL can still be returned from cache. Stale content does get updated from the network. |
|
|
@@ -647,6 +647,7 @@ self.addEventListener("fetch", (event) => {
|
|
|
647
647
|
|
|
648
648
|
- Only GET requests are cached
|
|
649
649
|
- Only 2xx (OK) GET responses are cached. Non-OK responses (4xx, 5xx, etc.) are not cached
|
|
650
|
+
- **Cross-origin requests are not cached** - Only requests to the same origin as the service worker are cached. Requests to different origins will return `null` and are not processed by the cache handler.
|
|
650
651
|
- Non-GET and non-mutating requests (POST/PATCH/PUT/DELETE) are not processed by the cache handler - it will return null. Practically, this means HEAD requests are not handled by the cache handler.
|
|
651
652
|
- Query strings are part of the cache key. Different query strings create different cache entries (e.g., `/api/users?page=1` and `/api/users?page=2` are separate cache entries)
|
|
652
653
|
- Cache invalidation happens automatically for mutations when `inferInvalidation: true`
|
package/index.js
CHANGED
|
@@ -28,9 +28,12 @@ import {
|
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Creates a request handler function for service worker fetch events.
|
|
31
|
+
* The handler implements HTTP caching with configurable strategies (cache-first, network-first, stale-while-revalidate),
|
|
32
|
+
* automatic cache invalidation for mutations, and periodic cache cleanup. Only handles same-origin GET requests
|
|
33
|
+
* that match the configured scope.
|
|
31
34
|
*
|
|
32
|
-
* @param {HandleRequestConfig} config - Configuration options
|
|
33
|
-
* @returns {(event: FetchEvent) => Promise<Response> | null} Request handler function
|
|
35
|
+
* @param {HandleRequestConfig} config - Configuration options including cache name, scope, default strategy, and TTL settings
|
|
36
|
+
* @returns {(event: FetchEvent) => Promise<Response> | null} Request handler function that can be used in service worker fetch event listeners
|
|
34
37
|
*/
|
|
35
38
|
export function createHandleRequest(config) {
|
|
36
39
|
validateConfig(config);
|
|
@@ -50,7 +53,14 @@ export function createHandleRequest(config) {
|
|
|
50
53
|
// Track fetch counter for periodic cleanup
|
|
51
54
|
let fetchCounter = 0;
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Service worker fetch event handler that implements HTTP caching strategies.
|
|
58
|
+
* Handles cache invalidation for mutations, implements cache-first/network-first/stale-while-revalidate
|
|
59
|
+
* strategies for GET requests, and performs automatic cache cleanup.
|
|
60
|
+
*
|
|
61
|
+
* @param {FetchEvent} event - The fetch event from the service worker
|
|
62
|
+
* @returns {Promise<Response> | null} The cached or fetched response, or null if request shouldn't be handled
|
|
63
|
+
*/
|
|
54
64
|
return function handleRequest(event) {
|
|
55
65
|
const request = event.request;
|
|
56
66
|
const url = request.url;
|
|
@@ -93,6 +103,29 @@ export function createHandleRequest(config) {
|
|
|
93
103
|
return null;
|
|
94
104
|
}
|
|
95
105
|
|
|
106
|
+
// Only cache same-origin requests - cross-origin requests are not cached
|
|
107
|
+
// In service worker context, self.location.origin is the service worker's origin
|
|
108
|
+
try {
|
|
109
|
+
const requestUrl = new URL(url);
|
|
110
|
+
// Check if we're in a service worker context and can determine the origin
|
|
111
|
+
if (
|
|
112
|
+
typeof self !== "undefined" &&
|
|
113
|
+
self.location &&
|
|
114
|
+
self.location.origin
|
|
115
|
+
) {
|
|
116
|
+
const serviceWorkerOrigin = self.location.origin;
|
|
117
|
+
// If request origin doesn't match service worker origin, don't cache
|
|
118
|
+
if (requestUrl.origin !== serviceWorkerOrigin) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// In test environments where self.location might not be available,
|
|
123
|
+
// we rely on the test setup to ensure proper origin handling
|
|
124
|
+
} catch (error) {
|
|
125
|
+
// If URL parsing fails, don't cache
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
96
129
|
// Periodic cleanup: run on first fetch and every 100 fetches
|
|
97
130
|
fetchCounter++;
|
|
98
131
|
if (fetchCounter === 1 || fetchCounter % 100 === 0) {
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type CacheStrategy =
|
|
|
12
12
|
export interface HandleRequestConfig {
|
|
13
13
|
/** Name of the cache, used when calling `Cache.open(cacheName)` internally. Changing this name effectively clears the previous cache entries. */
|
|
14
14
|
cacheName: string;
|
|
15
|
-
/** URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. */
|
|
15
|
+
/** URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. Note: Cross-origin requests are never cached, regardless of scope or TTL headers. */
|
|
16
16
|
scope?: string[];
|
|
17
17
|
/** Default caching strategy: `'cache-first'`, `'network-first'`, or `'stale-while-revalidate'`. */
|
|
18
18
|
defaultStrategy?: CacheStrategy;
|