@ultimat3/pwa 31.13.1 → 31.15.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/CLAUDE.md +2 -0
- package/package.json +6 -6
- package/src/pages-cache-source.ts +25 -5
- package/src/precached-asset-source.ts +29 -0
- package/src/service-worker.ts +5 -0
package/CLAUDE.md
CHANGED
|
@@ -28,6 +28,8 @@ role's boot loads, `@ultimat3/mcp/schema`'s shape; `serve-graph.test.ts` exempts
|
|
|
28
28
|
| Install fill | `PRECACHE_CONCURRENCY` (4) in flight, never `Promise.all` over the manifest: it runs on the first visit against the connection the first click needs. Still one entry per failure. An answer stamped with another build's core `BUILD_ID_HEADER` is not stored (an old pod mid-rollout); an unstamped one is. |
|
|
29
29
|
| Rule patterns | built from the DECODED route path, matched against the PERCENT-ENCODED `url.pathname`: a literal segment goes through the WHATWG serializer (`URL.pathname`, never `encodeURI` — it escapes `\|`, `[`, `^`, `%` a browser leaves literal), either hex case. A catch-all is `(?:/.*)?`, so `/docs/*path` owns `/docs`. `route-rules.test.ts`; the e2e drives both in Chrome. |
|
|
30
30
|
| Runtime assets | `ServiceWorkerConfig.runtimeAssets` prefixes (`/islands/`) are `cache-first` runtime rules AHEAD of every page rule (`assetRules`, `route-rules.ts`), and go to the network as the browser asked — no build-id header, because a classic script is a `no-cors` request whose headers a worker may not extend. The precache names only what a precached page boots (the CLI chooses: `sw-precache-plan.ts`); all 43 islands (~1 MB) were precached on a first anonymous visit until 22.3.3. |
|
|
31
|
+
| Precached assets | answered from the precache AHEAD of every rule (`precached-asset-source.ts`): first when revision = url, else network first. No rule read them before, `As of 2026-11`: offline documents rendered unstyled. |
|
|
32
|
+
| Pages cache | ≤ `PAGES_CACHE_MAX` (50) documents per cache, oldest written evicted (`putPage`). |
|
|
31
33
|
| Locales | the worker matches pathnames and the server strips `/en/` before it matches, so the CALLER spells every route once per routed locale (`@ultimat3/cli`'s `sw-precache-plan.ts`). `localePrefixes` is only for the offline document: a navigation under `/en/` tries `/en` + `OFFLINE_DOC` first. |
|
|
32
34
|
| What is a navigation | to `offlineFallback`: `mode === 'navigate'`, or the client router's soft visit — a `fetch` carrying `CLIENT_NAVIGATION_HEADER: soft` (core's constant, interpolated). Its `prefetch` is a guess: the 503, or the offline page would be cached as a later click's answer. `offline-fallback.test.ts`. |
|
|
33
35
|
| Precache revision | content hash. Never the build id — that re-downloads everything per deploy. |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/pwa",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.15.0",
|
|
4
4
|
"description": "Generated service worker, web manifest, icons, push and version-skew handling.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"test": "bun test"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ultimat3/action": "31.
|
|
49
|
-
"@ultimat3/core": "31.
|
|
50
|
-
"@ultimat3/i18n": "31.
|
|
51
|
-
"@ultimat3/policy": "31.
|
|
52
|
-
"@ultimat3/schema": "31.
|
|
48
|
+
"@ultimat3/action": "31.15.0",
|
|
49
|
+
"@ultimat3/core": "31.15.0",
|
|
50
|
+
"@ultimat3/i18n": "31.15.0",
|
|
51
|
+
"@ultimat3/policy": "31.15.0",
|
|
52
|
+
"@ultimat3/schema": "31.15.0"
|
|
53
53
|
}
|
|
54
54
|
}
|
|
@@ -17,6 +17,14 @@ export const PAGES_CLEARED_MESSAGE = 'pages-cleared';
|
|
|
17
17
|
/** Every build's pages cache starts with this — `cacheNamespace(id, 'pages')` is `x-pages-<id>`. */
|
|
18
18
|
export const PAGES_CACHE_PREFIX = 'x-pages-';
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Documents one pages cache keeps, oldest WRITTEN evicted first (`cache.keys()` order: a `put`
|
|
22
|
+
* re-appends). Unbounded, a build's `x-pages-<id>` grew with every document visited until the next
|
|
23
|
+
* deploy or the browser's quota — and a quota eviction takes the whole origin's storage, precache
|
|
24
|
+
* and outbox included. 50 is a session's worth of offline reading, not an archive.
|
|
25
|
+
*/
|
|
26
|
+
export const PAGES_CACHE_MAX = 50;
|
|
27
|
+
|
|
20
28
|
/**
|
|
21
29
|
* What the worker does with a page rendered for someone — `pwa.offline.personalPages`.
|
|
22
30
|
*
|
|
@@ -36,7 +44,19 @@ function isPrivate(r){
|
|
|
36
44
|
return /(^|,)\\s*(private|no-store)\\s*(,|$)/i.test(r.headers.get('cache-control')||'')
|
|
37
45
|
}
|
|
38
46
|
// Every cache but the pages cache is GUARDED in both modes: a private response is never stored there.
|
|
39
|
-
|
|
47
|
+
// A pages cache (\`bounded\`) is capped at PAGES_MAX documents, oldest WRITTEN out first.
|
|
48
|
+
function guarded(c,bounded){return{match:(req)=>c.match(req),put:async(req,r)=>{if(!isPrivate(r))return bounded?putPage(c,req,r):c.put(req,r)}}}
|
|
49
|
+
// keys() is insertion order and a put re-appends, so the head is the page least recently stored.
|
|
50
|
+
// One write at a time: a trim reads keys() and deletes by URL, so a refresh landing between the two
|
|
51
|
+
// re-appended a page the trim then deleted — the page just written gone, an older one kept.
|
|
52
|
+
const PAGES_MAX=${String(PAGES_CACHE_MAX)};
|
|
53
|
+
let paging=Promise.resolve();
|
|
54
|
+
function putPage(c,req,r){const run=paging.then(()=>putTrim(c,req,r));paging=run.catch(()=>{});return run}
|
|
55
|
+
async function putTrim(c,req,r){
|
|
56
|
+
await c.put(req,r);
|
|
57
|
+
const keys=await c.keys();
|
|
58
|
+
if(keys.length>PAGES_MAX)await Promise.all(keys.slice(0,keys.length-PAGES_MAX).map((k)=>c.delete(k)));
|
|
59
|
+
}
|
|
40
60
|
function fallbackFor(rule,req){return rule.c==='pages'?()=>pageOffline(req):()=>offlineFallback(req)}
|
|
41
61
|
// The app's sign-out: every build's pages cache, and every per-member partition.
|
|
42
62
|
async function clearPages(){
|
|
@@ -47,7 +67,7 @@ async function clearPages(){
|
|
|
47
67
|
const NEVER = `// A private response (\`private\`/\`no-store\`, or carrying the scope header) is never stored,
|
|
48
68
|
// whatever the rule. 21.0.0 kept one per member for offline, and after sign-out an offline
|
|
49
69
|
// navigation on a shared device answered the previous member's data. \`no-store\` means what it says.
|
|
50
|
-
function openCache(cn){return caches.open(cn).then(guarded)}
|
|
70
|
+
function openCache(cn){return caches.open(cn).then((c)=>guarded(c,cn===PAGES))}
|
|
51
71
|
/** Offline only: the shared copy (a warm-up may have landed it late), then the offline document. */
|
|
52
72
|
async function pageOffline(req){
|
|
53
73
|
await warming;
|
|
@@ -60,11 +80,11 @@ const LAST_MEMBER = `// personalPages: 'last-member'. The pages cache is a FACAD
|
|
|
60
80
|
// read as before; a private one is never read from here — so no strategy answers it from cache
|
|
61
81
|
// while online — and is kept only in its principal's own partition, PAGES~<scope>, which offline
|
|
62
82
|
// alone reads. Storing one principal's page wipes every other partition. clear-pages empties them.
|
|
63
|
-
function openCache(cn){return cn===PAGES?Promise.resolve(pagesCache()):caches.open(cn).then(guarded)}
|
|
83
|
+
function openCache(cn){return cn===PAGES?Promise.resolve(pagesCache()):caches.open(cn).then((c)=>guarded(c,false))}
|
|
64
84
|
function pagesCache(){return{
|
|
65
85
|
match:async(req)=>(await caches.open(PAGES)).match(req),
|
|
66
86
|
put:async(req,r)=>{
|
|
67
|
-
if(!isPrivate(r))return (await caches.open(PAGES)
|
|
87
|
+
if(!isPrivate(r))return putPage(await caches.open(PAGES),req,r);
|
|
68
88
|
const scope=r.headers.get(SCOPE_HEADER);
|
|
69
89
|
// Private with no scope: no principal to file it under, so it is not kept at all.
|
|
70
90
|
if(scope===null)return;
|
|
@@ -74,7 +94,7 @@ function pagesCache(){return{
|
|
|
74
94
|
}}
|
|
75
95
|
let keeping=Promise.resolve();
|
|
76
96
|
async function keepScoped(req,r,name){
|
|
77
|
-
await (await caches.open(name)
|
|
97
|
+
await putPage(await caches.open(name),req,r);
|
|
78
98
|
const names=await caches.keys();
|
|
79
99
|
await Promise.all(names.filter((n)=>n.startsWith(PAGES+'~')&&n!==name).map((n)=>caches.delete(n)));
|
|
80
100
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The emitted worker's read of a precached ASSET — a stylesheet, an island chunk, a placeholder.
|
|
3
|
+
* Its own module because `service-worker.ts` is the rest of `sw.js`; spliced in after the precache
|
|
4
|
+
* manifest, and read by the fetch block before any route rule. `service-worker-assets.test.ts`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { PrecacheManifest } from './precache';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Until this ran, the install block filled `x-precache-<build>` with every stylesheet and nothing
|
|
11
|
+
* ever read one: no rule matched `/styles/<hash>.css`, so an offline document rendered unstyled
|
|
12
|
+
* beside its own stylesheet, and a precached island chunk was downloaded a second time into the
|
|
13
|
+
* runtime cache. `[url, immutable]` — immutable when the revision IS the url, the caller's
|
|
14
|
+
* declaration that the URL is content-addressed (`sw-precache-plan.ts` writes it for every chunk).
|
|
15
|
+
*/
|
|
16
|
+
export function precachedAssetSource(manifest: PrecacheManifest): string {
|
|
17
|
+
const rows = manifest.entries
|
|
18
|
+
.filter((entry) => entry.reason === 'asset')
|
|
19
|
+
.map((entry) => [entry.url, entry.revision === entry.url]);
|
|
20
|
+
return `// Keyed by absolute href, as the install block stores them (new Request(e.url)).
|
|
21
|
+
const PRECACHED_ASSETS=new Map(${JSON.stringify(rows)}.map(([u,i])=>[new URL(u,self.location.origin).href,i]));
|
|
22
|
+
// Content-addressed: the precache first, the bytes cannot differ. Otherwise the network first, so
|
|
23
|
+
// a newer build's bytes at the same URL win online, and the precached copy answers offline.
|
|
24
|
+
async function precachedAsset(req,immutable){
|
|
25
|
+
const c=await caches.open(PRECACHE);
|
|
26
|
+
if(immutable){const hit=await c.match(req);return hit||fetch(req)}
|
|
27
|
+
try{return await fetch(req)}catch(e){const hit=await c.match(req);if(hit)return hit;throw e}
|
|
28
|
+
}`;
|
|
29
|
+
}
|
package/src/service-worker.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
} from './pages-cache-source';
|
|
25
25
|
import type { PrecacheAsset, PrecacheManifest } from './precache';
|
|
26
26
|
import { buildPrecacheManifest, serializePrecacheManifest } from './precache';
|
|
27
|
+
import { precachedAssetSource } from './precached-asset-source';
|
|
27
28
|
import type { VapidConfig } from './push';
|
|
28
29
|
import { pushSource } from './push';
|
|
29
30
|
import type { RouteRule } from './route-rules';
|
|
@@ -150,6 +151,7 @@ export function generateServiceWorker(
|
|
|
150
151
|
header(buildId),
|
|
151
152
|
constants(buildId, scope, retained, fallback.neverCache),
|
|
152
153
|
`const PRECACHE_MANIFEST=${serializePrecacheManifest(precache)};`,
|
|
154
|
+
precachedAssetSource(precache),
|
|
153
155
|
`const ROUTE_RULES=${serializeRules(rules)};`,
|
|
154
156
|
[NETWORK_SOURCE, ...usedStrategies.map((strategy) => STRATEGY_SOURCE[strategy])].join('\n'),
|
|
155
157
|
offlineFallbackSource(fallback, config.localePrefixes ?? []),
|
|
@@ -370,6 +372,9 @@ self.addEventListener('fetch',(event)=>{
|
|
|
370
372
|
const url=new URL(req.url);
|
|
371
373
|
if(url.origin!==self.location.origin)return;
|
|
372
374
|
if(NEVER_CACHE.some((p)=>url.pathname.startsWith(p)))return passThrough(event);
|
|
375
|
+
// Ahead of every rule: the install block filled the precache for exactly these requests.
|
|
376
|
+
const asset=PRECACHED_ASSETS.get(url.href);
|
|
377
|
+
if(asset!==undefined)return event.respondWith(precachedAsset(req,asset));
|
|
373
378
|
const rule=ruleFor(url);
|
|
374
379
|
if(!rule)return passThrough(event);
|
|
375
380
|
const fn=STRATEGIES[rule.s];
|