@uniweb/core 1.4.0 → 1.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/core",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
@@ -8,18 +8,21 @@
8
8
  "./base-path": "./src/base-path.js",
9
9
  "./records-service": "./src/records-service.js",
10
10
  "./data-paths": "./src/data-paths.js",
11
+ "./data-script": "./src/data-script.js",
11
12
  "./data-keys": "./src/data-keys.js",
12
13
  "./datastore": "./src/datastore.js",
13
14
  "./detail-url": "./src/detail-url.js",
14
15
  "./fetch-config": "./src/fetch-config.js",
15
16
  "./layout-name": "./src/layout-name.js",
16
17
  "./page-data": "./src/page-data.js",
18
+ "./page-shell": "./src/page-shell.js",
17
19
  "./icon-corpus": "./src/icon-corpus.js",
18
20
  "./insets": "./src/insets.js",
19
21
  "./locale-config": "./src/locale-config.js",
20
22
  "./route-match": "./src/route-match.js",
21
23
  "./resolve-route": "./src/resolve-route.js",
22
24
  "./section-id": "./src/section-id.js",
25
+ "./section-type": "./src/section-type.js",
23
26
  "./services": "./src/services.js",
24
27
  "./sort": "./src/sort.js",
25
28
  "./tracker": "./src/tracker.js",
@@ -50,7 +53,7 @@
50
53
  },
51
54
  "dependencies": {
52
55
  "@uniweb/semantic-parser": "~1.12.2",
53
- "@uniweb/theming": "~1.2.0"
56
+ "@uniweb/theming": "~1.2.1"
54
57
  },
