@baukit/pwa-web 0.18.2 → 0.19.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 +6 -3
- package/dist/cache-strategy.d.ts +5 -0
- package/dist/cache-strategy.d.ts.map +1 -1
- package/dist/cache-strategy.js +16 -5
- package/dist/cache-strategy.js.map +1 -1
- package/dist/cjs/cache-strategy.js +17 -6
- package/dist/cjs/index.js +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/worker.js +13 -3
- package/package.json +3 -3
- package/src/cache-strategy.test.ts +29 -0
- package/src/cache-strategy.ts +35 -9
- package/src/fetch-handler.test.ts +19 -0
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -23,9 +23,12 @@ decideCacheStrategy(
|
|
|
23
23
|
|
|
24
24
|
The rules run in this order:
|
|
25
25
|
|
|
26
|
-
1. A path under
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
1. A path under `/auth/session/` (`ALWAYS_NETWORK_ONLY_PATH_PREFIXES`, on any origin), a request
|
|
27
|
+
with an `Authorization` header (pass `headers`, as a service worker `Request` does), and a path
|
|
28
|
+
under any `neverCachedPathPrefixes` entry are `network-only`. Options cannot override the first
|
|
29
|
+
two: session responses and bearer-authorized responses never reach the Cache API. The
|
|
30
|
+
configurable list is empty by default. A sync endpoint belongs there: replaying a stale delta
|
|
31
|
+
corrupts the local database.
|
|
29
32
|
2. Any non-GET method is `network-only`.
|
|
30
33
|
3. A path under any `apiPathPrefixes` entry (default `['/api']`) gets `apiStrategy`, default
|
|
31
34
|
`network-first`.
|
package/dist/cache-strategy.d.ts
CHANGED
|
@@ -10,6 +10,9 @@ export interface CacheStrategyRequest {
|
|
|
10
10
|
readonly method?: string;
|
|
11
11
|
readonly mode?: string;
|
|
12
12
|
readonly destination?: string;
|
|
13
|
+
readonly headers?: {
|
|
14
|
+
has(name: string): boolean;
|
|
15
|
+
};
|
|
13
16
|
}
|
|
14
17
|
export interface CacheStrategyOptions {
|
|
15
18
|
readonly appOrigin: string;
|
|
@@ -27,6 +30,8 @@ export interface CacheStrategyDecision {
|
|
|
27
30
|
readonly strategy: CacheStrategy;
|
|
28
31
|
readonly navigationFallback?: string;
|
|
29
32
|
}
|
|
33
|
+
/** Browser session routes: never cached, whatever the options say. */
|
|
34
|
+
export declare const ALWAYS_NETWORK_ONLY_PATH_PREFIXES: readonly string[];
|
|
30
35
|
export declare function decideCacheStrategyDecision(request: CacheStrategyRequest, options: CacheStrategyOptions): CacheStrategyDecision;
|
|
31
36
|
export declare function decideCacheStrategy(request: CacheStrategyRequest, options: CacheStrategyOptions): CacheStrategy;
|
|
32
37
|
export declare function createCacheStrategyDecider(options: CacheStrategyOptions): (request: CacheStrategyRequest) => CacheStrategy;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-strategy.d.ts","sourceRoot":"","sources":["../src/cache-strategy.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB;;;;;CAKnB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO,gBAAgB,CAAC,CAAC;AAErF,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"cache-strategy.d.ts","sourceRoot":"","sources":["../src/cache-strategy.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB;;;;;CAKnB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO,gBAAgB,CAAC,CAAC;AAErF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B,mBAAmB,CACjC,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,oBAAoB,GAC5B,aAAa,CAEf;AAED,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,oBAAoB,GAC5B,CAAC,OAAO,EAAE,oBAAoB,KAAK,aAAa,CAElD"}
|
package/dist/cache-strategy.js
CHANGED
|
@@ -4,6 +4,8 @@ export const CACHE_STRATEGIES = {
|
|
|
4
4
|
networkOnly: 'network-only',
|
|
5
5
|
staleWhileRevalidate: 'stale-while-revalidate',
|
|
6
6
|
};
|
|
7
|
+
/** Browser session routes: never cached, whatever the options say. */
|
|
8
|
+
export const ALWAYS_NETWORK_ONLY_PATH_PREFIXES = ['/auth/session/'];
|
|
7
9
|
const DEFAULT_API_PATH_PREFIXES = ['/api'];
|
|
8
10
|
const DEFAULT_STATIC_DESTINATIONS = [
|
|
9
11
|
'audio',
|
|
@@ -58,12 +60,23 @@ function hasStaticExtension(pathname, extensions) {
|
|
|
58
60
|
function startsWithAny(pathname, prefixes) {
|
|
59
61
|
return prefixes.some((prefix) => prefix.length > 0 && pathname.startsWith(prefix));
|
|
60
62
|
}
|
|
63
|
+
function isStaticAsset(request, pathname, options) {
|
|
64
|
+
const { staticPathPrefixes = [], staticDestinations = DEFAULT_STATIC_DESTINATIONS, staticExtensions = DEFAULT_STATIC_EXTENSIONS, } = options;
|
|
65
|
+
return ((request.destination !== undefined && staticDestinations.includes(request.destination)) ||
|
|
66
|
+
hasStaticExtension(pathname, staticExtensions) ||
|
|
67
|
+
startsWithAny(pathname, staticPathPrefixes));
|
|
68
|
+
}
|
|
69
|
+
function isNeverCached(request, pathname, neverCachedPathPrefixes) {
|
|
70
|
+
return (matchesAnyPathPrefix(pathname, ALWAYS_NETWORK_ONLY_PATH_PREFIXES) ||
|
|
71
|
+
matchesAnyPathPrefix(pathname, neverCachedPathPrefixes) ||
|
|
72
|
+
request.headers?.has('authorization') === true);
|
|
73
|
+
}
|
|
61
74
|
export function decideCacheStrategyDecision(request, options) {
|
|
62
|
-
const { appOrigin, apiPathPrefixes = DEFAULT_API_PATH_PREFIXES, navigationFallback, neverCachedPathPrefixes = [],
|
|
75
|
+
const { appOrigin, apiPathPrefixes = DEFAULT_API_PATH_PREFIXES, navigationFallback, neverCachedPathPrefixes = [], apiStrategy = CACHE_STRATEGIES.networkFirst, staticStrategy = CACHE_STRATEGIES.cacheFirst, defaultStrategy = CACHE_STRATEGIES.networkFirst, } = options;
|
|
63
76
|
const parsed = new URL(request.url, appOrigin);
|
|
64
77
|
const { pathname } = parsed;
|
|
65
78
|
const sameOrigin = parsed.origin === appOrigin;
|
|
66
|
-
if (
|
|
79
|
+
if (isNeverCached(request, pathname, neverCachedPathPrefixes)) {
|
|
67
80
|
return { strategy: CACHE_STRATEGIES.networkOnly };
|
|
68
81
|
}
|
|
69
82
|
const method = (request.method ?? 'GET').toUpperCase();
|
|
@@ -79,9 +92,7 @@ export function decideCacheStrategyDecision(request, options) {
|
|
|
79
92
|
}
|
|
80
93
|
return { strategy: defaultStrategy };
|
|
81
94
|
}
|
|
82
|
-
const staticAsset = (request
|
|
83
|
-
hasStaticExtension(pathname, staticExtensions) ||
|
|
84
|
-
startsWithAny(pathname, staticPathPrefixes);
|
|
95
|
+
const staticAsset = isStaticAsset(request, pathname, options);
|
|
85
96
|
return { strategy: sameOrigin && staticAsset ? staticStrategy : defaultStrategy };
|
|
86
97
|
}
|
|
87
98
|
export function decideCacheStrategy(request, options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-strategy.js","sourceRoot":"","sources":["../src/cache-strategy.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,UAAU,EAAE,aAAa;IACzB,YAAY,EAAE,eAAe;IAC7B,WAAW,EAAE,cAAc;IAC3B,oBAAoB,EAAE,wBAAwB;CACtC,CAAC;
|
|
1
|
+
{"version":3,"file":"cache-strategy.js","sourceRoot":"","sources":["../src/cache-strategy.ts"],"names":[],"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EAAE,QAAQ,EAAE,eAAe,EAAE,kBAAkB,EAAE,CAAC;QAC3D,CAAC;QACD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC;IACvC,CAAC;IAED,MAAM,WAAW,GAAG,aAAa,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC9D,OAAO,EAAE,QAAQ,EAAE,UAAU,IAAI,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC;AACpF,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,OAA6B,EAC7B,OAA6B;IAE7B,OAAO,2BAA2B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC;AAChE,CAAC;AAED,MAAM,UAAU,0BAA0B,CACxC,OAA6B;IAE7B,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CACHE_STRATEGIES = void 0;
|
|
3
|
+
exports.ALWAYS_NETWORK_ONLY_PATH_PREFIXES = exports.CACHE_STRATEGIES = void 0;
|
|
4
4
|
exports.decideCacheStrategyDecision = decideCacheStrategyDecision;
|
|
5
5
|
exports.decideCacheStrategy = decideCacheStrategy;
|
|
6
6
|
exports.createCacheStrategyDecider = createCacheStrategyDecider;
|
|
@@ -10,6 +10,8 @@ exports.CACHE_STRATEGIES = {
|
|
|
10
10
|
networkOnly: 'network-only',
|
|
11
11
|
staleWhileRevalidate: 'stale-while-revalidate',
|
|
12
12
|
};
|
|
13
|
+
/** Browser session routes: never cached, whatever the options say. */
|
|
14
|
+
exports.ALWAYS_NETWORK_ONLY_PATH_PREFIXES = ['/auth/session/'];
|
|
13
15
|
const DEFAULT_API_PATH_PREFIXES = ['/api'];
|
|
14
16
|
const DEFAULT_STATIC_DESTINATIONS = [
|
|
15
17
|
'audio',
|
|
@@ -64,12 +66,23 @@ function hasStaticExtension(pathname, extensions) {
|
|
|
64
66
|
function startsWithAny(pathname, prefixes) {
|
|
65
67
|
return prefixes.some((prefix) => prefix.length > 0 && pathname.startsWith(prefix));
|
|
66
68
|
}
|
|
69
|
+
function isStaticAsset(request, pathname, options) {
|
|
70
|
+
const { staticPathPrefixes = [], staticDestinations = DEFAULT_STATIC_DESTINATIONS, staticExtensions = DEFAULT_STATIC_EXTENSIONS, } = options;
|
|
71
|
+
return ((request.destination !== undefined && staticDestinations.includes(request.destination)) ||
|
|
72
|
+
hasStaticExtension(pathname, staticExtensions) ||
|
|
73
|
+
startsWithAny(pathname, staticPathPrefixes));
|
|
74
|
+
}
|
|
75
|
+
function isNeverCached(request, pathname, neverCachedPathPrefixes) {
|
|
76
|
+
return (matchesAnyPathPrefix(pathname, exports.ALWAYS_NETWORK_ONLY_PATH_PREFIXES) ||
|
|
77
|
+
matchesAnyPathPrefix(pathname, neverCachedPathPrefixes) ||
|
|
78
|
+
request.headers?.has('authorization') === true);
|
|
79
|
+
}
|
|
67
80
|
function decideCacheStrategyDecision(request, options) {
|
|
68
|
-
const { appOrigin, apiPathPrefixes = DEFAULT_API_PATH_PREFIXES, navigationFallback, neverCachedPathPrefixes = [],
|
|
81
|
+
const { appOrigin, apiPathPrefixes = DEFAULT_API_PATH_PREFIXES, navigationFallback, neverCachedPathPrefixes = [], apiStrategy = exports.CACHE_STRATEGIES.networkFirst, staticStrategy = exports.CACHE_STRATEGIES.cacheFirst, defaultStrategy = exports.CACHE_STRATEGIES.networkFirst, } = options;
|
|
69
82
|
const parsed = new URL(request.url, appOrigin);
|
|
70
83
|
const { pathname } = parsed;
|
|
71
84
|
const sameOrigin = parsed.origin === appOrigin;
|
|
72
|
-
if (
|
|
85
|
+
if (isNeverCached(request, pathname, neverCachedPathPrefixes)) {
|
|
73
86
|
return { strategy: exports.CACHE_STRATEGIES.networkOnly };
|
|
74
87
|
}
|
|
75
88
|
const method = (request.method ?? 'GET').toUpperCase();
|
|
@@ -85,9 +98,7 @@ function decideCacheStrategyDecision(request, options) {
|
|
|
85
98
|
}
|
|
86
99
|
return { strategy: defaultStrategy };
|
|
87
100
|
}
|
|
88
|
-
const staticAsset = (request
|
|
89
|
-
hasStaticExtension(pathname, staticExtensions) ||
|
|
90
|
-
startsWithAny(pathname, staticPathPrefixes);
|
|
101
|
+
const staticAsset = isStaticAsset(request, pathname, options);
|
|
91
102
|
return { strategy: sameOrigin && staticAsset ? staticStrategy : defaultStrategy };
|
|
92
103
|
}
|
|
93
104
|
function decideCacheStrategy(request, options) {
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createFetchHandler = exports.decideCacheStrategy = exports.createCacheStrategyDecider = exports.CACHE_STRATEGIES = exports.cleanupCaches = void 0;
|
|
3
|
+
exports.createFetchHandler = exports.decideCacheStrategy = exports.createCacheStrategyDecider = exports.CACHE_STRATEGIES = exports.ALWAYS_NETWORK_ONLY_PATH_PREFIXES = exports.cleanupCaches = void 0;
|
|
4
4
|
var cache_cleanup_js_1 = require("./cache-cleanup.js");
|
|
5
5
|
Object.defineProperty(exports, "cleanupCaches", { enumerable: true, get: function () { return cache_cleanup_js_1.cleanupCaches; } });
|
|
6
6
|
var cache_strategy_js_1 = require("./cache-strategy.js");
|
|
7
|
+
Object.defineProperty(exports, "ALWAYS_NETWORK_ONLY_PATH_PREFIXES", { enumerable: true, get: function () { return cache_strategy_js_1.ALWAYS_NETWORK_ONLY_PATH_PREFIXES; } });
|
|
7
8
|
Object.defineProperty(exports, "CACHE_STRATEGIES", { enumerable: true, get: function () { return cache_strategy_js_1.CACHE_STRATEGIES; } });
|
|
8
9
|
Object.defineProperty(exports, "createCacheStrategyDecider", { enumerable: true, get: function () { return cache_strategy_js_1.createCacheStrategyDecider; } });
|
|
9
10
|
Object.defineProperty(exports, "decideCacheStrategy", { enumerable: true, get: function () { return cache_strategy_js_1.decideCacheStrategy; } });
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { cleanupCaches, type CacheCleanupOptions, type CacheCleanupPorts, type CacheCleanupResult, } from './cache-cleanup.js';
|
|
2
|
-
export { CACHE_STRATEGIES, createCacheStrategyDecider, decideCacheStrategy, type CacheStrategy, type CacheStrategyOptions, type CacheStrategyRequest, } from './cache-strategy.js';
|
|
2
|
+
export { ALWAYS_NETWORK_ONLY_PATH_PREFIXES, CACHE_STRATEGIES, createCacheStrategyDecider, decideCacheStrategy, type CacheStrategy, type CacheStrategyOptions, type CacheStrategyRequest, } from './cache-strategy.js';
|
|
3
3
|
export { createFetchHandler, type FetchHandlerOptions, type FetchHandlerPorts, type FetchLikeRequest, } from './fetch-handler.js';
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,gBAAgB,EAChB,0BAA0B,EAC1B,mBAAmB,EACnB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,GAC1B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,iCAAiC,EACjC,gBAAgB,EAChB,0BAA0B,EAC1B,mBAAmB,EACnB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,GAC1B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { cleanupCaches, } from './cache-cleanup.js';
|
|
2
|
-
export { CACHE_STRATEGIES, createCacheStrategyDecider, decideCacheStrategy, } from './cache-strategy.js';
|
|
2
|
+
export { ALWAYS_NETWORK_ONLY_PATH_PREFIXES, CACHE_STRATEGIES, createCacheStrategyDecider, decideCacheStrategy, } from './cache-strategy.js';
|
|
3
3
|
export { createFetchHandler, } from './fetch-handler.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,GAId,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,gBAAgB,EAChB,0BAA0B,EAC1B,mBAAmB,GAIpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,GAInB,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,GAId,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,iCAAiC,EACjC,gBAAgB,EAChB,0BAA0B,EAC1B,mBAAmB,GAIpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,GAInB,MAAM,oBAAoB,CAAC"}
|
package/dist/worker.js
CHANGED
|
@@ -18,6 +18,8 @@ this.globalThis.BaukitPwa = (function(exports) {
|
|
|
18
18
|
networkOnly: "network-only",
|
|
19
19
|
staleWhileRevalidate: "stale-while-revalidate"
|
|
20
20
|
};
|
|
21
|
+
/** Browser session routes: never cached, whatever the options say. */
|
|
22
|
+
const ALWAYS_NETWORK_ONLY_PATH_PREFIXES = ["/auth/session/"];
|
|
21
23
|
const DEFAULT_API_PATH_PREFIXES = ["/api"];
|
|
22
24
|
const DEFAULT_STATIC_DESTINATIONS = [
|
|
23
25
|
"audio",
|
|
@@ -68,12 +70,19 @@ this.globalThis.BaukitPwa = (function(exports) {
|
|
|
68
70
|
function startsWithAny(pathname, prefixes) {
|
|
69
71
|
return prefixes.some((prefix) => prefix.length > 0 && pathname.startsWith(prefix));
|
|
70
72
|
}
|
|
73
|
+
function isStaticAsset(request, pathname, options) {
|
|
74
|
+
const { staticPathPrefixes = [], staticDestinations = DEFAULT_STATIC_DESTINATIONS, staticExtensions = DEFAULT_STATIC_EXTENSIONS } = options;
|
|
75
|
+
return request.destination !== void 0 && staticDestinations.includes(request.destination) || hasStaticExtension(pathname, staticExtensions) || startsWithAny(pathname, staticPathPrefixes);
|
|
76
|
+
}
|
|
77
|
+
function isNeverCached(request, pathname, neverCachedPathPrefixes) {
|
|
78
|
+
return matchesAnyPathPrefix(pathname, ALWAYS_NETWORK_ONLY_PATH_PREFIXES) || matchesAnyPathPrefix(pathname, neverCachedPathPrefixes) || request.headers?.has("authorization") === true;
|
|
79
|
+
}
|
|
71
80
|
function decideCacheStrategyDecision(request, options) {
|
|
72
|
-
const { appOrigin, apiPathPrefixes = DEFAULT_API_PATH_PREFIXES, navigationFallback, neverCachedPathPrefixes = [],
|
|
81
|
+
const { appOrigin, apiPathPrefixes = DEFAULT_API_PATH_PREFIXES, navigationFallback, neverCachedPathPrefixes = [], apiStrategy = CACHE_STRATEGIES.networkFirst, staticStrategy = CACHE_STRATEGIES.cacheFirst, defaultStrategy = CACHE_STRATEGIES.networkFirst } = options;
|
|
73
82
|
const parsed = new URL(request.url, appOrigin);
|
|
74
83
|
const { pathname } = parsed;
|
|
75
84
|
const sameOrigin = parsed.origin === appOrigin;
|
|
76
|
-
if (
|
|
85
|
+
if (isNeverCached(request, pathname, neverCachedPathPrefixes)) return { strategy: CACHE_STRATEGIES.networkOnly };
|
|
77
86
|
if ((request.method ?? "GET").toUpperCase() !== "GET") return { strategy: CACHE_STRATEGIES.networkOnly };
|
|
78
87
|
if (matchesAnyPathPrefix(pathname, apiPathPrefixes)) return { strategy: apiStrategy };
|
|
79
88
|
if (request.mode === "navigate") {
|
|
@@ -83,7 +92,7 @@ this.globalThis.BaukitPwa = (function(exports) {
|
|
|
83
92
|
};
|
|
84
93
|
return { strategy: defaultStrategy };
|
|
85
94
|
}
|
|
86
|
-
const staticAsset =
|
|
95
|
+
const staticAsset = isStaticAsset(request, pathname, options);
|
|
87
96
|
return { strategy: sameOrigin && staticAsset ? staticStrategy : defaultStrategy };
|
|
88
97
|
}
|
|
89
98
|
function decideCacheStrategy(request, options) {
|
|
@@ -146,6 +155,7 @@ this.globalThis.BaukitPwa = (function(exports) {
|
|
|
146
155
|
};
|
|
147
156
|
}
|
|
148
157
|
//#endregion
|
|
158
|
+
exports.ALWAYS_NETWORK_ONLY_PATH_PREFIXES = ALWAYS_NETWORK_ONLY_PATH_PREFIXES;
|
|
149
159
|
exports.CACHE_STRATEGIES = CACHE_STRATEGIES;
|
|
150
160
|
exports.cleanupCaches = cleanupCaches;
|
|
151
161
|
exports.createCacheStrategyDecider = createCacheStrategyDecider;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/pwa-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "Cache strategy, cleanup, and a classic worker artifact for web PWAs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Patrick Koss",
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"test": "pnpm run build && tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts && pnpm run test:artifact && pnpm run test:package",
|
|
45
45
|
"test:artifact": "node scripts/test-artifact.mjs",
|
|
46
46
|
"test:package": "node scripts/test-package.mjs",
|
|
47
|
-
"lint": "eslint
|
|
47
|
+
"lint": "eslint . --max-warnings=0",
|
|
48
48
|
"format:check": "prettier --check src scripts package.json tsconfig.json tsconfig.test.json tsconfig.cjs.json"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"rolldown": "1.2.12",
|
|
52
52
|
"typescript": "6.0.3",
|
|
53
|
-
"vitest": "5.0.
|
|
53
|
+
"vitest": "5.0.5"
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -52,6 +52,35 @@ describe('decideCacheStrategy', () => {
|
|
|
52
52
|
).toBe(CACHE_STRATEGIES.networkOnly);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
+
it('keeps browser session routes network-only on any origin, whatever the options say', () => {
|
|
56
|
+
const options = {
|
|
57
|
+
appOrigin: APP_ORIGIN,
|
|
58
|
+
apiPathPrefixes: ['/auth'],
|
|
59
|
+
apiStrategy: CACHE_STRATEGIES.cacheFirst,
|
|
60
|
+
defaultStrategy: CACHE_STRATEGIES.cacheFirst,
|
|
61
|
+
};
|
|
62
|
+
for (const url of [
|
|
63
|
+
`${APP_ORIGIN}/auth/session`,
|
|
64
|
+
`${APP_ORIGIN}/auth/session/refresh`,
|
|
65
|
+
'https://api.example.com/auth/session/exchange',
|
|
66
|
+
]) {
|
|
67
|
+
expect(decideCacheStrategy({ url }, options)).toBe(CACHE_STRATEGIES.networkOnly);
|
|
68
|
+
}
|
|
69
|
+
expect(decideCacheStrategy({ url: `${APP_ORIGIN}/auth/sessions` }, options)).toBe(
|
|
70
|
+
CACHE_STRATEGIES.cacheFirst,
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('never caches a request that carries an Authorization header', () => {
|
|
75
|
+
const headers = new Headers({ Authorization: 'Bearer token' });
|
|
76
|
+
for (const url of [`${APP_ORIGIN}/api/v1/me`, `${APP_ORIGIN}/logo.png`, `${APP_ORIGIN}/`]) {
|
|
77
|
+
expect(decideCacheStrategy({ url, headers }, OPTIONS)).toBe(CACHE_STRATEGIES.networkOnly);
|
|
78
|
+
}
|
|
79
|
+
expect(
|
|
80
|
+
decideCacheStrategy({ url: `${APP_ORIGIN}/logo.png`, headers: new Headers() }, OPTIONS),
|
|
81
|
+
).toBe(CACHE_STRATEGIES.cacheFirst);
|
|
82
|
+
});
|
|
83
|
+
|
|
55
84
|
it('leaves the sync path unconfigured by default', () => {
|
|
56
85
|
expect(decideCacheStrategy({ url: `${APP_ORIGIN}/api/v1/sync` }, OPTIONS)).toBe(
|
|
57
86
|
CACHE_STRATEGIES.networkFirst,
|
package/src/cache-strategy.ts
CHANGED
|
@@ -12,6 +12,7 @@ export interface CacheStrategyRequest {
|
|
|
12
12
|
readonly method?: string;
|
|
13
13
|
readonly mode?: string;
|
|
14
14
|
readonly destination?: string;
|
|
15
|
+
readonly headers?: { has(name: string): boolean };
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export interface CacheStrategyOptions {
|
|
@@ -32,6 +33,9 @@ export interface CacheStrategyDecision {
|
|
|
32
33
|
readonly navigationFallback?: string;
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
/** Browser session routes: never cached, whatever the options say. */
|
|
37
|
+
export const ALWAYS_NETWORK_ONLY_PATH_PREFIXES: readonly string[] = ['/auth/session/'];
|
|
38
|
+
|
|
35
39
|
const DEFAULT_API_PATH_PREFIXES: readonly string[] = ['/api'];
|
|
36
40
|
|
|
37
41
|
const DEFAULT_STATIC_DESTINATIONS: readonly string[] = [
|
|
@@ -93,6 +97,35 @@ function startsWithAny(pathname: string, prefixes: readonly string[]): boolean {
|
|
|
93
97
|
return prefixes.some((prefix) => prefix.length > 0 && pathname.startsWith(prefix));
|
|
94
98
|
}
|
|
95
99
|
|
|
100
|
+
function isStaticAsset(
|
|
101
|
+
request: CacheStrategyRequest,
|
|
102
|
+
pathname: string,
|
|
103
|
+
options: CacheStrategyOptions,
|
|
104
|
+
): boolean {
|
|
105
|
+
const {
|
|
106
|
+
staticPathPrefixes = [],
|
|
107
|
+
staticDestinations = DEFAULT_STATIC_DESTINATIONS,
|
|
108
|
+
staticExtensions = DEFAULT_STATIC_EXTENSIONS,
|
|
109
|
+
} = options;
|
|
110
|
+
return (
|
|
111
|
+
(request.destination !== undefined && staticDestinations.includes(request.destination)) ||
|
|
112
|
+
hasStaticExtension(pathname, staticExtensions) ||
|
|
113
|
+
startsWithAny(pathname, staticPathPrefixes)
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isNeverCached(
|
|
118
|
+
request: CacheStrategyRequest,
|
|
119
|
+
pathname: string,
|
|
120
|
+
neverCachedPathPrefixes: readonly string[],
|
|
121
|
+
): boolean {
|
|
122
|
+
return (
|
|
123
|
+
matchesAnyPathPrefix(pathname, ALWAYS_NETWORK_ONLY_PATH_PREFIXES) ||
|
|
124
|
+
matchesAnyPathPrefix(pathname, neverCachedPathPrefixes) ||
|
|
125
|
+
request.headers?.has('authorization') === true
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
96
129
|
export function decideCacheStrategyDecision(
|
|
97
130
|
request: CacheStrategyRequest,
|
|
98
131
|
options: CacheStrategyOptions,
|
|
@@ -102,9 +135,6 @@ export function decideCacheStrategyDecision(
|
|
|
102
135
|
apiPathPrefixes = DEFAULT_API_PATH_PREFIXES,
|
|
103
136
|
navigationFallback,
|
|
104
137
|
neverCachedPathPrefixes = [],
|
|
105
|
-
staticPathPrefixes = [],
|
|
106
|
-
staticDestinations = DEFAULT_STATIC_DESTINATIONS,
|
|
107
|
-
staticExtensions = DEFAULT_STATIC_EXTENSIONS,
|
|
108
138
|
apiStrategy = CACHE_STRATEGIES.networkFirst,
|
|
109
139
|
staticStrategy = CACHE_STRATEGIES.cacheFirst,
|
|
110
140
|
defaultStrategy = CACHE_STRATEGIES.networkFirst,
|
|
@@ -114,7 +144,7 @@ export function decideCacheStrategyDecision(
|
|
|
114
144
|
const { pathname } = parsed;
|
|
115
145
|
const sameOrigin = parsed.origin === appOrigin;
|
|
116
146
|
|
|
117
|
-
if (
|
|
147
|
+
if (isNeverCached(request, pathname, neverCachedPathPrefixes)) {
|
|
118
148
|
return { strategy: CACHE_STRATEGIES.networkOnly };
|
|
119
149
|
}
|
|
120
150
|
|
|
@@ -134,11 +164,7 @@ export function decideCacheStrategyDecision(
|
|
|
134
164
|
return { strategy: defaultStrategy };
|
|
135
165
|
}
|
|
136
166
|
|
|
137
|
-
const staticAsset =
|
|
138
|
-
(request.destination !== undefined && staticDestinations.includes(request.destination)) ||
|
|
139
|
-
hasStaticExtension(pathname, staticExtensions) ||
|
|
140
|
-
startsWithAny(pathname, staticPathPrefixes);
|
|
141
|
-
|
|
167
|
+
const staticAsset = isStaticAsset(request, pathname, options);
|
|
142
168
|
return { strategy: sameOrigin && staticAsset ? staticStrategy : defaultStrategy };
|
|
143
169
|
}
|
|
144
170
|
|
|
@@ -71,6 +71,25 @@ describe('createFetchHandler', () => {
|
|
|
71
71
|
expect(puts).toEqual([]);
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
+
it.each([
|
|
75
|
+
{ name: 'a session route', request: { url: `${APP_ORIGIN}/auth/session/refresh` } },
|
|
76
|
+
{
|
|
77
|
+
name: 'an authorized API request',
|
|
78
|
+
request: {
|
|
79
|
+
url: `${APP_ORIGIN}/api/v1/me`,
|
|
80
|
+
headers: new Headers({ authorization: 'Bearer token' }),
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
])('neither reads nor writes the cache for $name', async ({ request }) => {
|
|
84
|
+
const { ports, fetchCalls, puts, matchCalls } = harness({ cached: { body: 'stale' } });
|
|
85
|
+
const handle = createFetchHandler({ appOrigin: APP_ORIGIN, ports });
|
|
86
|
+
|
|
87
|
+
await expect(handle(request)).resolves.toEqual({ body: 'network' });
|
|
88
|
+
expect(fetchCalls).toEqual([{ url: request.url, noStore: true }]);
|
|
89
|
+
expect(puts).toEqual([]);
|
|
90
|
+
expect(matchCalls).toEqual([]);
|
|
91
|
+
});
|
|
92
|
+
|
|
74
93
|
it('serves a cache-first hit without touching the network', async () => {
|
|
75
94
|
const { ports, fetchCalls } = harness({ cached: { body: 'cached-icon' } });
|
|
76
95
|
const handle = createFetchHandler({ appOrigin: APP_ORIGIN, ports });
|