@oxy.so/app-preset 6.7.2 → 6.7.3
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 -2
- package/package.json +1 -1
- package/web/oxy-sw-base.js +111 -18
- package/web/oxy-sw-register.js +16 -0
package/README.md
CHANGED
|
@@ -109,9 +109,13 @@ passes it instead: `oxy-web-pwa dist --theme-color=#f9f2f6,#100d11` (hex or a
|
|
|
109
109
|
colour name; a page that already has a `theme-color` keeps it).
|
|
110
110
|
|
|
111
111
|
The base worker (`web/oxy-sw-base.js`, from Inbosh's) answers navigations
|
|
112
|
-
network-first with the one
|
|
112
|
+
network-first with the one shell (fetched from `/`, stored under `/index.html`,
|
|
113
|
+
never as a redirected response, which Chrome refuses for a navigation) as the
|
|
114
|
+
offline fallback, and
|
|
113
115
|
same-origin static files (`/_expo/`, `/assets/`, images, fonts, JS, CSS)
|
|
114
|
-
stale-while-revalidate.
|
|
116
|
+
stale-while-revalidate. On install it stores the shell's bundles and CSS, and
|
|
117
|
+
the registration sends it the files the page loaded before it took control,
|
|
118
|
+
so the app works offline from the first visit. It never stores or serves anything else: cross-origin
|
|
115
119
|
requests (every Oxy API), non-GET, `/api/`, requests carrying `Authorization`
|
|
116
120
|
or `Range`, and `private`/`no-store` responses.
|
|
117
121
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxy.so/app-preset",
|
|
3
|
-
"version": "6.7.
|
|
3
|
+
"version": "6.7.3",
|
|
4
4
|
"description": "The Oxy distro of Expo — centralized Expo config plugin, Metro/Babel/ESLint factories, Tailwind base CSS and TypeScript bases shared by every Oxy app (Mention, Homiio, Allo, accounts, …). A zero-build package: apps replace copy-pasted config with a single dependency bump.",
|
|
5
5
|
"main": "./app.plugin.js",
|
|
6
6
|
"publishConfig": {
|
package/web/oxy-sw-base.js
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
* Strategies (ported from Inbosh's worker, which paid for each rule):
|
|
12
12
|
*
|
|
13
13
|
* - Navigation: network-first, so a deploy is visible on the next load. The
|
|
14
|
-
* one SPA shell is stored once under `/index.html` and
|
|
14
|
+
* one SPA shell, fetched from `/`, is stored once under `/index.html` and
|
|
15
|
+
* served offline, always as a response that went through no redirect.
|
|
15
16
|
* - Same-origin static files (`/_expo/`, `/assets/`, images, fonts, JS, CSS):
|
|
16
17
|
* stale-while-revalidate. Metro hashes these names, so an entry is never
|
|
17
18
|
* stale in content, and chunks of the previous deploy stay loadable for a
|
|
@@ -54,14 +55,91 @@
|
|
|
54
55
|
|
|
55
56
|
const offline = () => new Response('Offline', { status: 503, statusText: 'Service Unavailable' });
|
|
56
57
|
|
|
58
|
+
/**
|
|
59
|
+
* A response a navigation can be answered with. Chrome refuses a response
|
|
60
|
+
* that went through a redirect for a navigation (`net::ERR_FAILED`), and
|
|
61
|
+
* hosts redirect: Cloudflare's static assets send `/index.html` to `/` with
|
|
62
|
+
* a 307. So a stored shell is always rebuilt as a plain response.
|
|
63
|
+
*/
|
|
64
|
+
function servable(response) {
|
|
65
|
+
if (!response?.redirected) return response;
|
|
66
|
+
return new Response(response.body, {
|
|
67
|
+
status: response.status,
|
|
68
|
+
statusText: response.statusText,
|
|
69
|
+
headers: response.headers,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A same-origin static file this worker stores: a bundle, stylesheet, font or image. */
|
|
74
|
+
function isStatic(url) {
|
|
75
|
+
if (url.origin !== self.location.origin || url.pathname.startsWith('/api/')) return false;
|
|
76
|
+
return (
|
|
77
|
+
url.pathname.startsWith('/_expo/') ||
|
|
78
|
+
url.pathname.startsWith('/assets/') ||
|
|
79
|
+
STATIC_FILE.test(url.pathname)
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The static files the shell's own tags load (its bundles and CSS). */
|
|
84
|
+
function shellAssets(html) {
|
|
85
|
+
const urls = new Set();
|
|
86
|
+
for (const [, src] of html.matchAll(/<(?:script|link)\b[^>]*?\s(?:src|href)="([^"]+)"/gi)) {
|
|
87
|
+
const url = new URL(src, self.location.origin);
|
|
88
|
+
if (isStatic(url)) urls.add(url.href);
|
|
89
|
+
}
|
|
90
|
+
return [...urls];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Stores each of `urls` that is a static file and not stored yet; a failure is skipped. */
|
|
94
|
+
async function storeAssets(cache, urls) {
|
|
95
|
+
const wanted = [
|
|
96
|
+
...new Set(
|
|
97
|
+
urls
|
|
98
|
+
.map((url) => {
|
|
99
|
+
try {
|
|
100
|
+
return new URL(url, self.location.origin);
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
.filter((url) => url && isStatic(url))
|
|
106
|
+
.map((url) => url.href),
|
|
107
|
+
),
|
|
108
|
+
].slice(0, MAX_ENTRIES);
|
|
109
|
+
await Promise.all(
|
|
110
|
+
wanted.map(async (url) => {
|
|
111
|
+
try {
|
|
112
|
+
if (await cache.match(url)) return;
|
|
113
|
+
const asset = await fetch(url);
|
|
114
|
+
if (cacheable(asset)) await cache.put(url, asset);
|
|
115
|
+
} catch {
|
|
116
|
+
// A file left out is fetched (and stored) the next time the page asks for it.
|
|
117
|
+
}
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
await trim(cache);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Stores the shell from `/` (the URL the host serves it at) and the bundles
|
|
125
|
+
* it loads. A first visit fetches those before this worker controls the page,
|
|
126
|
+
* so without this the app would only work offline from the second visit on.
|
|
127
|
+
*/
|
|
128
|
+
async function storeShell() {
|
|
129
|
+
const response = await fetch('/', { cache: 'reload' });
|
|
130
|
+
if (!cacheable(response) || !(response.headers.get('content-type') || '').includes('text/html'))
|
|
131
|
+
return;
|
|
132
|
+
const html = await response.clone().text();
|
|
133
|
+
const cache = await caches.open(CACHE_NAME);
|
|
134
|
+
await cache.put(SHELL_KEY, servable(response));
|
|
135
|
+
await storeAssets(cache, shellAssets(html));
|
|
136
|
+
}
|
|
137
|
+
|
|
57
138
|
self.addEventListener('install', (event) => {
|
|
58
139
|
event.waitUntil(
|
|
59
|
-
|
|
60
|
-
.
|
|
61
|
-
|
|
62
|
-
.catch(() => {
|
|
63
|
-
// Non-fatal: the shell is stored on the first navigation instead.
|
|
64
|
-
}),
|
|
140
|
+
storeShell().catch(() => {
|
|
141
|
+
// Non-fatal: the shell is stored on the first navigation instead.
|
|
142
|
+
}),
|
|
65
143
|
);
|
|
66
144
|
self.skipWaiting();
|
|
67
145
|
});
|
|
@@ -81,8 +159,16 @@
|
|
|
81
159
|
);
|
|
82
160
|
});
|
|
83
161
|
|
|
162
|
+
/**
|
|
163
|
+
* `OXY_SW_STORE`: the files a page loaded before this worker controlled it
|
|
164
|
+
* (its route chunks, fonts), which `oxy-sw-register.js` sends when the worker
|
|
165
|
+
* takes over. Only static same-origin files are stored, whatever is sent.
|
|
166
|
+
*/
|
|
84
167
|
self.addEventListener('message', (event) => {
|
|
85
|
-
|
|
168
|
+
const data = event.data;
|
|
169
|
+
if (data?.type === 'SKIP_WAITING') self.skipWaiting();
|
|
170
|
+
if (data?.type === 'OXY_SW_STORE' && Array.isArray(data.urls))
|
|
171
|
+
event.waitUntil(caches.open(CACHE_NAME).then((cache) => storeAssets(cache, data.urls)));
|
|
86
172
|
});
|
|
87
173
|
|
|
88
174
|
/** `'navigation'`, `'static'`, or `null` for a request this worker leaves alone. */
|
|
@@ -93,13 +179,7 @@
|
|
|
93
179
|
if (url.pathname.startsWith('/api/')) return null;
|
|
94
180
|
if (request.headers.has('authorization') || request.headers.has('range')) return null;
|
|
95
181
|
if (request.mode === 'navigate') return 'navigation';
|
|
96
|
-
|
|
97
|
-
url.pathname.startsWith('/_expo/') ||
|
|
98
|
-
url.pathname.startsWith('/assets/') ||
|
|
99
|
-
STATIC_FILE.test(url.pathname)
|
|
100
|
-
)
|
|
101
|
-
return 'static';
|
|
102
|
-
return null;
|
|
182
|
+
return isStatic(url) ? 'static' : null;
|
|
103
183
|
}
|
|
104
184
|
|
|
105
185
|
/** Only a complete, same-origin, storable response is ever cached. */
|
|
@@ -127,12 +207,13 @@
|
|
|
127
207
|
(response.headers.get('content-type') || '').includes('text/html')
|
|
128
208
|
) {
|
|
129
209
|
const cache = await caches.open(CACHE_NAME);
|
|
130
|
-
await cache.put(SHELL_KEY, response.clone());
|
|
210
|
+
await cache.put(SHELL_KEY, servable(response.clone()));
|
|
131
211
|
}
|
|
132
212
|
return response;
|
|
133
213
|
} catch {
|
|
134
214
|
const cache = await caches.open(CACHE_NAME);
|
|
135
|
-
|
|
215
|
+
// servable() again: a shell stored by 6.7.2 or earlier may be a redirected one.
|
|
216
|
+
return servable(await cache.match(SHELL_KEY)) || offline();
|
|
136
217
|
}
|
|
137
218
|
}
|
|
138
219
|
|
|
@@ -161,5 +242,17 @@
|
|
|
161
242
|
else if (strategy === 'static') event.respondWith(staleWhileRevalidate(event));
|
|
162
243
|
});
|
|
163
244
|
|
|
164
|
-
self.__oxySwBaseInternals = {
|
|
245
|
+
self.__oxySwBaseInternals = {
|
|
246
|
+
strategyFor,
|
|
247
|
+
cacheable,
|
|
248
|
+
servable,
|
|
249
|
+
shellAssets,
|
|
250
|
+
storeAssets,
|
|
251
|
+
navigation,
|
|
252
|
+
storeShell,
|
|
253
|
+
trim,
|
|
254
|
+
CACHE_NAME,
|
|
255
|
+
SHELL_KEY,
|
|
256
|
+
MAX_ENTRIES,
|
|
257
|
+
};
|
|
165
258
|
})();
|
package/web/oxy-sw-register.js
CHANGED
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
* so the page's state is checked first. Failure is silent: the app works
|
|
10
10
|
* without a worker.
|
|
11
11
|
*
|
|
12
|
+
* First visit: the page loads its bundles and route chunks before the worker
|
|
13
|
+
* controls it, so none of them goes through the worker's cache. When the worker
|
|
14
|
+
* takes over, this sends it the same-origin files the page has loaded so far
|
|
15
|
+
* (`OXY_SW_STORE`), and the app works offline from the first visit.
|
|
16
|
+
*
|
|
12
17
|
* Updates: `updateViaCache: 'none'` makes every update check fetch `/sw.js` and
|
|
13
18
|
* `/oxy-sw-base.js` past the HTTP cache. The browser checks on each navigation,
|
|
14
19
|
* but an installed app can stay open for days without one, so a page that
|
|
@@ -18,6 +23,17 @@
|
|
|
18
23
|
if (!('serviceWorker' in navigator) || !window.isSecureContext) return;
|
|
19
24
|
const HOUR = 60 * 60 * 1000;
|
|
20
25
|
let checked = Date.now();
|
|
26
|
+
const sendLoaded = () => {
|
|
27
|
+
const worker = navigator.serviceWorker.controller;
|
|
28
|
+
if (!worker || typeof performance === 'undefined') return;
|
|
29
|
+
const urls = performance
|
|
30
|
+
.getEntriesByType('resource')
|
|
31
|
+
.map((entry) => entry.name)
|
|
32
|
+
.filter((name) => name.startsWith(`${location.origin}/`));
|
|
33
|
+
worker.postMessage({ type: 'OXY_SW_STORE', urls });
|
|
34
|
+
};
|
|
35
|
+
if (!navigator.serviceWorker.controller)
|
|
36
|
+
navigator.serviceWorker.addEventListener('controllerchange', sendLoaded, { once: true });
|
|
21
37
|
const register = () => {
|
|
22
38
|
navigator.serviceWorker
|
|
23
39
|
.register('/sw.js', { updateViaCache: 'none' })
|