@oxy.so/app-preset 6.7.1 → 6.7.2

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 CHANGED
@@ -100,7 +100,8 @@ The app keeps its own `public/manifest.json` and icons. The step:
100
100
  app has no `public/sw.js`. An app with its own worker (web push) keeps it and
101
101
  opts in with `importScripts('/oxy-sw-base.js');` as its first line; until it
102
102
  does, the step warns. A worker that already answers `fetch` (Inbosh, Nilo)
103
- must not import it. `--no-sw` ships no worker.
103
+ must not import it. `--no-sw` ships no worker; `--remove-sw` retires one
104
+ shipped before (see Cleanup below).
104
105
 
105
106
  `theme-color` comes from the manifest's `theme_color`, one tag. An app whose
106
107
  startup colour is a light/dark pair (Bloom adopts one and owns it at runtime)
@@ -112,12 +113,28 @@ network-first with the one `/index.html` shell as the offline fallback, and
112
113
  same-origin static files (`/_expo/`, `/assets/`, images, fonts, JS, CSS)
113
114
  stale-while-revalidate. It never stores or serves anything else: cross-origin
114
115
  requests (every Oxy API), non-GET, `/api/`, requests carrying `Authorization`
115
- or `Range`, and `private`/`no-store` responses. Its
116
- cache name is a hash of the export, so each deploy installs a new worker and
117
- drops the old cache; the cache holds at most 150 entries.
118
-
119
- Serve `/sw.js` with `Cache-Control: no-cache` (`public/_headers`). `@oxy.so/doctor`
120
- 1.10.0+ warns (`web-not-installable`) about a web export without this step.
116
+ or `Range`, and `private`/`no-store` responses.
117
+
118
+ **Updates.** `oxy-web-pwa` stamps `/oxy-sw-base.js` with a hash of the export,
119
+ so every deploy changes its bytes and the browser installs the new worker, which
120
+ takes over at once. The registration uses `updateViaCache: 'none'`, and a page
121
+ that returns to the foreground checks for an update at most once an hour (an
122
+ installed app can stay open for days without a navigation). App code is never
123
+ stale: navigations are network-first, so the next load runs the new deploy.
124
+
125
+ **Cleanup.** One cache, `oxy-sw-v1`, holds at most 150 entries, oldest dropped
126
+ first and the shell never. It is not renamed per deploy: hashed chunks never
127
+ collide, so a tab opened before a deploy can still lazy-load the chunks the
128
+ server has already dropped. `activate` deletes every other `oxy-sw-*` cache, so
129
+ bumping the name in the base worker clears old entries everywhere on the next
130
+ deploy. To take the worker away, build with `--remove-sw`: it ships a `/sw.js`
131
+ that deletes the Oxy caches and unregisters itself (shipping no `/sw.js` would
132
+ leave the old worker running, since a failed update check keeps it).
133
+
134
+ Serve `/sw.js` and `/oxy-sw-base.js` with `Cache-Control: no-cache` or
135
+ `max-age=0, must-revalidate` (Cloudflare's default for unhashed assets).
136
+ `@oxy.so/doctor` 1.10.0+ warns (`web-not-installable`) about a web export
137
+ without this step.
121
138
 
122
139
  ## Usage
123
140
 
@@ -5,14 +5,15 @@
5
5
  *
6
6
  * expo export --platform web --output-dir dist && oxy-web-pwa dist
7
7
  *
8
- * oxy-web-pwa [dist] [--no-sw] [--theme-color=<light>,<dark>]
8
+ * oxy-web-pwa [dist] [--no-sw | --remove-sw] [--theme-color=<light>,<dark>]
9
9
  *
10
10
  * `dist` defaults to `dist`. The step checks the exported manifest
11
11
  * (public/manifest.json) against what browsers need to offer installation,
12
12
  * adds the manifest link, theme colour, Apple tags and the worker registration
13
13
  * to every exported page that lacks them, and ships the Oxy base service
14
14
  * worker (`/oxy-sw-base.js`, plus `/sw.js` when the app has none).
15
- * `--no-sw` skips the worker. `--theme-color=<light>,<dark>` writes the
15
+ * `--no-sw` skips the worker. `--remove-sw` ships a `/sw.js` that retires a
16
+ * worker shipped before (deletes its caches and unregisters it). `--theme-color=<light>,<dark>` writes the
16
17
  * startup `theme-color` as a light/dark pair (the one Bloom adopts at runtime)
17
18
  * instead of the manifest's single `theme_color`.
18
19
  *
@@ -22,16 +23,18 @@
22
23
  const { resolve } = require('node:path');
23
24
  const { PwaError, makeInstallable } = require('../web/pwa');
24
25
 
25
- const usage = 'oxy-web-pwa [dist] [--no-sw] [--theme-color=<light>,<dark>]';
26
+ const usage = 'oxy-web-pwa [dist] [--no-sw | --remove-sw] [--theme-color=<light>,<dark>]';
26
27
  const args = process.argv.slice(2);
27
28
  let dist = 'dist';
28
29
  let serviceWorker = true;
30
+ let removeServiceWorker = false;
29
31
  let themeColors;
30
32
  for (const arg of args) {
31
33
  if (arg === '--help' || arg === '-h') {
32
34
  console.log(usage);
33
35
  process.exit(0);
34
36
  } else if (arg === '--no-sw') serviceWorker = false;
37
+ else if (arg === '--remove-sw') removeServiceWorker = true;
35
38
  else if (arg.startsWith('--theme-color='))
36
39
  themeColors = arg
37
40
  .slice('--theme-color='.length)
@@ -44,7 +47,11 @@ for (const arg of args) {
44
47
  }
45
48
 
46
49
  try {
47
- const result = makeInstallable(resolve(dist), { serviceWorker, themeColors });
50
+ const result = makeInstallable(resolve(dist), {
51
+ serviceWorker,
52
+ removeServiceWorker,
53
+ themeColors,
54
+ });
48
55
  for (const warning of result.warnings) console.warn(`oxy-web-pwa: warning: ${warning}`);
49
56
  console.log(
50
57
  `oxy-web-pwa: ${result.manifest} is installable; ${result.updated.length} of ${result.pages} pages updated` +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxy.so/app-preset",
3
- "version": "6.7.1",
3
+ "version": "6.7.2",
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": {
@@ -24,10 +24,21 @@
24
24
  * Private responses must never be cached by URL: Inbosh once served one
25
25
  * signed-in user's API response to the next.
26
26
  *
27
- * `__OXY_SW_VERSION__` is replaced by `oxy-web-pwa` with a hash of the export,
28
- * so the bytes change on every deploy and the browser installs the new worker
29
- * (imported scripts are compared byte for byte too). The cache is bounded and
30
- * old caches are dropped on `activate`.
27
+ * Updates. `__OXY_SW_VERSION__` is replaced by `oxy-web-pwa` with a hash of
28
+ * the export, so this file's bytes change on every deploy; the browser compares
29
+ * imported scripts byte for byte, so it installs the new worker, which takes
30
+ * over at once (`skipWaiting`, `clients.claim`). App code is never stale: a
31
+ * navigation is network-first, so the next load or reload runs the new deploy,
32
+ * and a page still running the old one keeps loading its own chunks.
33
+ *
34
+ * Cleanup. The cache name is fixed (`oxy-sw-v1`), not per deploy: hashed
35
+ * chunks never collide, so one cache holds both deploys and a tab opened
36
+ * before a deploy can still lazy-load the chunks it was built with (the server
37
+ * no longer has them). It holds at most 150 entries, oldest dropped first, the
38
+ * shell never. `activate` deletes every other `oxy-sw-*` cache, so bumping the
39
+ * name here (when a strategy changes what a cached entry means) clears old
40
+ * entries everywhere on the next deploy. `oxy-web-pwa --remove-sw` retires the
41
+ * worker and its caches entirely.
31
42
  */
32
43
  (() => {
33
44
  if (self.__oxySwBase) return;
@@ -35,7 +46,8 @@
35
46
 
36
47
  const VERSION = '__OXY_SW_VERSION__';
37
48
  const CACHE_PREFIX = 'oxy-sw-';
38
- const CACHE_NAME = `${CACHE_PREFIX}${VERSION}`;
49
+ const CACHE_NAME = `${CACHE_PREFIX}v1`;
50
+ self.__oxySwVersion = VERSION;
39
51
  const SHELL_KEY = '/index.html';
40
52
  const MAX_ENTRIES = 150;
41
53
  const STATIC_FILE = /\.(?:js|mjs|css|woff2?|ttf|otf|png|jpe?g|gif|svg|ico|webp|avif|wasm)$/i;
@@ -149,5 +161,5 @@
149
161
  else if (strategy === 'static') event.respondWith(staleWhileRevalidate(event));
150
162
  });
151
163
 
152
- self.__oxySwBaseInternals = { strategyFor, cacheable, CACHE_NAME, SHELL_KEY, MAX_ENTRIES };
164
+ self.__oxySwBaseInternals = { strategyFor, cacheable, trim, CACHE_NAME, SHELL_KEY, MAX_ENTRIES };
153
165
  })();
@@ -8,11 +8,27 @@
8
8
  * after `load` has often fired, and a `load` listener added then never runs,
9
9
  * so the page's state is checked first. Failure is silent: the app works
10
10
  * without a worker.
11
+ *
12
+ * Updates: `updateViaCache: 'none'` makes every update check fetch `/sw.js` and
13
+ * `/oxy-sw-base.js` past the HTTP cache. The browser checks on each navigation,
14
+ * but an installed app can stay open for days without one, so a page that
15
+ * comes back to the foreground checks too, at most once an hour.
11
16
  */
12
17
  (() => {
13
18
  if (!('serviceWorker' in navigator) || !window.isSecureContext) return;
19
+ const HOUR = 60 * 60 * 1000;
20
+ let checked = Date.now();
14
21
  const register = () => {
15
- navigator.serviceWorker.register('/sw.js').catch(() => {});
22
+ navigator.serviceWorker
23
+ .register('/sw.js', { updateViaCache: 'none' })
24
+ .then((registration) => {
25
+ document.addEventListener('visibilitychange', () => {
26
+ if (document.visibilityState !== 'visible' || Date.now() - checked < HOUR) return;
27
+ checked = Date.now();
28
+ registration.update().catch(() => {});
29
+ });
30
+ })
31
+ .catch(() => {});
16
32
  };
17
33
  if (document.readyState === 'complete') register();
18
34
  else window.addEventListener('load', register, { once: true });
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Written as `/sw.js` by `oxy-web-pwa --remove-sw`: retires this origin's
3
+ * worker. The browser installs it on the next update check (it differs from the
4
+ * worker it replaces), and on `activate` it deletes the Oxy caches and
5
+ * unregisters itself. It answers no `fetch`, so open pages go to the network
6
+ * at once (nothing reloads under the user) and the next load has no worker.
7
+ * Shipping no `/sw.js` at all would not do this: a failed update check leaves
8
+ * the old worker running.
9
+ */
10
+ self.addEventListener('install', () => self.skipWaiting());
11
+ self.addEventListener('activate', (event) => {
12
+ event.waitUntil(
13
+ (async () => {
14
+ for (const key of await caches.keys())
15
+ if (key.startsWith('oxy-sw-')) await caches.delete(key);
16
+ await self.registration.unregister();
17
+ })(),
18
+ );
19
+ });
package/web/pwa.js CHANGED
@@ -27,6 +27,7 @@ const REQUIRED_ICON_SIZES = ['192x192', '512x512'];
27
27
  const SKIPPED_DIRS = new Set(['_expo', 'assets', 'node_modules']);
28
28
  const SW_BASE = 'oxy-sw-base.js';
29
29
  const SW_REGISTER = 'oxy-sw-register.js';
30
+ const SW_REMOVE = 'oxy-sw-remove.js';
30
31
  const WEB_DIR = __dirname;
31
32
 
32
33
  class PwaError extends Error {}
@@ -203,7 +204,15 @@ importScripts('/${SW_BASE}');
203
204
  * Runs the whole step on `dist`. Throws PwaError on anything that leaves the
204
205
  * app uninstallable; returns what it did and any warnings.
205
206
  */
206
- function makeInstallable(dist, { serviceWorker = true, themeColors } = {}) {
207
+ function makeInstallable(
208
+ dist,
209
+ { serviceWorker = true, removeServiceWorker = false, themeColors } = {},
210
+ ) {
211
+ if (removeServiceWorker && !serviceWorker)
212
+ throw new PwaError(
213
+ '--remove-sw and --no-sw cannot be combined; --remove-sw ships no worker either',
214
+ );
215
+ if (removeServiceWorker) serviceWorker = false;
207
216
  if (themeColors !== undefined) {
208
217
  const valid = /^(?:#[0-9a-f]{3,8}|[a-z]+|(?:rgb|hsl)a?\([^"<>]*\))$/i;
209
218
  if (
@@ -250,7 +259,14 @@ function makeInstallable(dist, { serviceWorker = true, themeColors } = {}) {
250
259
  }
251
260
 
252
261
  let worker = null;
253
- if (serviceWorker) {
262
+ if (removeServiceWorker) {
263
+ if (existsSync(join(dist, 'sw.js')))
264
+ throw new PwaError(
265
+ '--remove-sw replaces /sw.js, but the app ships its own public/sw.js; drop its importScripts line instead',
266
+ );
267
+ writeFileSync(join(dist, 'sw.js'), readFileSync(join(WEB_DIR, SW_REMOVE)));
268
+ worker = 'removal (retires the worker and its caches)';
269
+ } else if (serviceWorker) {
254
270
  const version = exportVersion(pages);
255
271
  const base = readFileSync(join(WEB_DIR, SW_BASE), 'utf8').replace(
256
272
  /__OXY_SW_VERSION__/g,
@@ -281,4 +297,5 @@ module.exports = {
281
297
  wantedTags,
282
298
  SW_BASE,
283
299
  SW_REGISTER,
300
+ SW_REMOVE,
284
301
  };