55
58
  "scripts": {
56
59
  "test": "vitest run"
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The data a page's shell carries — the ONE rule for writing it, for every lane that writes a
3
+ * shell: a static build (`__SITE_CONTENT__`) and a host that assembles one per request
4
+ * (`__DATA__`, `{ foundation, content }`, which the runtime reads in `decodeData`).
5
+ *
6
+ * Two jobs, both easy to get subtly wrong in a copy:
7
+ *
8
+ * 1. ⭐ JSON INSIDE A `<script>` ESCAPES EVERY `<` AS `<`. A `<script>` element's text ends at
9
+ * `</script` and changes state at `<!--`, so either inside a string value would break the page;
10
+ * `<` is a JSON escape, so the text still parses as the same value. ⛔ A JavaScript-string
11
+ * escape is not a JSON one: `<\!--` keeps the HTML parser calm and makes the whole payload
12
+ * invalid JSON. Measured 2026-10-23 on a hosted site, whose every page then stayed its server
13
+ * render with no client app.
14
+ *
15
+ * 2. ⭐ SPLIT CONTENT CARRIES ONE PAGE'S SECTIONS. When a site's content is split, a page's data
16
+ * keeps every page's entry but only its own sections and every dynamic template's, which a
17
+ * route's record page is built from. Every other page's sections are fetched when the page is
18
+ * visited (`Page.loadContent`), from the address `config.pageHashes[locale][route]` names on a
19
+ * host, or `_pages/<route>.json` in a static build. A page whose sections leave says
20
+ * `hasContent`, so it is not taken for an empty folder.
21
+ *
22
+ * Zero-dependency leaf: the build imports it, and a host reaches it through `@uniweb/runtime/ssr`.
23
+ */
24
+
25
+ /**
26
+ * A value as JSON text that can sit inside a `<script>` element: every `<` as `<`.
27
+ *
28
+ * @param {*} value
29
+ * @returns {string}
30
+ */
31
+ export function jsonForScript(value) {
32
+ return JSON.stringify(value).replace(/</g, '\\u003c')
33
+ }
34
+
35
+ /**
36
+ * The pages a split page's data carries: every entry, with sections only for the page being served
37
+ * and for every dynamic template. A page whose sections leave says `hasContent`.
38
+ *
39
+ * @param {Array<Object>} pages - the site content's `pages`
40
+ * @param {string|null} route - the route of the page being served
41
+ * @param {Object} [options]
42
+ * @param {(page: Object) => boolean} [options.fetchable] - whether a page's sections can be fetched
43
+ * when it is visited; a page whose cannot keeps them inline
44
+ * @returns {Array<Object>}
45
+ */
46
+ export function splitPageSections(pages, route, { fetchable = () => true } = {}) {
47
+ return pages.map((page) => {
48
+ if (page.route === route || page.isDynamic || !Array.isArray(page.sections) || !fetchable(page)) {
49
+ return page
50
+ }
51
+ const { sections, ...entry } = page
52
+ return { ...entry, hasContent: entry.hasContent ?? sections.length > 0 }
53
+ })
54
+ }
55
+
56
+ /**
57
+ * The `<script id="__DATA__">` element a host writes into a page's shell — the boot payload
58
+ * `{ foundation, content }`, as JSON a `<script>` cannot end or misparse (`jsonForScript`).
59
+ *
60
+ * When the content names where each page's sections are served — `content.config.pageHashes`,
61
+ * per locale — it carries only the served page's sections and every dynamic template's
62
+ * (`splitPageSections`); a page with no address keeps its sections inline. Without that map it
63
+ * carries the content as it is.
64
+ *
65
+ * @param {{ foundation: Object, content: Object }} data
66
+ * @param {Object} [options]
67
+ * @param {string} [options.route] - the route of the page being served, as the content names it (a
68
+ * dynamic page's own template route for one of its records)
69
+ * @param {string} [options.locale] - the locale the content is for; by default its `activeLocale`
70
+ * @returns {string}
71
+ */
72
+ export function dataScript({ foundation, content }, { route = null, locale } = {}) {
73
+ const addresses = content?.config?.pageHashes?.[locale ?? content?.config?.activeLocale]
74
+ const carried =
75
+ addresses && Array.isArray(content.pages)
76
+ ? {
77
+ ...content,
78
+ pages: splitPageSections(content.pages, route, { fetchable: (page) => Boolean(addresses[page.route]) }),
79
+ }
80
+ : content
81
+ return `<script id="__DATA__" type="application/json">${jsonForScript({ foundation, content: carried })}</script>`
82
+ }
@@ -52,19 +52,51 @@
52
52
  */
53
53
  export const DEFAULT_ICON_BASE = 'https://uniweb.github.io/icons'
54
54
 
55
+ /**
56
+ * Families whose own sites spell a digit off by a hyphen (Lucide's `building-2` and `grid-2x2`,
57
+ * Heroicons' `bars-3`, Feather's `bar-chart-2`), where the corpus, named after react-icons, does
58
+ * not (`building2`, `grid2-x2`, `bars3`, `bar-chart2`). An author copies a name from the family's
59
+ * site, so for these the corpus answers that spelling too.
60
+ *
61
+ * ⛔ Only families the rule leaves every corpus name of unchanged, since the writer composes its
62
+ * paths with it too. Material's `3d-rotation` and Tabler's `cube3d-sphere` keep a letter after a
63
+ * digit unhyphenated, so the rule would move them. `_contracts/icon-corpus-names.test.js` holds
64
+ * these four to it, over every name their manifests list.
65
+ */
66
+ const UPSTREAM_SPELLED = new Set(['lu', 'hi', 'hi2', 'fi'])
67
+
68
+ /**
69
+ * An icon's name as the corpus spells it. In a family above, a digit takes no hyphen before it,
70
+ * and a letter after a digit takes one: `building-2` → `building2`, `grid-2x2` → `grid2-x2`. A
71
+ * name the corpus already spells comes back unchanged.
72
+ *
73
+ * ⛔ *Until 2026-10-24 a name was taken as written, so the 107 Lucide icons whose names hold a
74
+ * digit answered 404 to the spelling on Lucide's own site.*
75
+ *
76
+ * @param {string} family - short family code
77
+ * @param {string} name - icon id as written
78
+ * @returns {string}
79
+ */
80
+ export function corpusIconName(family, name) {
81
+ const written = String(name ?? '')
82
+ if (!UPSTREAM_SPELLED.has(family)) return written
83
+ return written.replace(/-(?=\d)/g, '').replace(/(\d)([a-z])/g, '$1-$2')
84
+ }
85
+
55
86
  /**
56
87
  * The corpus path for one icon, relative to any origin serving it.
57
88
  *
58
89
  * `{family}/{family}-{name}.svg` — the family repeats deliberately: the
59
90
  * directory groups, and the filename prefix keeps ids unique across families so
60
- * a name alone is never ambiguous.
91
+ * a name alone is never ambiguous. The name is the corpus's spelling of it
92
+ * (`corpusIconName`).
61
93
  *
62
94
  * @param {string} family - short family code (`lu`, `hi2`, `fa6`)
63
95
  * @param {string} name - icon id within that family (`house`, `a-arrow-down`)
64
96
  * @returns {string} e.g. `lu/lu-house.svg`
65
97
  */
66
98
  export function iconPath(family, name) {
67
- return `${family}/${family}-${name}.svg`
99
+ return `${family}/${family}-${corpusIconName(family, name)}.svg`
68
100
  }
69
101
 
70
102
  /**
@@ -0,0 +1,128 @@
1
+ /**
2
+ * A page's shell — the ONE writer of the HTML a page starts as, for a host that assembles pages:
3
+ * the head (charset, viewport, title, icon, the foundation's stylesheet, the runtime's import map,
4
+ * entry and preloads), an empty `#root`, and `__DATA__`. A host calls it through its own link for a
5
+ * page it serves without rendering it, and inside an isolate (`@uniweb/runtime/ssr` re-exports it,
6
+ * versioned with the runtime) as the shell `createPageRenderer` renders into.
7
+ *
8
+ * ⭐ Page assembly is framework's [Diego, 2026-10-23: a host is "supposed to do hosting right,
9
+ * not rendering or page assembling. That's our job."]. A host hands in what only it knows — the
10
+ * addresses it resolved and the content it read — and serves what comes back. ⛔ Until then each
11
+ * host wrote its own shell, and one escaped `__DATA__` its own way, as invalid JSON, so no page
12
+ * of the site it served could start.
13
+ *
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.
16
+ *
17
+ * Zero-dependency leaf, like `data-script.js`, which writes its `__DATA__`.
18
+ */
19
+ import { dataScript, jsonForScript } from './data-script.js'
20
+
21
+ /**
22
+ * Escape a string for HTML text or a double-quoted attribute value.
23
+ *
24
+ * @param {*} str
25
+ * @returns {string}
26
+ */
27
+ export function escapeHtml(str) {
28
+ if (!str) return ''
29
+ return String(str)
30
+ .replace(/&/g, '&amp;')
31
+ .replace(/</g, '&lt;')
32
+ .replace(/>/g, '&gt;')
33
+ .replace(/"/g, '&quot;')
34
+ .replace(/'/g, '&#39;')
35
+ }
36
+
37
+ /**
38
+ * The MIME type of a favicon, by its file's extension — or null when unknown, and the `type`
39
+ * attribute is then left out.
40
+ *
41
+ * @param {string} href
42
+ * @returns {string|null}
43
+ */
44
+ export function faviconType(href) {
45
+ const ext = String(href || '').split('?')[0].split('.').pop()?.toLowerCase()
46
+ if (ext === 'svg') return 'image/svg+xml'
47
+ if (ext === 'ico') return 'image/x-icon'
48
+ if (ext === 'png') return 'image/png'
49
+ return null
50
+ }
51
+
52
+ /**
53
+ * A foundation's stylesheet, by the convention every foundation build follows: `…/entry.js` →
54
+ * `…/assets/style.css`. The runtime's loader derives an extension's the same way.
55
+ *
56
+ * @param {string} url - the foundation's (or an extension's) entry
57
+ * @returns {string|null}
58
+ */
59
+ export function foundationCssUrl(url) {
60
+ return typeof url === 'string' && url ? url.replace(/[^/]+\.js$/, 'assets/style.css') : null
61
+ }
62
+
63
+ /** A runtime file's address: its path in the manifest, on the runtime's base. */
64
+ function onBase(base, path) {
65
+ return `${base.endsWith('/') ? base : `${base}/`}${String(path).replace(/^\/+/, '')}`
66
+ }
67
+
68
+ const extensionUrl = (entry) => (typeof entry === 'string' ? entry : entry?.url) || null
69
+
70
+ /**
71
+ * The HTML a page starts as.
72
+ *
73
+ * @param {Object} input
74
+ * @param {Object} input.content - one locale's content as the host stamped it: `config` (`name`,
75
+ * `favicon`, `activeLocale`, `extensions` — each extension's entry URL, in declared order), the
76
+ * pages, and any `fetchedData`
77
+ * @param {string} [input.route] - the served page's route as the content names it, so a split
78
+ * site's `__DATA__` carries that page's sections (`dataScript`)
79
+ * @param {{ url: string, cssUrl?: string }} input.foundation - the foundation's entry; its
80
+ * stylesheet is `foundationCssUrl(url)` unless given
81
+ * @param {{ base: string, manifest: { entry: string, preloads?: string[], importMap?: Object } }}
82
+ * input.runtime - the runtime's base address, absolute, and its `manifest.json`
83
+ * @returns {string}
84
+ */
85
+ export function pageShell({ content, route = null, foundation, runtime }) {
86
+ const config = content?.config || {}
87
+ const manifest = runtime?.manifest || {}
88
+ const base = runtime?.base || ''
89
+ const cssUrl = foundation?.cssUrl || foundationCssUrl(foundation?.url)
90
+ const extensions = (Array.isArray(config.extensions) ? config.extensions : []).map(extensionUrl).filter(Boolean)
91
+ const importMap = Object.fromEntries(
92
+ Object.entries(manifest.importMap || {}).map(([specifier, path]) => [specifier, onBase(base, path)])
93
+ )
94
+ const preloads = [
95
+ ...(Array.isArray(manifest.preloads) ? manifest.preloads : []).map((path) => onBase(base, path)),
96
+ foundation?.url,
97
+ ...extensions,
98
+ ].filter(Boolean)
99
+ const lang = config.activeLocale
100
+
101
+ const head = [
102
+ '<meta charset="UTF-8">',
103
+ '<meta name="viewport" content="width=device-width, initial-scale=1.0">',
104
+ config.name ? `<title>${escapeHtml(config.name)}</title>` : null,
105
+ config.favicon
106
+ ? `<link rel="icon"${faviconType(config.favicon) ? ` type="${faviconType(config.favicon)}"` : ''} href="${escapeHtml(config.favicon)}">`
107
+ : null,
108
+ cssUrl ? `<link rel="stylesheet" href="${escapeHtml(cssUrl)}">` : null,
109
+ // Before the module script and every preload: a browser reads an import map only ahead of them.
110
+ `<script type="importmap">${jsonForScript({ imports: importMap })}</script>`,
111
+ manifest.entry ? `<script type="module" crossorigin src="${escapeHtml(onBase(base, manifest.entry))}"></script>` : null,
112
+ ...preloads.map((href) => `<link rel="modulepreload" crossorigin href="${escapeHtml(href)}">`),
113
+ ].filter(Boolean)
114
+
115
+ return [
116
+ '<!DOCTYPE html>',
117
+ lang ? `<html lang="${escapeHtml(lang)}">` : '<html>',
118
+ '<head>',
119
+ ...head,
120
+ '</head>',
121
+ '<body>',
122
+ '<div id="root"></div>',
123
+ dataScript({ foundation: { url: foundation?.url, cssUrl }, content }, { route, locale: lang }),
124
+ '</body>',
125
+ '</html>',
126
+ '',
127
+ ].join('\n')
128
+ }
package/src/page.js CHANGED
@@ -538,10 +538,11 @@ export default class Page {
538
538
  const lang = this.website.activeLocale
539
539
  const defaultLang = this.website.defaultLocale
540
540
 
541
- // Phase 5 of the CDN migration: prefer the content-addressed URL from
542
- // __DATA__.config.pageHashes (the publish handler emits it). Falls
543
- // back to the legacy /{lang}/_pages/{route}.json shape when the map
544
- // isn't present (older sites that haven't republished yet).
541
+ // A host that splits a site's content names each page's address in
542
+ // `config.pageHashes`, and that address wins. Without one, the static
543
+ // build's own layout: `/{lang}/_pages/{route}.json` beside the site
544
+ // (`build/src/prerender.js`). ⛔ This called that layout "legacy", for
545
+ // sites that had not republished, until 2026-10-24.
545
546
  const hashes = this.website.config?.pageHashes
546
547
  const hashedUrl = hashes?.[lang]?.[this.route]
547
548
 
@@ -556,10 +557,10 @@ export default class Page {
556
557
 
557
558
  const res = await fetch(url)
558
559
  if (res.status === 404 && _shouldReloadOnHashed404(hashedUrl)) {
559
- // Stale tab: this tab's __DATA__ map references a publish that
560
- // no longer exists in R2. One reload usually picks up the fresh
560
+ // Stale tab: this tab's `__DATA__` names the addresses of a publish
561
+ // the host no longer serves. One reload usually picks up the fresh
561
562
  // HTML (and a fresh map). Capped at 2 attempts to defend against
562
- // a malformed publish where new HTML and new R2 also disagree.
563
+ // a publish whose new HTML and new addresses also disagree.
563
564
  window.location.reload()
564
565
  return
565
566
  }
@@ -758,14 +759,14 @@ export default class Page {
758
759
  /**
759
760
  * Helper for the stale-tab 404→reload guard in Page.loadContent.
760
761
  *
761
- * Phase 5 of the CDN migration writes content-addressed `_pages/...-{hash}.json`
762
- * files. Each publish wipes the prior hashes from R2 and writes fresh ones.
763
- * A browser tab loaded against a previous publish carries a stale __DATA__
764
- * map; on SPA navigation it'll request a hash that no longer exists.
765
- * Reloading the tab pulls fresh HTML (and a fresh map). The session-scoped
766
- * counter caps reloads at 2 so a malformed publish (where new HTML and new
767
- * R2 disagree) doesn't trigger an infinite loop — after 2 failed reloads
768
- * we render the synthetic 404 path instead.
762
+ * A host's split content is content-addressed (`config.pageHashes`), and a
763
+ * publish replaces the addresses. A browser tab loaded against a previous
764
+ * publish carries a stale map; on SPA navigation it requests an address that
765
+ * is no longer served. Reloading the tab pulls fresh HTML (and a fresh map).
766
+ * The session-scoped counter caps reloads at 2 so a publish whose new HTML and
767
+ * new addresses disagree doesn't trigger an infinite loop. After 2 failed
768
+ * reloads the page is marked empty with a warning, as any other failed fetch
769
+ * is. ⛔ This said it rendered "the synthetic 404 path" until 2026-10-24.
769
770
  *
770
771
  * Only fires when the request was a hashed URL: legacy non-hashed 404s
771
772
  * keep the prior "mark empty, log warning" behavior (they're not symptomatic
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Section types — the ONE rule for what renders a section that names none.
3
+ *
4
+ * A section names the section type that renders it (`type: Hero` in its file's
5
+ * frontmatter). One that names none renders with the foundation's `defaultSection`,
6
+ * else `Section`. The site's object graph asks this when it builds a Block
7
+ * (`Website.getDefaultBlockType`), and a push asks it for the type a store requires
8
+ * of every section — so a host rendering what the store holds renders what the
9
+ * site's files render. ⛔ Until 2026-10-23 a push sent `Content` for such a section,
10
+ * which no starter template's foundation has, and a hosted page showed
11
+ * "Component not found: Content" where the files rendered `Section`.
12
+ *
13
+ * Zero-dependency leaf, like `layout-name.js`: the build imports it, and should not
14
+ * pull more of core to agree on a type.
15
+ */
16
+
17
+ /** What renders a section when neither it nor its foundation names a type. */
18
+ export const DEFAULT_SECTION_TYPE = 'Section'
19
+
20
+ /**
21
+ * The type a section that names none renders with: the foundation's
22
+ * `defaultSection`, else `Section`.
23
+ *
24
+ * @param {Object|null|undefined} foundation - the foundation's declarations: its
25
+ * `main.js` default export, its schema's `_self`, or the runtime's foundation config
26
+ * @returns {string}
27
+ */
28
+ export function defaultSectionType(foundation) {
29
+ const declared = foundation?.defaultSection
30
+ return typeof declared === 'string' && declared ? declared : DEFAULT_SECTION_TYPE
31
+ }
package/src/website.js CHANGED
@@ -16,6 +16,7 @@ import { resolveFetchConfigs, pageRouteQuery, recordPages } from './fetch-config
16
16
  import { declaredKeys } from './data-keys.js'
17
17
  import { pageRecordConfig, planFor, runPlanSync } from './page-data.js'
18
18
  import { findLayoutEntry } from './layout-name.js'
19
+ import { defaultSectionType } from './section-type.js'
19
20
  import { resolveService } from './services.js'
20
21
  import {
21
22
  routeIndex,
@@ -709,10 +710,11 @@ export default class Website {
709
710
  }
710
711
 
711
712
  /**
712
- * Get default block type from foundation config
713
+ * The type a section that names none renders with — the foundation's
714
+ * `defaultSection`, else `Section` (`section-type.js`, the rule a push fills by too).
713
715
  */
714
716
  getDefaultBlockType() {
715
- return globalThis.uniweb?.foundationConfig?.defaultSection || 'Section'
717
+ return defaultSectionType(globalThis.uniweb?.foundationConfig)
716
718
  }
717
719
 
718
720
  // ─────────────────────────────────────────────────────────────────