@stacksjs/stx 0.2.406 → 0.2.408
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/dist/app-service-worker.d.ts +120 -0
- package/dist/chunk-00g9x6w2.js +2 -0
- package/dist/chunk-02g67164.js +2 -0
- package/dist/chunk-04tgatyy.js +2 -0
- package/dist/{chunk-4g4yz97f.js → chunk-0a315nbf.js} +3 -3
- package/dist/{chunk-zwb8wv5p.js → chunk-0akydm2d.js} +1 -1
- package/dist/{chunk-tfy07tmv.js → chunk-0btmm4m2.js} +1 -1
- package/dist/{chunk-g0w8dyat.js → chunk-0c2h69k2.js} +1 -1
- package/dist/{chunk-aa4ss5pb.js → chunk-0pkk226n.js} +1 -1
- package/dist/{chunk-6de3hczw.js → chunk-0we9nw1d.js} +1 -1
- package/dist/chunk-178n9c40.js +2 -0
- package/dist/{chunk-2r399td7.js → chunk-1fs9jvpb.js} +1 -1
- package/dist/chunk-1hc58sh2.js +2 -0
- package/dist/chunk-1q4jrkhw.js +2 -0
- package/dist/chunk-1qembpzh.js +2 -0
- package/dist/chunk-1qq26zt0.js +2 -0
- package/dist/{chunk-7q06md6v.js → chunk-1te8k8gp.js} +1 -1
- package/dist/{chunk-jw6s8rkk.js → chunk-1ws3xcea.js} +2 -2
- package/dist/{chunk-z9a0ft6h.js → chunk-213jwgpx.js} +1 -1
- package/dist/{chunk-1yvx8s3v.js → chunk-26r88x1t.js} +2 -2
- package/dist/{chunk-k4x5we49.js → chunk-27fgag95.js} +2 -2
- package/dist/chunk-2c6t60vw.js +2 -0
- package/dist/{chunk-x7qyx7vn.js → chunk-2fw05hs2.js} +1 -1
- package/dist/{chunk-0seyntda.js → chunk-38wvkv3s.js} +1 -1
- package/dist/chunk-3w6z6tw9.js +2 -0
- package/dist/chunk-3za7m29m.js +2 -0
- package/dist/{chunk-ypw4egcv.js → chunk-4m7ajama.js} +1 -1
- package/dist/{chunk-g81ymdat.js → chunk-4zcbj36p.js} +2 -2
- package/dist/{chunk-yh9apppb.js → chunk-50pszj17.js} +1 -1
- package/dist/{chunk-59pakqdv.js → chunk-563shnkz.js} +2 -2
- package/dist/{chunk-h1yb87ep.js → chunk-5r4s9s38.js} +2 -2
- package/dist/chunk-603x5gb2.js +2 -0
- package/dist/{chunk-afwbqeb2.js → chunk-6jrczd12.js} +2 -2
- package/dist/{chunk-abs56gdt.js → chunk-6kd5jjwc.js} +1 -1
- package/dist/{chunk-ewxbx67c.js → chunk-6kdrncsx.js} +1 -1
- package/dist/{chunk-pkspqh0m.js → chunk-72rwcbp6.js} +1 -1
- package/dist/{chunk-4m4n1sfc.js → chunk-76w34xne.js} +1 -1
- package/dist/chunk-7ypm243n.js +2 -0
- package/dist/{chunk-z80v5h5h.js → chunk-8036mdat.js} +2 -2
- package/dist/{chunk-46zx4pt8.js → chunk-82pd4b5w.js} +1 -1
- package/dist/chunk-8ds7rrdc.js +2 -0
- package/dist/chunk-8w2mqdnv.js +2 -0
- package/dist/chunk-8x05b790.js +2 -0
- package/dist/{chunk-pqhpcxsd.js → chunk-90b2vt2n.js} +1 -1
- package/dist/{chunk-arjhs9bb.js → chunk-920779jx.js} +2 -2
- package/dist/chunk-934eeghd.js +2 -0
- package/dist/{chunk-245ft0zp.js → chunk-9e8t0twa.js} +1 -1
- package/dist/{chunk-wpsx5avz.js → chunk-9fqp72ze.js} +1 -1
- package/dist/chunk-9wze2290.js +2 -0
- package/dist/{chunk-dj658wyq.js → chunk-9xew0kgf.js} +1 -1
- package/dist/{chunk-ca57sg6v.js → chunk-akwd34xt.js} +2 -2
- package/dist/{chunk-rqr735w8.js → chunk-anydpa34.js} +1 -1
- package/dist/{chunk-ffdxdgep.js → chunk-aq1xq0r4.js} +2 -2
- package/dist/{chunk-9dap2hdv.js → chunk-avaywwsc.js} +2 -2
- package/dist/{chunk-36rnwhn2.js → chunk-bbt3tp6f.js} +1 -1
- package/dist/{chunk-n4197kjq.js → chunk-bjar18jd.js} +1 -1
- package/dist/{chunk-twg0y6ga.js → chunk-bjcsc6wx.js} +1 -1
- package/dist/{chunk-akzmq0jr.js → chunk-bn1edb1y.js} +2 -2
- package/dist/{chunk-503wfstj.js → chunk-btd7ds3x.js} +1 -1
- package/dist/{chunk-8mm86z5w.js → chunk-bwsxym3d.js} +1 -1
- package/dist/{chunk-c1f1vtjt.js → chunk-cj3adc77.js} +1 -1
- package/dist/{chunk-sjc7x8y1.js → chunk-cqchpe34.js} +396 -165
- package/dist/chunk-ct1jwd4r.js +2 -0
- package/dist/{chunk-znt6t1dz.js → chunk-ej16bgrr.js} +2 -2
- package/dist/{chunk-2c23jad1.js → chunk-esq1k7fj.js} +2 -2
- package/dist/{chunk-ayhhvep9.js → chunk-f6s7d1jq.js} +2 -2
- package/dist/{chunk-bds4s9d8.js → chunk-fcnrgxck.js} +1 -1
- package/dist/chunk-fphs5g19.js +2 -0
- package/dist/{chunk-stdkw5st.js → chunk-fpwzacpm.js} +1 -1
- package/dist/chunk-gbmeh7jg.js +3 -0
- package/dist/{chunk-81zw5vw5.js → chunk-gc06g35w.js} +1 -1
- package/dist/{chunk-rctgq5xp.js → chunk-gcvj5zdy.js} +3 -3
- package/dist/{chunk-1nnskved.js → chunk-gengvwb7.js} +3 -3
- package/dist/{chunk-63dyh42m.js → chunk-gjrgr367.js} +2 -2
- package/dist/{chunk-69c5rv6n.js → chunk-gna6dc48.js} +169 -169
- package/dist/{chunk-pyyccs6a.js → chunk-gvbf1mhs.js} +1 -1
- package/dist/chunk-h1r17awz.js +2 -0
- package/dist/chunk-ha46rg9g.js +2 -0
- package/dist/chunk-hcvjstmr.js +2 -0
- package/dist/{chunk-a01sf1sc.js → chunk-hdysywd8.js} +1 -1
- package/dist/{chunk-55g4qm7m.js → chunk-hx998w7n.js} +1 -1
- package/dist/chunk-j3r97xs5.js +2 -0
- package/dist/chunk-j5qeaprt.js +2 -0
- package/dist/chunk-jxvmf02k.js +2 -0
- package/dist/{chunk-8t3h7fsk.js → chunk-k80sn8d1.js} +1 -1
- package/dist/chunk-kajym1cx.js +2 -0
- package/dist/{chunk-f02g2h4p.js → chunk-kbwhs5df.js} +2 -2
- package/dist/{chunk-4n6ekt06.js → chunk-kmsyvaex.js} +1 -1
- package/dist/{chunk-4tsjysng.js → chunk-kvmx7dnb.js} +1 -1
- package/dist/{chunk-s1gwy2rp.js → chunk-m072c38p.js} +2 -2
- package/dist/{chunk-jpnja3jy.js → chunk-mgrckqww.js} +1 -1
- package/dist/{chunk-971yqxa2.js → chunk-mq5ewmz6.js} +1 -1
- package/dist/{chunk-sptvg80e.js → chunk-mrahwn5v.js} +1 -1
- package/dist/{chunk-vyq86x5c.js → chunk-n2swas11.js} +1 -1
- package/dist/{chunk-zgzzn9qk.js → chunk-n9cqrw9j.js} +1 -1
- package/dist/{chunk-3sea73fx.js → chunk-naw4jt7j.js} +1 -1
- package/dist/{chunk-1n2ng8nx.js → chunk-ny67c03k.js} +1 -1
- package/dist/chunk-p0m4ty6g.js +2 -0
- package/dist/chunk-p32x2apf.js +2 -0
- package/dist/{chunk-8bg1qmhg.js → chunk-p4zrvt5f.js} +1 -1
- package/dist/{chunk-0tnsyynz.js → chunk-p5wp0ce6.js} +2 -2
- package/dist/{chunk-mywrb5fy.js → chunk-pcxp4r9n.js} +1 -1
- package/dist/{chunk-me7ypzw9.js → chunk-pdfjy3at.js} +2 -2
- package/dist/chunk-pjrn29gg.js +2 -0
- package/dist/chunk-q738pwv6.js +2 -0
- package/dist/{chunk-w6n5c479.js → chunk-qmmp5kb5.js} +1 -1
- package/dist/{chunk-015p3jq5.js → chunk-qn5srnj3.js} +1 -1
- package/dist/{chunk-1vrk5y92.js → chunk-qvnbnsv9.js} +1 -1
- package/dist/{chunk-z8zmp74b.js → chunk-rt3cx932.js} +1025 -58
- package/dist/{chunk-7ba7x6jt.js → chunk-rz9f3zqz.js} +2 -2
- package/dist/{chunk-dxjtszaa.js → chunk-s8jd13nb.js} +1 -1
- package/dist/{chunk-qem2tw9r.js → chunk-s9za1vvd.js} +1 -1
- package/dist/{chunk-r909bddf.js → chunk-spxsw4mp.js} +1 -1
- package/dist/chunk-ssn5k77d.js +2 -0
- package/dist/chunk-sy9pfe3h.js +2 -0
- package/dist/{chunk-0nhpymyy.js → chunk-t1htzg9e.js} +1 -1
- package/dist/{chunk-rkzfwgr8.js → chunk-t1tdjjht.js} +1 -1
- package/dist/{chunk-1x6c4ys1.js → chunk-t5ja7qkn.js} +131 -34
- package/dist/{chunk-rx0cntdm.js → chunk-td08rnk3.js} +2 -2
- package/dist/chunk-tgadbsd4.js +2 -0
- package/dist/{chunk-xakdgn9h.js → chunk-tgrpp58r.js} +1 -1
- package/dist/{chunk-pgeh4g3a.js → chunk-tkyrk66a.js} +1 -1
- package/dist/chunk-tpcmn119.js +2 -0
- package/dist/{chunk-0n054h7w.js → chunk-v3nwkveg.js} +1 -1
- package/dist/{chunk-83tefmh1.js → chunk-vsbmc9kt.js} +1 -1
- package/dist/{chunk-qcpnfw0k.js → chunk-vx86cf4r.js} +1 -1
- package/dist/{chunk-yn8qq8tn.js → chunk-w6hqh4z5.js} +2 -2
- package/dist/{chunk-k3wcezqg.js → chunk-w9n12yzf.js} +3 -3
- package/dist/chunk-wkanrfb3.js +2 -0
- package/dist/chunk-wvegegmb.js +2 -0
- package/dist/{chunk-seqmzr8k.js → chunk-xq1acw1r.js} +1 -1
- package/dist/{chunk-vtjegbgy.js → chunk-xsng1mtq.js} +1 -1
- package/dist/chunk-xv9bhvye.js +12 -0
- package/dist/{chunk-gyccx2pr.js → chunk-z52f4zcy.js} +2 -2
- package/dist/{chunk-vtmhh3dp.js → chunk-z5rwnnk1.js} +2 -2
- package/dist/{chunk-m8bdy2wt.js → chunk-zcz9rs6p.js} +1 -1
- package/dist/{chunk-1phxzej9.js → chunk-zpcnbdhn.js} +2 -2
- package/dist/{chunk-wxkn47vk.js → chunk-zq4ssxj9.js} +1 -1
- package/dist/cli.js +14 -14
- package/dist/composables/index.d.ts +10 -1
- package/dist/composables/index.js +6 -1
- package/dist/composables/use-cached-query.d.ts +18 -0
- package/dist/composables/use-cached-query.js +27 -0
- package/dist/composables/use-offline.d.ts +18 -0
- package/dist/composables/use-offline.js +17 -0
- package/dist/composables/use-outbox.d.ts +19 -27
- package/dist/composables/use-outbox.js +100 -16
- package/dist/composables/use-query.d.ts +2 -0
- package/dist/composables/use-storage.d.ts +7 -3
- package/dist/composables/use-storage.js +22 -13
- package/dist/craft-entry.js +1 -1
- package/dist/craft.js +1 -1
- package/dist/database.js +1 -1
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/offline/app-service-worker.d.ts +61 -13
- package/dist/offline/app-service-worker.js +398 -58
- package/dist/offline/kept-store.d.ts +67 -0
- package/dist/offline/kept-store.js +252 -0
- package/dist/offline/query-cache.d.ts +137 -0
- package/dist/offline/query-cache.js +260 -0
- package/dist/pwa.js +2 -2
- package/dist/runtime-globals.js +3 -0
- package/dist/signals.js +123 -24
- package/dist/ssg.js +2 -2
- package/dist/stx-module.d.ts +3 -0
- package/dist/stx.d.ts +48 -1
- package/dist/unresolved-identifiers.js +2 -0
- package/dist/visual-testing.js +1 -1
- package/package.json +5 -5
- package/stx-module.d.ts +3 -0
- package/stx.d.ts +48 -1
- package/dist/chunk-4hqaxar9.js +0 -2
- package/dist/chunk-4md4tj27.js +0 -3
- package/dist/chunk-53x9hrx3.js +0 -2
- package/dist/chunk-5e2d3grq.js +0 -2
- package/dist/chunk-5s44f83p.js +0 -2
- package/dist/chunk-62btdm51.js +0 -2
- package/dist/chunk-68gspceh.js +0 -2
- package/dist/chunk-6cbjhg0y.js +0 -2
- package/dist/chunk-8vg5tpts.js +0 -2
- package/dist/chunk-b26mjv83.js +0 -2
- package/dist/chunk-bmfg13wx.js +0 -2
- package/dist/chunk-ccct2hsa.js +0 -2
- package/dist/chunk-d8yksesg.js +0 -2
- package/dist/chunk-db6fm81v.js +0 -2
- package/dist/chunk-emx4ey5d.js +0 -2
- package/dist/chunk-gxc02n32.js +0 -2
- package/dist/chunk-hch1t137.js +0 -2
- package/dist/chunk-jc5kqj3t.js +0 -2
- package/dist/chunk-jqm6tbtb.js +0 -2
- package/dist/chunk-k6vz8455.js +0 -2
- package/dist/chunk-m7b67r66.js +0 -12
- package/dist/chunk-mtata78t.js +0 -2
- package/dist/chunk-n1yz08j4.js +0 -2
- package/dist/chunk-nsese4as.js +0 -2
- package/dist/chunk-pqskmpz0.js +0 -2
- package/dist/chunk-qa333652.js +0 -2
- package/dist/chunk-qcbn5mrw.js +0 -2
- package/dist/chunk-sgmqfphy.js +0 -2
- package/dist/chunk-wj66sab5.js +0 -2
- package/dist/chunk-wqgyb4zb.js +0 -2
- package/dist/chunk-wszdgqe3.js +0 -2
- package/dist/chunk-wz7ca99x.js +0 -2
- package/dist/chunk-xqz9dqef.js +0 -2
- package/dist/chunk-xvsz1qy3.js +0 -2
- package/dist/chunk-xw23twxw.js +0 -2
- package/dist/chunk-xz870mf2.js +0 -2
- package/dist/chunk-y3cz15x3.js +0 -2
- package/dist/chunk-yf0v44v8.js +0 -2
- package/dist/chunk-zawdn9z8.js +0 -2
|
@@ -16,17 +16,126 @@ export function offlineRoute(path) {
|
|
|
16
16
|
return null;
|
|
17
17
|
return { path, re: `^/${parts.join("/")}/?$`, names, sample: `/${sample.join("/")}` };
|
|
18
18
|
}
|
|
19
|
-
export
|
|
19
|
+
export function registerOfflineWorker(path) {
|
|
20
|
+
if (typeof navigator > "u" || !("serviceWorker" in navigator) || !navigator.serviceWorker)
|
|
21
|
+
return;
|
|
22
|
+
const sw = navigator.serviceWorker, w = window;
|
|
23
|
+
if (w.stxOffline)
|
|
24
|
+
return;
|
|
25
|
+
let registration = null, reloadOnTakeOver = !1;
|
|
26
|
+
const waitingCallbacks = [], api = {
|
|
27
|
+
updateReady: !1,
|
|
28
|
+
registration: () => registration,
|
|
29
|
+
onUpdateReady(callback) {
|
|
30
|
+
if (api.updateReady)
|
|
31
|
+
try {
|
|
32
|
+
callback();
|
|
33
|
+
} catch {}
|
|
34
|
+
else
|
|
35
|
+
waitingCallbacks.push(callback);
|
|
36
|
+
return () => {
|
|
37
|
+
const at = waitingCallbacks.indexOf(callback);
|
|
38
|
+
if (at !== -1)
|
|
39
|
+
waitingCallbacks.splice(at, 1);
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
applyUpdate() {
|
|
43
|
+
const waiting = registration && registration.waiting;
|
|
44
|
+
if (!waiting)
|
|
45
|
+
return !1;
|
|
46
|
+
reloadOnTakeOver = !0;
|
|
47
|
+
waiting.postMessage({ type: "stx:activate-update", force: !0 });
|
|
48
|
+
return !0;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
w.stxOffline = api;
|
|
52
|
+
const ready = (reg) => {
|
|
53
|
+
if (!reg.waiting || !sw.controller || api.updateReady)
|
|
54
|
+
return;
|
|
55
|
+
api.updateReady = !0;
|
|
56
|
+
const callbacks = waitingCallbacks.splice(0);
|
|
57
|
+
for (const callback of callbacks)
|
|
58
|
+
try {
|
|
59
|
+
callback();
|
|
60
|
+
} catch {}
|
|
61
|
+
try {
|
|
62
|
+
window.dispatchEvent(new CustomEvent("stx:offline-update-ready"));
|
|
63
|
+
} catch {}
|
|
64
|
+
};
|
|
65
|
+
sw.addEventListener("message", (event) => {
|
|
66
|
+
const data = event.data || {};
|
|
67
|
+
if (data.type === "stx:updated")
|
|
68
|
+
try {
|
|
69
|
+
window.dispatchEvent(new CustomEvent("stx:updated", { detail: { url: data.url, kind: data.kind, fragment: !!data.fragment } }));
|
|
70
|
+
} catch {}
|
|
71
|
+
});
|
|
72
|
+
sw.addEventListener("controllerchange", () => {
|
|
73
|
+
if (reloadOnTakeOver) {
|
|
74
|
+
reloadOnTakeOver = !1;
|
|
75
|
+
location.reload();
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
document.addEventListener("visibilitychange", () => {
|
|
79
|
+
if (document.visibilityState === "hidden" && registration && registration.waiting)
|
|
80
|
+
registration.waiting.postMessage({ type: "stx:activate-update", reason: "hidden" });
|
|
81
|
+
});
|
|
82
|
+
const register = () => {
|
|
83
|
+
sw.register(path, { scope: "/" }).then((reg) => {
|
|
84
|
+
registration = reg;
|
|
85
|
+
if (reg.waiting) {
|
|
86
|
+
ready(reg);
|
|
87
|
+
reg.waiting.postMessage({ type: "stx:activate-update", reason: "load" });
|
|
88
|
+
}
|
|
89
|
+
reg.addEventListener("updatefound", () => {
|
|
90
|
+
const installing = reg.installing;
|
|
91
|
+
if (!installing)
|
|
92
|
+
return;
|
|
93
|
+
installing.addEventListener("statechange", () => {
|
|
94
|
+
if (installing.state === "installed")
|
|
95
|
+
ready(reg);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}).catch(() => {});
|
|
99
|
+
}, afterPaint = () => {
|
|
100
|
+
const later = (fn) => {
|
|
101
|
+
setTimeout(fn, 0);
|
|
102
|
+
};
|
|
103
|
+
if (typeof requestAnimationFrame === "function")
|
|
104
|
+
requestAnimationFrame(() => later(register));
|
|
105
|
+
else
|
|
106
|
+
later(register);
|
|
107
|
+
};
|
|
108
|
+
if (!sw.controller)
|
|
109
|
+
if (document.readyState === "complete")
|
|
110
|
+
afterPaint();
|
|
111
|
+
else
|
|
112
|
+
addEventListener("load", afterPaint);
|
|
113
|
+
else if (document.readyState === "loading")
|
|
114
|
+
document.addEventListener("DOMContentLoaded", afterPaint);
|
|
115
|
+
else
|
|
116
|
+
afterPaint();
|
|
117
|
+
}
|
|
118
|
+
function registerScriptSource() {
|
|
119
|
+
const source = `(${registerOfflineWorker.toString()})(${JSON.stringify(OFFLINE_WORKER_PATH)})`;
|
|
120
|
+
try {
|
|
121
|
+
return new Bun.Transpiler({ loader: "js", minifyWhitespace: !0 }).transformSync(source).trim().replace(/;$/, "");
|
|
122
|
+
} catch {
|
|
123
|
+
return source;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export const OFFLINE_REGISTER_SCRIPT = `<script data-stx-offline>${registerScriptSource()}</script>`;
|
|
20
127
|
export function generateOfflineWorker(config, buildId) {
|
|
21
128
|
const settings = {
|
|
22
129
|
build: String(buildId || "dev"),
|
|
23
130
|
pages: Array.from(new Set((config.pages || []).filter((page) => typeof page === "string" && page.startsWith("/")))),
|
|
24
131
|
api: config.apiPrefix || "/api/",
|
|
25
|
-
timeout: Math.max(500, Number(config.networkTimeoutMs) ||
|
|
132
|
+
timeout: Math.max(500, Number(config.networkTimeoutMs) || 1500),
|
|
133
|
+
networkFirst: (config.networkFirst || []).filter((path) => typeof path === "string" && path.startsWith("/")),
|
|
26
134
|
fallback: config.fallback || config.pages && config.pages[0] || null,
|
|
27
135
|
routes: (config.routes || []).map(offlineRoute).filter(Boolean),
|
|
28
136
|
exclude: ["/_stx/hmr", OFFLINE_WORKER_PATH, ...config.exclude || []],
|
|
29
|
-
mediaMax: Math.max(10485760, Number(config.mediaMaxBytes) || 629145600)
|
|
137
|
+
mediaMax: Math.max(10485760, Number(config.mediaMaxBytes) || 629145600),
|
|
138
|
+
assetsMax: Math.max(50, Number(config.assetsMaxEntries) || 500)
|
|
30
139
|
};
|
|
31
140
|
return `/* stx offline worker, build ${settings.build} */
|
|
32
141
|
'use strict';
|
|
@@ -36,7 +145,12 @@ var ASSETS = 'stx-assets';
|
|
|
36
145
|
var DATA = 'stx-data';
|
|
37
146
|
var MEDIA = 'stx-media';
|
|
38
147
|
var MEDIA_INDEX = '/__stx_media_index__';
|
|
148
|
+
var LAST_WRITE = '/__stx_last_write__';
|
|
39
149
|
var FRAGMENT = 'X-STX-Router';
|
|
150
|
+
// Stamped on every kept copy: when it was kept, and a fingerprint of its body,
|
|
151
|
+
// so revalidating can tell a changed answer from the same one sent again.
|
|
152
|
+
var KEPT_AT = 'X-STX-Kept-At';
|
|
153
|
+
var KEPT_HASH = 'X-STX-Hash';
|
|
40
154
|
|
|
41
155
|
function variantUrl(url, fragment) {
|
|
42
156
|
var u = new URL(url, self.location.origin);
|
|
@@ -56,10 +170,115 @@ function withTimeout(promise, ms) {
|
|
|
56
170
|
});
|
|
57
171
|
}
|
|
58
172
|
|
|
173
|
+
function hex(buffer) {
|
|
174
|
+
return Array.from(new Uint8Array(buffer)).slice(0, 12).map(function (byte) { return byte.toString(16).padStart(2, '0'); }).join('');
|
|
175
|
+
}
|
|
176
|
+
|
|
59
177
|
function hashOf(text) {
|
|
60
178
|
if (!text) return Promise.resolve('anon');
|
|
61
|
-
return crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)).then(
|
|
62
|
-
|
|
179
|
+
return crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)).then(hex);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Keep the background work of a fetch alive past its answer: the worker may
|
|
183
|
+
// otherwise be stopped before the revalidated copy is stored.
|
|
184
|
+
function keepAlive(event, promise) {
|
|
185
|
+
var settled = promise.catch(function () {});
|
|
186
|
+
try { if (event && event.waitUntil) event.waitUntil(settled); }
|
|
187
|
+
catch (error) {}
|
|
188
|
+
return settled;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Whether this request wants the network before any kept copy: a pull to
|
|
192
|
+
// refresh asks with cache: 'no-cache', and some paths are configured so.
|
|
193
|
+
function wantsNetworkFirst(request, pathname) {
|
|
194
|
+
if (request.cache === 'no-cache' || request.cache === 'reload' || request.cache === 'no-store') return true;
|
|
195
|
+
for (var i = 0; i < S.networkFirst.length; i++) {
|
|
196
|
+
if (pathname.indexOf(S.networkFirst[i]) === 0) return true;
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Only what was asked for, whole and from this origin, is ever kept: not a
|
|
202
|
+
// redirect (to a sign-in page), an error, or a partial answer.
|
|
203
|
+
function keepable(response) {
|
|
204
|
+
return response.status === 200 && !response.redirected && response.type === 'basic';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// A page's body changes on every render where it carries a CSP nonce or a
|
|
208
|
+
// CSRF token. Those are left out of its fingerprint, or every visit would
|
|
209
|
+
// look like new content.
|
|
210
|
+
var VOLATILE = /\\snonce="[^"]*"|<meta\\s+name="csrf-token"[^>]*>/g;
|
|
211
|
+
function fingerprint(response) {
|
|
212
|
+
var type = response.headers.get('Content-Type') || '';
|
|
213
|
+
return response.arrayBuffer().then(function (body) {
|
|
214
|
+
var bytes = body;
|
|
215
|
+
if (type.indexOf('html') !== -1) bytes = new TextEncoder().encode(new TextDecoder().decode(body).replace(VOLATILE, ''));
|
|
216
|
+
return crypto.subtle.digest('SHA-256', bytes).then(function (digest) { return { body: body, hash: hex(digest) }; });
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function stamped(response, body, hash) {
|
|
221
|
+
var headers = new Headers(response.headers);
|
|
222
|
+
// The body read here is already decoded: a compressed answer's encoding and
|
|
223
|
+
// length no longer describe it, and a browser trusting them cut it short.
|
|
224
|
+
headers.delete('Content-Encoding');
|
|
225
|
+
headers.delete('Content-Length');
|
|
226
|
+
headers.set(KEPT_AT, String(Date.now()));
|
|
227
|
+
if (hash) headers.set(KEPT_HASH, hash);
|
|
228
|
+
return new Response(body, { status: response.status, statusText: response.statusText, headers: headers });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Keep a copy under each key, stamped. Resolves with its fingerprint.
|
|
232
|
+
function store(cache, keys, response) {
|
|
233
|
+
return fingerprint(response).then(function (print) {
|
|
234
|
+
return Promise.all(keys.map(function (key) { return cache.put(key, stamped(response, print.body, print.hash)); })).then(function () { return print; });
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function keptHash(hit) {
|
|
239
|
+
var known = hit.headers.get(KEPT_HASH);
|
|
240
|
+
if (known) return Promise.resolve(known);
|
|
241
|
+
// Kept by an earlier worker, before copies were stamped.
|
|
242
|
+
return fingerprint(hit.clone()).then(function (print) { return print.hash; });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// The same content as the kept copy: a matching validator says so without
|
|
246
|
+
// reading the body; otherwise the fingerprints decide.
|
|
247
|
+
function unchanged(hit, response, hash) {
|
|
248
|
+
var a = hit.headers.get('ETag');
|
|
249
|
+
var b = response.headers.get('ETag');
|
|
250
|
+
if (a && b && a === b) return Promise.resolve(true);
|
|
251
|
+
var c = hit.headers.get('Last-Modified');
|
|
252
|
+
var d = response.headers.get('Last-Modified');
|
|
253
|
+
if (c && d && c === d && !a && !b) return Promise.resolve(true);
|
|
254
|
+
return keptHash(hit).then(function (known) { return known === hash; });
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Every open page of the app hears what changed, so it can redraw.
|
|
258
|
+
function announce(message) {
|
|
259
|
+
if (!self.clients || !self.clients.matchAll) return Promise.resolve();
|
|
260
|
+
return self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
|
|
261
|
+
list.forEach(function (client) {
|
|
262
|
+
try { client.postMessage(message); }
|
|
263
|
+
catch (error) {}
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Ask the network behind a kept copy that was already answered. A different
|
|
269
|
+
// answer replaces the copy and is announced; the same one only refreshes
|
|
270
|
+
// when it was kept.
|
|
271
|
+
function revalidate(request, cache, keys, hit, kind) {
|
|
272
|
+
return fetch(request).then(function (response) {
|
|
273
|
+
if (!keepable(response)) return;
|
|
274
|
+
return store(cache, keys, response).then(function (print) {
|
|
275
|
+
return unchanged(hit, response, print.hash).then(function (same) {
|
|
276
|
+
if (same) return;
|
|
277
|
+
var url = new URL(request.url);
|
|
278
|
+
url.hash = '';
|
|
279
|
+
return announce({ type: 'stx:updated', url: url.toString(), kind: kind, fragment: isFragment(request) });
|
|
280
|
+
});
|
|
281
|
+
});
|
|
63
282
|
});
|
|
64
283
|
}
|
|
65
284
|
|
|
@@ -100,14 +319,23 @@ function withParams(response, params) {
|
|
|
100
319
|
// install but never opened online came up offline without its styles.
|
|
101
320
|
var keepingAssets = {};
|
|
102
321
|
var ASSET_REF = /<(?:link|script)\\b[^>]*?\\s(?:href|src)="(\\/[^"]+\\.(?:css|js))"/g;
|
|
322
|
+
function assetRefs(html) {
|
|
323
|
+
var urls = [];
|
|
324
|
+
var match;
|
|
325
|
+
ASSET_REF.lastIndex = 0;
|
|
326
|
+
while ((match = ASSET_REF.exec(html))) {
|
|
327
|
+
if (urls.indexOf(match[1]) === -1) urls.push(match[1]);
|
|
328
|
+
}
|
|
329
|
+
return urls;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function keepAsset(cache, request, response) {
|
|
333
|
+
return response.blob().then(function (body) { return cache.put(request, stamped(response, body, null)); });
|
|
334
|
+
}
|
|
335
|
+
|
|
103
336
|
function keepAssetsOf(response) {
|
|
104
337
|
return response.text().then(function (html) {
|
|
105
|
-
var urls =
|
|
106
|
-
var match;
|
|
107
|
-
ASSET_REF.lastIndex = 0;
|
|
108
|
-
while ((match = ASSET_REF.exec(html))) {
|
|
109
|
-
if (urls.indexOf(match[1]) === -1) urls.push(match[1]);
|
|
110
|
-
}
|
|
338
|
+
var urls = assetRefs(html);
|
|
111
339
|
return caches.open(ASSETS).then(function (cache) {
|
|
112
340
|
return Promise.all(urls.map(function (url) {
|
|
113
341
|
// A page and its fragment link the same files: one fetch each.
|
|
@@ -115,7 +343,7 @@ function keepAssetsOf(response) {
|
|
|
115
343
|
keepingAssets[url] = cache.match(url).then(function (hit) {
|
|
116
344
|
if (hit) return;
|
|
117
345
|
return fetch(url, { credentials: 'same-origin' }).then(function (asset) {
|
|
118
|
-
if (asset.status === 200) return cache
|
|
346
|
+
if (asset.status === 200) return keepAsset(cache, url, asset);
|
|
119
347
|
});
|
|
120
348
|
}).catch(function () {});
|
|
121
349
|
}
|
|
@@ -135,23 +363,81 @@ function precache() {
|
|
|
135
363
|
return fetch(page.url, { headers: headers, credentials: 'same-origin' }).then(function (response) {
|
|
136
364
|
if (response.status !== 200 || response.redirected) return;
|
|
137
365
|
var copy = response.clone();
|
|
138
|
-
return Promise.all([cache
|
|
366
|
+
return Promise.all([store(cache, [page.key(fragment)], response), keepAssetsOf(copy)]);
|
|
139
367
|
}).catch(function () {});
|
|
140
368
|
}));
|
|
141
369
|
}));
|
|
142
370
|
});
|
|
143
371
|
}
|
|
144
372
|
|
|
373
|
+
// Stylesheets and scripts no kept screen of this build links to, and that
|
|
374
|
+
// nothing fetched lately, are an earlier build's: dropped when this build
|
|
375
|
+
// takes over. Other files (images, fonts) are capped, least recently fetched
|
|
376
|
+
// first. Fetching refreshes a file's stamp, so one in use is never the oldest.
|
|
377
|
+
var ASSET_GRACE_MS = 24 * 60 * 60 * 1000;
|
|
378
|
+
function pruneAssets() {
|
|
379
|
+
return Promise.all([caches.open(SHELL), caches.open(ASSETS)]).then(function (opened) {
|
|
380
|
+
var shell = opened[0];
|
|
381
|
+
var assets = opened[1];
|
|
382
|
+
return shell.keys().then(function (pages) {
|
|
383
|
+
return Promise.all(pages.map(function (page) {
|
|
384
|
+
return shell.match(page).then(function (hit) { return hit ? hit.text() : ''; }).catch(function () { return ''; });
|
|
385
|
+
}));
|
|
386
|
+
}).then(function (htmls) {
|
|
387
|
+
var linked = {};
|
|
388
|
+
htmls.forEach(function (html) {
|
|
389
|
+
assetRefs(html).forEach(function (url) { linked[new URL(url, self.location.origin).toString()] = true; });
|
|
390
|
+
});
|
|
391
|
+
return assets.keys().then(function (requests) {
|
|
392
|
+
return Promise.all(requests.map(function (request) {
|
|
393
|
+
return assets.match(request).then(function (hit) { return { request: request, at: hit ? Number(hit.headers.get(KEPT_AT) || 0) : 0 }; });
|
|
394
|
+
}));
|
|
395
|
+
}).then(function (entries) {
|
|
396
|
+
var now = Date.now();
|
|
397
|
+
var drops = [];
|
|
398
|
+
var others = [];
|
|
399
|
+
entries.forEach(function (entry) {
|
|
400
|
+
var url = entry.request.url;
|
|
401
|
+
if (/\\.(?:css|js)$/.test(new URL(url).pathname)) {
|
|
402
|
+
// With no screen kept there is nothing to judge by: keep them.
|
|
403
|
+
if (htmls.length && !linked[url] && now - entry.at > ASSET_GRACE_MS) drops.push(entry.request);
|
|
404
|
+
}
|
|
405
|
+
else others.push(entry);
|
|
406
|
+
});
|
|
407
|
+
if (others.length > S.assetsMax) {
|
|
408
|
+
others.sort(function (a, b) { return a.at - b.at; });
|
|
409
|
+
others.slice(0, others.length - S.assetsMax).forEach(function (entry) { drops.push(entry.request); });
|
|
410
|
+
}
|
|
411
|
+
return Promise.all(drops.map(function (request) { return assets.delete(request); }));
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
}).catch(function () {});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// A new build waits for the one running to be let go (see 'message'), rather
|
|
418
|
+
// than taking over under a page that is on screen: its fragments would not
|
|
419
|
+
// match the page's build, and the router would reload under the person.
|
|
145
420
|
self.addEventListener('install', function (event) {
|
|
146
|
-
event.waitUntil(precache()
|
|
421
|
+
event.waitUntil(precache());
|
|
147
422
|
});
|
|
148
423
|
|
|
149
424
|
self.addEventListener('activate', function (event) {
|
|
150
425
|
event.waitUntil(caches.keys().then(function (keys) {
|
|
151
426
|
return Promise.all(keys.filter(function (key) { return key.indexOf('stx-shell-') === 0 && key !== SHELL; }).map(function (key) { return caches.delete(key); }));
|
|
152
|
-
}).then(function () { return self.clients.claim(); }));
|
|
427
|
+
}).then(pruneAssets).then(function () { return self.clients.claim(); }));
|
|
153
428
|
});
|
|
154
429
|
|
|
430
|
+
// When a page may let this build take over: asked outright (an "Update"
|
|
431
|
+
// button), the app starting cold, or no page of the app on screen.
|
|
432
|
+
function mayTakeOver(data) {
|
|
433
|
+
if (data.force || data.reason === 'cold-start') return Promise.resolve(true);
|
|
434
|
+
if (!self.clients || !self.clients.matchAll) return Promise.resolve(false);
|
|
435
|
+
return self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
|
|
436
|
+
if (data.reason === 'load' && list.length <= 1) return true;
|
|
437
|
+
return !list.some(function (client) { return client.visibilityState === 'visible'; });
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
155
441
|
// --- Media kept for offline: videos a page asked for (cacheMedia()) ---
|
|
156
442
|
// Stored whole and answered here, ranges included: a player seeks with Range
|
|
157
443
|
// requests, and the network may not be there to answer them. The index (url
|
|
@@ -243,8 +529,14 @@ function mediaResponse(request) {
|
|
|
243
529
|
self.addEventListener('message', function (event) {
|
|
244
530
|
var data = event.data || {};
|
|
245
531
|
if (data.type === 'stx:clear-offline-data') {
|
|
532
|
+
lastWrite = null;
|
|
246
533
|
event.waitUntil(Promise.all([caches.delete(DATA), caches.delete(MEDIA)]));
|
|
247
534
|
}
|
|
535
|
+
if (data.type === 'stx:activate-update') {
|
|
536
|
+
// Sent to the waiting worker. One already running ignores it: skipWaiting
|
|
537
|
+
// does nothing once active.
|
|
538
|
+
event.waitUntil(mayTakeOver(data).then(function (yes) { if (yes) return self.skipWaiting(); }));
|
|
539
|
+
}
|
|
248
540
|
if (data.type === 'stx:cache-media' && Array.isArray(data.urls)) {
|
|
249
541
|
var reply = event.ports && event.ports[0];
|
|
250
542
|
event.waitUntil(keepMedia(data.urls.filter(function (url) { return typeof url === 'string'; })).then(function (kept) {
|
|
@@ -270,49 +562,71 @@ function markFallback(response) {
|
|
|
270
562
|
});
|
|
271
563
|
}
|
|
272
564
|
|
|
273
|
-
|
|
565
|
+
// The app starting cold (a navigation with no other page of it open) while a
|
|
566
|
+
// new build waits: the new build takes over now, and this navigation goes to
|
|
567
|
+
// the network so it opens on the new build rather than the kept old one.
|
|
568
|
+
function coldStartUpdate(request) {
|
|
569
|
+
var registration = self.registration;
|
|
570
|
+
if (request.mode !== 'navigate' || !registration || !registration.waiting || !self.clients || !self.clients.matchAll) return Promise.resolve(false);
|
|
571
|
+
return self.clients.matchAll({ type: 'window' }).then(function (list) {
|
|
572
|
+
if (list.length > 1 || !registration.waiting) return false;
|
|
573
|
+
registration.waiting.postMessage({ type: 'stx:activate-update', reason: 'cold-start' });
|
|
574
|
+
return true;
|
|
575
|
+
}).catch(function () { return false; });
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function pageResponse(request, event) {
|
|
274
579
|
var fragment = isFragment(request);
|
|
275
580
|
var key = variantUrl(request.url, fragment);
|
|
276
581
|
var dynamic = routeOf(new URL(request.url).pathname);
|
|
277
|
-
var
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
582
|
+
var keys = dynamic ? [key, routeKey(dynamic.route, fragment)] : [key];
|
|
583
|
+
return Promise.all([caches.open(SHELL), coldStartUpdate(request)]).then(function (opened) {
|
|
584
|
+
var cache = opened[0];
|
|
585
|
+
var networkFirst = opened[1] || wantsNetworkFirst(request, new URL(request.url).pathname);
|
|
586
|
+
return cache.match(key).then(function (hit) {
|
|
587
|
+
if (hit && !networkFirst) {
|
|
588
|
+
keepAlive(event, revalidate(request, cache, keys, hit, 'page'));
|
|
589
|
+
return hit;
|
|
590
|
+
}
|
|
591
|
+
var network = fetch(request).then(function (response) {
|
|
592
|
+
if (keepable(response)) keepAlive(event, store(cache, keys, response.clone()));
|
|
593
|
+
return response;
|
|
594
|
+
});
|
|
595
|
+
// What stands in when the network is not there, or slow: the kept copy,
|
|
596
|
+
// then a dynamic route's kept page, then the fallback screen.
|
|
597
|
+
var standIn = function (offline) {
|
|
598
|
+
if (hit) return Promise.resolve(hit);
|
|
599
|
+
var route = dynamic ? cache.match(routeKey(dynamic.route, fragment)) : Promise.resolve(null);
|
|
600
|
+
return route.then(function (kept) {
|
|
601
|
+
if (kept) return withParams(kept, dynamic.params);
|
|
602
|
+
if (!offline) return null;
|
|
603
|
+
if (!S.fallback || fragment) return Response.error();
|
|
604
|
+
return cache.match(variantUrl(S.fallback, false)).then(function (fallback) { return fallback ? markFallback(fallback) : Response.error(); });
|
|
605
|
+
});
|
|
606
|
+
};
|
|
607
|
+
return withTimeout(network, S.timeout).catch(function () {
|
|
608
|
+
return standIn(false).then(function (kept) {
|
|
609
|
+
if (kept) {
|
|
610
|
+
keepAlive(event, network);
|
|
611
|
+
return kept;
|
|
612
|
+
}
|
|
613
|
+
// Slower than the timeout, nothing kept to show: wait for it rather than fail.
|
|
614
|
+
return network.catch(function () { return standIn(true); });
|
|
301
615
|
});
|
|
302
616
|
});
|
|
303
617
|
});
|
|
304
618
|
});
|
|
305
619
|
}
|
|
306
620
|
|
|
307
|
-
function assetResponse(request) {
|
|
621
|
+
function assetResponse(request, event) {
|
|
308
622
|
return caches.open(ASSETS).then(function (cache) {
|
|
309
623
|
return cache.match(request).then(function (hit) {
|
|
310
624
|
var network = fetch(request).then(function (response) {
|
|
311
|
-
if (response
|
|
625
|
+
if (keepable(response)) keepAlive(event, keepAsset(cache, request, response.clone()));
|
|
312
626
|
return response;
|
|
313
627
|
});
|
|
314
628
|
if (hit) {
|
|
315
|
-
|
|
629
|
+
keepAlive(event, network);
|
|
316
630
|
return hit;
|
|
317
631
|
}
|
|
318
632
|
return network;
|
|
@@ -320,21 +634,43 @@ function assetResponse(request) {
|
|
|
320
634
|
});
|
|
321
635
|
}
|
|
322
636
|
|
|
323
|
-
|
|
637
|
+
// When the app last wrote to its API (any request but a GET). A copy kept
|
|
638
|
+
// before it is not answered at once: it would show the screen as if the
|
|
639
|
+
// write had not happened. Kept in the data cache so a restarted worker knows.
|
|
640
|
+
var lastWrite = null;
|
|
641
|
+
function lastWriteAt() {
|
|
642
|
+
if (!lastWrite) {
|
|
643
|
+
lastWrite = caches.open(DATA).then(function (cache) { return cache.match(LAST_WRITE); })
|
|
644
|
+
.then(function (hit) { return hit ? hit.text() : '0'; })
|
|
645
|
+
.then(function (text) { return Number(text) || 0; })
|
|
646
|
+
.catch(function () { return 0; });
|
|
647
|
+
}
|
|
648
|
+
return lastWrite;
|
|
649
|
+
}
|
|
650
|
+
function noteWrite() {
|
|
651
|
+
var at = Date.now();
|
|
652
|
+
lastWrite = Promise.resolve(at);
|
|
653
|
+
return caches.open(DATA).then(function (cache) { return cache.put(LAST_WRITE, new Response(String(at))); }).catch(function () {});
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function apiResponse(request, event) {
|
|
324
657
|
return hashOf(request.headers.get('Authorization') || '').then(function (who) {
|
|
325
658
|
var u = new URL(request.url);
|
|
326
659
|
u.searchParams.set('__stx_who', who);
|
|
327
660
|
var key = u.toString();
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
661
|
+
return Promise.all([caches.open(DATA), lastWriteAt()]).then(function (opened) {
|
|
662
|
+
var cache = opened[0];
|
|
663
|
+
return cache.match(key).then(function (hit) {
|
|
664
|
+
var stale = hit && opened[1] > 0 && Number(hit.headers.get(KEPT_AT) || 0) <= opened[1];
|
|
665
|
+
if (hit && !stale && !wantsNetworkFirst(request, u.pathname)) {
|
|
666
|
+
keepAlive(event, revalidate(request, cache, [key], hit, 'api'));
|
|
667
|
+
return hit;
|
|
668
|
+
}
|
|
669
|
+
var network = fetch(request).then(function (response) {
|
|
670
|
+
if (keepable(response)) keepAlive(event, store(cache, [key], response.clone()));
|
|
671
|
+
return response;
|
|
672
|
+
});
|
|
673
|
+
return withTimeout(network, S.timeout).catch(function () {
|
|
338
674
|
return hit || network;
|
|
339
675
|
});
|
|
340
676
|
});
|
|
@@ -344,9 +680,13 @@ function apiResponse(request) {
|
|
|
344
680
|
|
|
345
681
|
self.addEventListener('fetch', function (event) {
|
|
346
682
|
var request = event.request;
|
|
347
|
-
if (request.method !== 'GET') return;
|
|
348
683
|
var url = new URL(request.url);
|
|
349
684
|
if (url.origin !== self.location.origin) return;
|
|
685
|
+
if (request.method !== 'GET') {
|
|
686
|
+
// Writes go straight to the network; only when they happened is noted.
|
|
687
|
+
if (url.pathname.indexOf(S.api) === 0) keepAlive(event, noteWrite());
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
350
690
|
// Media kept for offline is answered from the device, a range included;
|
|
351
691
|
// anything else asking for a range is the browser's to answer, since a
|
|
352
692
|
// cache cannot hold a partial response.
|
|
@@ -358,16 +698,16 @@ self.addEventListener('fetch', function (event) {
|
|
|
358
698
|
if (url.pathname.indexOf(S.exclude[i]) === 0) return;
|
|
359
699
|
}
|
|
360
700
|
if (url.pathname.indexOf(S.api) === 0) {
|
|
361
|
-
event.respondWith(apiResponse(request));
|
|
701
|
+
event.respondWith(apiResponse(request, event));
|
|
362
702
|
return;
|
|
363
703
|
}
|
|
364
704
|
if (url.pathname.indexOf('/_stx/') === 0 || url.pathname.indexOf('/assets/') === 0 || /\\.(?:js|css|woff2?|png|jpe?g|webp|avif|svg|ico)$/.test(url.pathname)) {
|
|
365
|
-
event.respondWith(assetResponse(request));
|
|
705
|
+
event.respondWith(assetResponse(request, event));
|
|
366
706
|
return;
|
|
367
707
|
}
|
|
368
708
|
var accept = request.headers.get('Accept') || '';
|
|
369
709
|
if (request.mode === 'navigate' || isFragment(request) || accept.indexOf('text/html') !== -1) {
|
|
370
|
-
event.respondWith(pageResponse(request));
|
|
710
|
+
event.respondWith(pageResponse(request, event));
|
|
371
711
|
}
|
|
372
712
|
});
|
|
373
713
|
`;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where kept values live: `keptState` and persisted queries.
|
|
3
|
+
*
|
|
4
|
+
* A phone app opens on last time's data, so a kept value has to be there
|
|
5
|
+
* synchronously when a screen first draws. localStorage is, but every write
|
|
6
|
+
* to it is a synchronous disk write on the main thread, and an API answer of a
|
|
7
|
+
* few hundred kilobytes (a month of calendar, a year of fitness data) written
|
|
8
|
+
* on every refresh stalled scrolling, and past ~5 MB it is refused outright.
|
|
9
|
+
*
|
|
10
|
+
* So a value is kept in three places, each for what it is good at:
|
|
11
|
+
*
|
|
12
|
+
* - memory: what every read after the first answers from;
|
|
13
|
+
* - localStorage, for small values: read synchronously on the first draw;
|
|
14
|
+
* - IndexedDB, for large ones: written off the main thread's critical path,
|
|
15
|
+
* coalesced, and read back asynchronously (a few milliseconds, well before
|
|
16
|
+
* any network answer).
|
|
17
|
+
*
|
|
18
|
+
* Values written before this existed sit in localStorage under the same key
|
|
19
|
+
* and are read from there, so nothing kept is lost; a large one moves to
|
|
20
|
+
* IndexedDB the next time it is written. Where IndexedDB is not available
|
|
21
|
+
* (some private modes, an old web view) everything stays in localStorage, and
|
|
22
|
+
* where neither is, in memory.
|
|
23
|
+
*
|
|
24
|
+
* `createKeptStore` is closure-free: the browser runtime embeds its source
|
|
25
|
+
* (signals.ts), so it may use nothing from this module's scope.
|
|
26
|
+
*/
|
|
27
|
+
/** An asynchronous key-value store behind localStorage, for large values. */
|
|
28
|
+
export interface KeptBackend {
|
|
29
|
+
get: (key: string) => Promise<unknown>;
|
|
30
|
+
set: (key: string, value: unknown) => Promise<void>;
|
|
31
|
+
remove: (key: string) => Promise<void>;
|
|
32
|
+
/** Remove every key starting with this prefix. */
|
|
33
|
+
removePrefix: (prefix: string) => Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export interface KeptStoreOptions {
|
|
36
|
+
/** Synchronous storage for small values. Default localStorage; null for none. */
|
|
37
|
+
storage?: Pick<Storage, 'getItem' | 'setItem' | 'removeItem' | 'key' | 'length'> | null;
|
|
38
|
+
/** Asynchronous storage for large values. Default IndexedDB where there is one; null for none. */
|
|
39
|
+
backend?: KeptBackend | null;
|
|
40
|
+
/** Characters of JSON past which a value goes to the backend rather than localStorage. Default 32768. */
|
|
41
|
+
limit?: number;
|
|
42
|
+
/** Defer a backend write (default: when idle, or 200 ms at the latest). */
|
|
43
|
+
defer?: (fn: () => void) => void;
|
|
44
|
+
}
|
|
45
|
+
export interface KeptStore {
|
|
46
|
+
/** The value kept under `key`, synchronously: from memory, or localStorage. */
|
|
47
|
+
peek: (key: string) => {
|
|
48
|
+
value: unknown;
|
|
49
|
+
} | undefined;
|
|
50
|
+
/** The value kept under `key`, wherever it is, IndexedDB included. */
|
|
51
|
+
load: (key: string) => Promise<{
|
|
52
|
+
value: unknown;
|
|
53
|
+
} | undefined>;
|
|
54
|
+
/** Keep a value. Readable from memory at once; stored behind. */
|
|
55
|
+
set: (key: string, value: unknown) => void;
|
|
56
|
+
/** Forget every key that starts with `prefix`. */
|
|
57
|
+
forget: (prefix: string) => Promise<void>;
|
|
58
|
+
/** Finish every write that is waiting. */
|
|
59
|
+
flush: () => Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
/** IndexedDB as a KeptBackend, or null where there is none. Closure-free. */
|
|
62
|
+
export declare function indexedDbBackend(name?: string): KeptBackend | null;
|
|
63
|
+
/**
|
|
64
|
+
* A kept store. Closure-free: embedded in the browser runtime by source, with
|
|
65
|
+
* `indexedDbBackend` passed in rather than referenced.
|
|
66
|
+
*/
|
|
67
|
+
export declare function createKeptStore(options?: KeptStoreOptions, openBackend?: () => KeptBackend | null): KeptStore;
|