@uniweb/core 1.4.2 → 1.4.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/core",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
@@ -52,7 +52,7 @@
52
52
  "vitest": "^4.1.7"
53
53
  },
54
54
  "dependencies": {
55
- "@uniweb/semantic-parser": "~1.12.2",
55
+ "@uniweb/semantic-parser": "~1.13.0",
56
56
  "@uniweb/theming": "~1.2.1"
57
57
  },
58
58
  "scripts": {
@@ -0,0 +1,111 @@
1
+ /**
2
+ * The inputs `pageShellFingerprint` renders (`page-shell.js`). Together they reach every branch of
3
+ * `pageShell` and of the `__DATA__` it writes (`dataScript`, `splitPageSections`), so a change to
4
+ * the HTML for any input changes the fingerprint.
5
+ *
6
+ * ⭐ A branch added to `pageShell`, to `data-script.js` or to a helper they call takes a vector here
7
+ * in the same commit. Adding one changes the fingerprint, which is what a new branch needs: a page
8
+ * cached before it was written without it.
9
+ *
10
+ * ⛔ Editing a vector changes the fingerprint too, and a host that keys its cache on it renders
11
+ * every page it cached again. Change one only to reach a branch.
12
+ *
13
+ * Pure data, so a bundle that never calls `pageShellFingerprint` leaves it out — the runtime's
14
+ * browser build, which imports `foundationCssUrl` from `page-shell.js`, carries none of it.
15
+ */
16
+ export const PAGE_SHELL_VECTORS = [
17
+ // The least a host passes: no name, icon, locale, extensions, entry or preloads.
18
+ {
19
+ content: { config: {}, pages: [{ route: '/', sections: [] }] },
20
+ foundation: { url: '/f/entry.js' },
21
+ runtime: { base: '', manifest: {} },
22
+ },
23
+
24
+ // Every part of the head, and text an author wrote that must be escaped where it lands.
25
+ {
26
+ content: {
27
+ config: {
28
+ name: `Café & "Co" <Ltd>'s`,
29
+ favicon: '/favicon.svg?v=2',
30
+ activeLocale: 'fr',
31
+ extensions: ['https://assets.test/effects/entry.js', { url: '/ext/charts/entry.js' }, null],
32
+ },
33
+ pages: [{ route: '/', sections: [{ type: 'Hero', content: { text: 'a </script><!-- b' } }] }],
34
+ },
35
+ route: '/',
36
+ foundation: {
37
+ url: 'https://assets.test/foundations/acme/site/1.0.0/entry.js',
38
+ cssUrl: 'https://assets.test/foundations/acme/site/1.0.0/custom.css',
39
+ },
40
+ runtime: {
41
+ base: 'https://cdn.test/runtime/1.1.17',
42
+ manifest: {
43
+ entry: '/assets/index-AAA.js',
44
+ preloads: ['assets/setup-BBB.js'],
45
+ importMap: { react: '/_importmap/react.js', '@uniweb/core': '_importmap/core.js' },
46
+ },
47
+ },
48
+ },
49
+
50
+ // Split content: the served page and a dynamic template keep their sections; a page with an
51
+ // address leaves them, saying whether it has content; one with no address, or no sections, stays.
52
+ {
53
+ content: {
54
+ config: {
55
+ activeLocale: 'en',
56
+ favicon: '/icon.png',
57
+ pageHashes: {
58
+ en: {
59
+ '/': '/_pages/a.json',
60
+ '/about': '/_pages/b.json',
61
+ '/empty': '/_pages/c.json',
62
+ '/docs': '/_pages/d.json',
63
+ '/blog/:slug': '/_pages/e.json',
64
+ '/group': '/_pages/f.json',
65
+ },
66
+ },
67
+ },
68
+ pages: [
69
+ { route: '/', sections: [{ type: 'Hero' }] },
70
+ { route: '/about', sections: [{ type: 'Text' }] },
71
+ { route: '/empty', sections: [] },
72
+ { route: '/docs', sections: [], hasContent: true },
73
+ { route: '/blog/:slug', isDynamic: true, sections: [{ type: 'Post' }] },
74
+ { route: '/contact', sections: [{ type: 'Form' }] },
75
+ { route: '/group' },
76
+ ],
77
+ },
78
+ route: '/',
79
+ foundation: { url: '/f/entry.js' },
80
+ runtime: { base: 'https://cdn.test/runtime/1.1.17/', manifest: { entry: 'assets/index-AAA.js' } },
81
+ },
82
+
83
+ // Addresses, but no locale named to read them by, so nothing splits; an empty import map; a
84
+ // relative base.
85
+ {
86
+ content: {
87
+ config: { favicon: '/favicon.ico', pageHashes: { de: { '/': '/_pages/x.json' } } },
88
+ pages: [
89
+ { route: '/', sections: [{ type: 'Hero' }] },
90
+ { route: '/about', sections: [{ type: 'Text' }] },
91
+ ],
92
+ },
93
+ route: '/',
94
+ foundation: { url: '/f/entry.js' },
95
+ runtime: { base: '/rt', manifest: { importMap: {} } },
96
+ },
97
+
98
+ // An icon of no known type, addresses but no pages, no foundation entry, no manifest.
99
+ {
100
+ content: { config: { activeLocale: 'en', favicon: '/logo.gif', pageHashes: { en: { '/': '/_pages/y.json' } } } },
101
+ foundation: {},
102
+ runtime: { base: 'https://cdn.test/rt/' },
103
+ },
104
+
105
+ // Content with no config, and an empty preload list.
106
+ {
107
+ content: { pages: [] },
108
+ foundation: { url: 'https://assets.test/f/entry.js' },
109
+ runtime: { base: 'https://cdn.test/rt', manifest: { entry: 'assets/index-AAA.js', preloads: [] } },
110
+ },
111
+ ]
package/src/page-shell.js CHANGED
@@ -12,11 +12,17 @@
12
12
  * of the site it served could start.
13
13
  *
14
14
  * Its output is a function of its inputs only — no request, no clock — because a host caches it
15
- * under a key of publish-time facts, one entry serving a site's every hostname.
15
+ * under a key of publish-time facts, one entry serving a site's every hostname. Besides its inputs,
16
+ * only this code changes it, and `pageShellFingerprint` says when.
17
+ *
18
+ * ⛔ A change to what this writes that an older runtime's browser build cannot read — a new
19
+ * `__DATA__` encoding, a new split shape — is a breaking change: mark the commit `!` and name the
20
+ * oldest runtime that reads the new pages.
16
21
  *
17
22
  * Zero-dependency leaf, like `data-script.js`, which writes its `__DATA__`.
18
23
  */
19
24
  import { dataScript, jsonForScript } from './data-script.js'
25
+ import { PAGE_SHELL_VECTORS } from './page-shell-vectors.js'
20
26
 
21
27
  /**
22
28
  * Escape a string for HTML text or a double-quoted attribute value.
@@ -126,3 +132,34 @@ export function pageShell({ content, route = null, foundation, runtime }) {
126
132
  '',
127
133
  ].join('\n')
128
134
  }
135
+
136
+ let fingerprint = null
137
+
138
+ /**
139
+ * A fingerprint of what `pageShell` writes: eight hex digits that change whenever the HTML it
140
+ * returns changes, and only then. A host that caches the pages it assembles puts it in its cache
141
+ * key, so a page cached before a change to this module or to `data-script.js` is not served after
142
+ * it, and a change that leaves every page's bytes alone moves nothing.
143
+ *
144
+ * It is worked out from the code that runs — `pageShell` over `PAGE_SHELL_VECTORS`, which reach
145
+ * each of its branches — so it cannot fall behind a change, and there is nothing to bump. A branch
146
+ * added to the shell takes a vector in the same commit (`page-shell-vectors.js`).
147
+ *
148
+ * @returns {string}
149
+ */
150
+ export function pageShellFingerprint() {
151
+ if (fingerprint === null) {
152
+ fingerprint = fnv1a(PAGE_SHELL_VECTORS.map((input) => pageShell(input)).join('\u0000'))
153
+ }
154
+ return fingerprint
155
+ }
156
+
157
+ /** FNV-1a, 32 bits, over UTF-16 code units: a fingerprint, not a security property. */
158
+ function fnv1a(text) {
159
+ let hash = 0x811c9dc5
160
+ for (let i = 0; i < text.length; i++) {
161
+ hash ^= text.charCodeAt(i)
162
+ hash = Math.imul(hash, 0x01000193)
163
+ }
164
+ return (hash >>> 0).toString(16).padStart(8, '0')
165
+ }
@@ -150,29 +150,43 @@ export function reverseTranslateRoute(site, displayRoute, locale) {
150
150
  }
151
151
 
152
152
  /**
153
- * ⭐ **A PAGE'S URL IN A LOCALE** — the one rule the language switcher, a redirect's destination
154
- * and the canonical form share. The locale served unprefixed shows the canonical route; a locale
155
- * with a domain of its own is that domain; any other is `/{code}` plus its translated route.
153
+ * A page's canonical route, from its route canonical or as the active locale shows it: the
154
+ * active locale's prefix comes off, then its display route is reverse-translated. How
155
+ * `localeUrl` reads the route it is given.
156
156
  *
157
157
  * @param {Object} site - a locale's site content, or its `routeIndex`
158
- * @param {string} localeCode - the locale the URL is for
159
158
  * @param {string} route - the page's route, canonical or as the active locale shows it
160
159
  * @param {{ activeLocale?: string, defaultLocale?: string }} [context]
161
- * @returns {string} a path, or an absolute URL on a locale's own domain
160
+ * @returns {string}
162
161
  */
163
- export function localeUrl(site, localeCode, route, context = {}) {
162
+ export function canonicalRoute(site, route, context = {}) {
164
163
  const index = indexOf(site)
165
164
  const activeLocale = context.activeLocale ?? index.activeLocale
166
165
  const defaultLocale = context.defaultLocale ?? index.defaultLocale
167
166
  let target = route
168
-
169
- // The active locale's prefix, then its display route → canonical.
170
167
  if (activeLocale && activeLocale !== defaultLocale) {
171
168
  const prefix = `/${activeLocale}`
172
169
  if (target === prefix || target === `${prefix}/`) target = '/'
173
170
  else if (target.startsWith(`${prefix}/`)) target = target.slice(prefix.length)
174
171
  }
175
- target = reverseTranslateRoute(index, target, activeLocale)
172
+ return reverseTranslateRoute(index, target, activeLocale)
173
+ }
174
+
175
+ /**
176
+ * ⭐ **A PAGE'S URL IN A LOCALE** — the one rule the language switcher, a redirect's destination
177
+ * and the canonical form share. The locale served unprefixed shows the canonical route; a locale
178
+ * with a domain of its own is that domain; any other is `/{code}` plus its translated route.
179
+ *
180
+ * @param {Object} site - a locale's site content, or its `routeIndex`
181
+ * @param {string} localeCode - the locale the URL is for
182
+ * @param {string} route - the page's route, canonical or as the active locale shows it
183
+ * @param {{ activeLocale?: string, defaultLocale?: string }} [context]
184
+ * @returns {string} a path, or an absolute URL on a locale's own domain
185
+ */
186
+ export function localeUrl(site, localeCode, route, context = {}) {
187
+ const index = indexOf(site)
188
+ const defaultLocale = context.defaultLocale ?? index.defaultLocale
189
+ const target = canonicalRoute(index, route, context)
176
190
 
177
191
  const designated = index.domainLocales && Object.entries(index.domainLocales).find(([, lang]) => lang === localeCode)
178
192
  if (designated) {
package/src/uniweb.js CHANGED
@@ -58,6 +58,10 @@ export default class Uniweb {
58
58
  // (`background: 'self'`). Filled by each boot path, like `childBlockRenderer`.
59
59
  this.backgroundRenderer = null
60
60
  this.routingComponents = {}
61
+ // How this host switches locale: `({ locale, route, url }) => void`, or null for a page load
62
+ // of `url` (`Website#switchLocale`). An editor's preview, which hands the runtime each
63
+ // locale's content itself, sets it through `initUniweb({ localeSwitch })`.
64
+ this.localeSwitch = null
61
65
  this.language = 'en'
62
66
 
63
67
  // Framework-level inset components — every foundation gets these
package/src/website.js CHANGED
@@ -22,9 +22,11 @@ import {
22
22
  routeIndex,
23
23
  resolveRoute as resolveRouteIn,
24
24
  localeUrl,
25
+ canonicalRoute,
25
26
  translateRoute as translateRouteIn,
26
27
  reverseTranslateRoute as reverseTranslateRouteIn,
27
28
  } from './resolve-route.js'
29
+ import { applyBasePath } from './base-path.js'
28
30
 
29
31
  /**
30
32
  * Services whose provider is not simply an address, keyed by name.
@@ -1006,6 +1008,38 @@ export default class Website {
1006
1008
  return localeUrl(this._routeIndex, localeCode, targetRoute, this._routeContext())
1007
1009
  }
1008
1010
 
1011
+ /**
1012
+ * Show the page in another locale.
1013
+ *
1014
+ * A locale is its own document, with its own content and routes, so this is a page load of
1015
+ * `getLocaleUrl()` with the base path applied. A host that hands the runtime each locale's
1016
+ * content itself, like an editor's preview, switches its own way instead
1017
+ * (`uniweb.localeSwitch`): it is told the locale, the page's canonical route and the URL a
1018
+ * page load would go to.
1019
+ *
1020
+ * For a menu that switches from code, like a `<select>`. A menu of links needs no call:
1021
+ * `<Link reload href={website.getLocaleUrl(code)}>` loads the page, and the runtime hands a
1022
+ * click on a link to another locale's page to such a host the same way.
1023
+ *
1024
+ * @param {string} localeCode - One of `getLocales()`
1025
+ * @param {string} [route] - The page, canonical or as the active locale shows it
1026
+ * (default: the current page)
1027
+ * @returns {boolean} false when the site has no such locale, and nothing happens
1028
+ */
1029
+ switchLocale(localeCode, route = null) {
1030
+ if (!this.getLocale(localeCode)) return false
1031
+ const context = this._routeContext()
1032
+ const page = route || this.activePage.getNavRoute()
1033
+ const url = applyBasePath(localeUrl(this._routeIndex, localeCode, page, context), this.basePath)
1034
+ const hostSwitch = globalThis.uniweb?.localeSwitch
1035
+ if (hostSwitch) {
1036
+ hostSwitch({ locale: localeCode, route: canonicalRoute(this._routeIndex, page, context), url })
1037
+ } else if (typeof window !== 'undefined') {
1038
+ window.location.assign(url)
1039
+ }
1040
+ return true
1041
+ }
1042
+
1009
1043
  /**
1010
1044
  * Get locale info by code
1011
1045
  * @param {string} localeCode - Locale code