@oxy.so/app-preset 6.7.2 → 6.8.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 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 `/index.html` shell as the offline fallback, and
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. It never stores or serves anything else: cross-origin
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
 
@@ -446,6 +450,27 @@ release bundle: Gradle can otherwise reuse its previous bundle despite a changed
446
450
  package install. Inspect the regenerated source map for a single CSS runtime
447
451
  before comparing device behavior.
448
452
 
453
+ #### A text that names no family draws Bloom's face (6.8.0+)
454
+
455
+ React Native draws a `Text` or `TextInput` that names no `fontFamily` in the
456
+ system face and has no setting for another default; react-native-web gives one
457
+ `font: 14px System`. Bloom's old default, `Text.defaultProps`, never reached a
458
+ text: React 19 applies `defaultProps` to class components only. So a raw
459
+ `<Text className="text-[15px]">` (Mention's feed rows, for one) drew Roboto, San
460
+ Francisco or Segoe UI next to Bloom's Inter.
461
+
462
+ `createOxyMetroConfig` bundles `metro/base-font/Text.js` and `TextInput.js`
463
+ wherever NativeWind's `Text` or `TextInput` would be bundled: the same
464
+ components (`styled(host, mapping)`, NativeWind's mappings) over a host that
465
+ puts the base face FIRST in its style. On native that is `Inter`, on a text with
466
+ no text ancestor (a nested text inherits its parent's family); on the web it is
467
+ `font-family: inherit` as an atomic class, so the text takes the document's
468
+ Inter (`--font-sans`). A `font-*` utility, an inline `fontFamily` and Bloom's
469
+ `Text` (which names its own family) still win. The match is on the resolved
470
+ file, so `import { Text } from 'react-native'`, deep `react-native/Libraries/…`
471
+ imports and the react-native-web path Expo's Babel preset rewrites to all reach
472
+ it; `withBaseFontText` is exported for an app that composes its own config.
473
+
449
474
  #### `react-native` on native: the complete NativeWind shim (4.1.0+)
450
475
 
451
476
  On native, NativeWind's resolver sends every `import … from 'react-native'`
@@ -0,0 +1,5 @@
1
+ // What `react-native-css`'s `Text` resolves to under the preset (`../index.js`,
2
+ // `withBaseFontText`): NativeWind's component, drawing Bloom's base face.
3
+ const { Text } = require('./base-font');
4
+
5
+ module.exports = { __esModule: true, Text, default: Text };
@@ -0,0 +1,5 @@
1
+ // What `react-native-css`'s `TextInput` resolves to under the preset (`../index.js`,
2
+ // `withBaseFontText`): NativeWind's component, drawing Bloom's base face.
3
+ const { TextInput } = require('./base-font');
4
+
5
+ module.exports = { __esModule: true, TextInput, default: TextInput };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * NativeWind's `Text` and `TextInput` on Android and iOS, drawing Bloom's base
3
+ * face, Inter, when the text names no family.
4
+ *
5
+ * React Native draws such a text in the system face and has no setting for
6
+ * another default (Android falls back to `Typeface.DEFAULT`). The old way to set
7
+ * one, `Text.defaultProps`, is gone: React 19 applies `defaultProps` to class
8
+ * components only, and `Text` is a function component. So every text that
9
+ * named no family, a raw `<Text className="text-[15px]">` included, drew Roboto
10
+ * or San Francisco.
11
+ *
12
+ * Each component is `styled(host, mapping)`, as NativeWind builds its own, over
13
+ * a host that puts the base face FIRST in the style it is finally given. Under
14
+ * `styled()` that style is the className's utilities followed by the caller's
15
+ * inline style, so a `font-*` utility, an inline `fontFamily` and every Bloom
16
+ * `Text` (which names its own family) still win. A text nested in another text
17
+ * inherits its parent's family, as React Native does: the base face goes only on
18
+ * a text with no text ancestor.
19
+ *
20
+ * `Inter` mirrors `fontFamilies.sans` in `@oxy.so/bloom/fonts`, the family
21
+ * `BloomThemeProvider` loads on native, as `css/base.css` restates `--font-sans`.
22
+ */
23
+ const { createElement, useContext } = require('react');
24
+ const { styled } = require('react-native-css');
25
+ const { copyComponentProperties } = require('react-native-css/components/copyComponentProperties');
26
+ const reactNative = require('react-native');
27
+ const MAPPINGS = require('./mappings');
28
+
29
+ const BASE_FONT = { fontFamily: 'Inter' };
30
+
31
+ function BaseFontText(props) {
32
+ if (useContext(reactNative.unstable_TextAncestorContext)) {
33
+ return createElement(reactNative.Text, props);
34
+ }
35
+ return createElement(reactNative.Text, { ...props, style: [BASE_FONT, props.style] });
36
+ }
37
+
38
+ function BaseFontTextInput(props) {
39
+ return createElement(reactNative.TextInput, { ...props, style: [BASE_FONT, props.style] });
40
+ }
41
+
42
+ module.exports = {
43
+ Text: copyComponentProperties(reactNative.Text, styled(BaseFontText, MAPPINGS.Text)),
44
+ TextInput: copyComponentProperties(
45
+ reactNative.TextInput,
46
+ styled(BaseFontTextInput, MAPPINGS.TextInput),
47
+ ),
48
+ };
@@ -0,0 +1,46 @@
1
+ /**
2
+ * NativeWind's `Text` and `TextInput` on the web, drawing the document's family
3
+ * (Inter: `--font-sans`, `css/base.css`) when the text names none.
4
+ *
5
+ * react-native-web gives a text with no text ancestor, and every TextInput,
6
+ * `font: 14px System`, so it drew the system stack (Segoe UI, Roboto, San
7
+ * Francisco) whatever the page's family. CSS alone cannot undo it: the rule sits
8
+ * in react-native-web's stylesheet at a class's specificity, and anything that
9
+ * beats it there also beats the atomic classes a `fontFamily` style compiles to.
10
+ *
11
+ * Each component is `styled(host, mapping)`, as NativeWind builds its own, over
12
+ * a host that puts `fontFamily: inherit` FIRST in its style, as an atomic class
13
+ * (`StyleSheet.create`): it beats react-native-web's default and loses to a
14
+ * `font-*` utility (the app's stylesheet follows react-native-web's), to a later
15
+ * `fontFamily` style (every Bloom `Text` names its own) and to an inline one. A
16
+ * nested text already inherits; inheriting again changes nothing.
17
+ *
18
+ * The hosts are react-native-web's own components, imported by the path that
19
+ * Expo's Babel preset rewrites `import { Text } from 'react-native'` to.
20
+ */
21
+ const { createElement } = require('react');
22
+ const { styled } = require('react-native-css');
23
+ const { copyComponentProperties } = require('react-native-css/components/copyComponentProperties');
24
+ const StyleSheet = require('react-native-web/dist/exports/StyleSheet').default;
25
+ const MAPPINGS = require('./mappings');
26
+
27
+ const RNWText = require('react-native-web/dist/exports/Text').default;
28
+ const RNWTextInput = require('react-native-web/dist/exports/TextInput').default;
29
+
30
+ const styles = StyleSheet.create({ documentFont: { fontFamily: 'inherit' } });
31
+
32
+ function DocumentFontText(props) {
33
+ return createElement(RNWText, { ...props, style: [styles.documentFont, props.style] });
34
+ }
35
+
36
+ function DocumentFontTextInput(props) {
37
+ return createElement(RNWTextInput, { ...props, style: [styles.documentFont, props.style] });
38
+ }
39
+
40
+ module.exports = {
41
+ Text: copyComponentProperties(RNWText, styled(DocumentFontText, MAPPINGS.Text)),
42
+ TextInput: copyComponentProperties(
43
+ RNWTextInput,
44
+ styled(DocumentFontTextInput, MAPPINGS.TextInput),
45
+ ),
46
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * NativeWind's own mappings for its `Text` and `TextInput`
3
+ * (`react-native-css/components/Text` and `/TextInput`): a className becomes
4
+ * `style`, and on a TextInput `text-center` and friends also reach `textAlign`.
5
+ */
6
+ module.exports = {
7
+ Text: { className: 'style' },
8
+ TextInput: { className: { target: 'style', nativeStyleMapping: { textAlign: true } } },
9
+ };
package/metro/index.js CHANGED
@@ -385,6 +385,78 @@ function withCompleteReactNative(config, projectRoot = config.projectRoot) {
385
385
  return config;
386
386
  }
387
387
 
388
+ const BASE_FONT_DIR = path.join(__dirname, 'base-font');
389
+ /** The base-font modules, by both spellings (as for the completed module). */
390
+ const BASE_FONT_MODULES = new Set(
391
+ fs
392
+ .readdirSync(BASE_FONT_DIR)
393
+ .flatMap((file) => [
394
+ path.join(BASE_FONT_DIR, file),
395
+ fs.realpathSync(path.join(BASE_FONT_DIR, file)),
396
+ ]),
397
+ );
398
+ /** NativeWind's `Text` and `TextInput` components, by any of the conditions Metro may pick. */
399
+ const CSS_TEXT_COMPONENT =
400
+ /[/\\]react-native-css[/\\](?:dist[/\\](?:commonjs|module)[/\\]components[/\\](Text|TextInput)\.js|src[/\\]components[/\\](Text|TextInput)\.tsx)$/;
401
+
402
+ /**
403
+ * Wraps a Metro config so that wherever NativeWind's `Text` or `TextInput`
404
+ * would be bundled, `./base-font/Text.js` or `./base-font/TextInput.js` is:
405
+ * the same component, drawing Bloom's base face when the text names no family
406
+ * (Inter on Android and iOS, the document's family on the web; see
407
+ * `./base-font/base-font.native.js` and `.web.js` for why each platform needs it).
408
+ *
409
+ * NativeWind is where every text of the app already passes: on native it sends
410
+ * `import { Text } from 'react-native'` (and deep `react-native/Libraries/…`
411
+ * imports) to its components, on the web the deep `react-native-web` import Expo's
412
+ * Babel preset rewrites it to. So the match is on the RESOLVED file, by every
413
+ * route that reaches it; nothing else changes.
414
+ *
415
+ * Apply it OUTERMOST. The base-font modules' own imports (React Native's hosts,
416
+ * NativeWind's runtime) go straight to Metro's resolver from the project, past
417
+ * every redirect: NativeWind's would send the hosts back to the components
418
+ * these modules replace.
419
+ *
420
+ * @param {import('metro-config').MetroConfig} config The finished config, NativeWind applied.
421
+ * @param {string} [projectRoot=config.projectRoot] The app package.
422
+ * @returns {import('metro-config').MetroConfig} `config`, its resolver wrapped.
423
+ */
424
+ function withBaseFontText(config, projectRoot = config.projectRoot) {
425
+ const parentResolveRequest = config.resolver.resolveRequest;
426
+ const projectOrigin = path.join(projectRoot, 'package.json');
427
+
428
+ config.resolver.resolveRequest = (context, moduleName, platform) => {
429
+ if (BASE_FONT_MODULES.has(context.originModulePath)) {
430
+ const own = moduleName.startsWith('.')
431
+ ? context
432
+ : cssRuntimeContext(
433
+ { ...context, originModulePath: projectOrigin },
434
+ moduleName,
435
+ platform,
436
+ projectRoot,
437
+ );
438
+ return context.resolveRequest(own, moduleName, platform);
439
+ }
440
+
441
+ const resolution = (parentResolveRequest ?? context.resolveRequest)(
442
+ context,
443
+ moduleName,
444
+ platform,
445
+ );
446
+ const component =
447
+ resolution?.type === 'sourceFile' && CSS_TEXT_COMPONENT.exec(resolution.filePath);
448
+ if (component) {
449
+ return {
450
+ type: 'sourceFile',
451
+ filePath: path.join(BASE_FONT_DIR, `${component[1] ?? component[2]}.js`),
452
+ };
453
+ }
454
+ return resolution;
455
+ };
456
+
457
+ return config;
458
+ }
459
+
388
460
  /**
389
461
  * Wraps a NativeWind Metro config with the Bloom single-instance rewrite and the
390
462
  * web `ws` shim. In hoisted monorepos the same Bloom version is duplicated under
@@ -447,9 +519,10 @@ function createOxyMetroConfigWithBloom(projectRoot, options = {}) {
447
519
  config,
448
520
  options.workspaceRoot ? path.resolve(options.workspaceRoot) : findWorkspaceRoot(projectRoot),
449
521
  );
450
- // Outermost, so it sees the final resolution and its own module's imports
451
- // reach no redirect beneath it.
452
- return withCompleteReactNative(config, projectRoot);
522
+ // Outermost, so they see the final resolution and their own modules'
523
+ // imports reach no redirect beneath them.
524
+ config = withCompleteReactNative(config, projectRoot);
525
+ return withBaseFontText(config, projectRoot);
453
526
  }
454
527
 
455
528
  module.exports = {
@@ -458,6 +531,7 @@ module.exports = {
458
531
  withBloomSingleInstance,
459
532
  withReactNativeCssSingleInstance,
460
533
  withCompleteReactNative,
534
+ withBaseFontText,
461
535
  withLinkedNativePackagesOnly,
462
536
  withFreshWorkspaceBuilds,
463
537
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxy.so/app-preset",
3
- "version": "6.7.2",
3
+ "version": "6.8.0",
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": {
@@ -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 served offline.
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
- caches
60
- .open(CACHE_NAME)
61
- .then((cache) => cache.add(new Request(SHELL_KEY, { cache: 'reload' })))
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
- if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting();
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
- if (
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
- return (await cache.match(SHELL_KEY)) || offline();
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 = { strategyFor, cacheable, trim, CACHE_NAME, SHELL_KEY, MAX_ENTRIES };
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
  })();
@@ -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' })