@uniweb/core 1.4.4 → 1.4.6

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,11 +1,12 @@
1
1
  {
2
2
  "name": "@uniweb/core",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.js",
8
8
  "./base-path": "./src/base-path.js",
9
+ "./site-address": "./src/site-address.js",
9
10
  "./records-service": "./src/records-service.js",
10
11
  "./data-paths": "./src/data-paths.js",
11
12
  "./data-script": "./src/data-script.js",
package/src/block.js CHANGED
@@ -13,6 +13,27 @@ import { normalizeTokenValue, isCssName, isCssValue } from '@uniweb/theming'
13
13
  import { sectionDomId } from './section-id.js'
14
14
  import { liftInsets } from './insets.js'
15
15
 
16
+ /**
17
+ * A video background's own fields, in its `video`: `video: <url>` is its `src`, and a `poster`,
18
+ * `sources`, `loop` or `muted` written beside it are the video's — the shorthand the reference
19
+ * documents. A video given only as `sources` takes the first one's as its `src`.
20
+ * ⛔ Until 2026-10-25 these passed through as written, while every renderer reads
21
+ * `background.video.src`: the documented `video: /videos/hero.mp4` beside a `poster:` drew nothing.
22
+ *
23
+ * @param {Object} raw - a background object whose mode is video
24
+ * @returns {Object}
25
+ */
26
+ function gatherVideoFields(raw) {
27
+ const { video, poster, sources, loop, muted, ...rest } = raw
28
+ const own = typeof video === 'string' ? { src: video } : { ...(video || {}) }
29
+ for (const [key, value] of Object.entries({ poster, sources, loop, muted })) {
30
+ if (value !== undefined && own[key] === undefined) own[key] = value
31
+ }
32
+ const first = !own.src && Array.isArray(own.sources) ? own.sources.find((s) => s?.src) : null
33
+ if (first) own.src = first.src
34
+ return { ...rest, video: own }
35
+ }
36
+
16
37
  export default class Block {
17
38
  constructor(blockData, id, page) {
18
39
  this.id = id
@@ -95,7 +116,7 @@ export default class Block {
95
116
  // Whether the section stays in view as the page scrolls — what the author wrote (`true` or
96
117
  // `false`; anything else is no answer), else its type's default from `meta.js`, which
97
118
  // `initComponent` reads, else not. The runtime applies it to the box that can stick: a
98
- // component's own markup sits in boxes as tall as itself, where `sticky` cannot move
119
+ // component's outermost element sits in boxes as tall as itself, where `sticky` cannot move
99
120
  // (2026-10-19).
100
121
  this.stickySetting = typeof stickyParam === 'boolean' ? stickyParam : null
101
122
  this.sticky = this.stickySetting ?? false
@@ -880,8 +901,8 @@ export default class Block {
880
901
  return { mode: 'color', color: normalizeTokenValue(raw) }
881
902
  }
882
903
 
883
- // Object with explicit mode — pass through
884
- if (raw.mode) return raw
904
+ // Object with explicit mode — pass through, a video's own fields gathered into it
905
+ if (raw.mode) return raw.mode === 'video' ? gatherVideoFields(raw) : raw
885
906
 
886
907
  // Normalize overlay shorthand: number → { enabled: true, type: 'dark', opacity }
887
908
  if (typeof raw.overlay === 'number') {
@@ -889,7 +910,7 @@ export default class Block {
889
910
  }
890
911
 
891
912
  // Infer mode from fields
892
- if (raw.video || raw.sources) return { mode: 'video', ...raw }
913
+ if (raw.video || raw.sources) return gatherVideoFields({ mode: 'video', ...raw })
893
914
  if (raw.image || raw.src) {
894
915
  // Support flat { src, position, size } shorthand
895
916
  if (raw.src) {
package/src/datastore.js CHANGED
@@ -332,6 +332,24 @@ export default class DataStore {
332
332
  return this._inflight
333
333
  }
334
334
 
335
+ /**
336
+ * Resolves once no fetch is in flight — waiting too for any that a settling one started, since
337
+ * one answer can start another fetch. For what must see a page's answers in place, as the dev
338
+ * check of sticky elements does (`@uniweb/runtime`), not for code that needs one answer: that
339
+ * awaits its own fetch. An entry left registered after its promise settled is waited on once.
340
+ *
341
+ * @returns {Promise<void>}
342
+ */
343
+ async whenIdle() {
344
+ const waited = new Set()
345
+ for (;;) {
346
+ const pending = [...this._inflight.values()].map((entry) => entry.promise).filter((p) => p && !waited.has(p))
347
+ if (!pending.length) return
348
+ for (const promise of pending) waited.add(promise)
349
+ await Promise.allSettled(pending)
350
+ }
351
+ }
352
+
335
353
  /**
336
354
  * Flush cache and in-flight map. Listeners are preserved so subscribers
337
355
  * that outlive the cache (kit hooks waiting on a key) aren't orphaned.
package/src/index.js CHANGED
@@ -109,6 +109,8 @@ export {
109
109
  // since it must not pull the package root into an SSR/Worker bundle.
110
110
  export { resolveService, resolveServiceUrl, readServiceOptions } from './services.js'
111
111
  export { applyBasePath } from './base-path.js'
112
+ // A site's address and the absolute URLs stated on it — also the leaf `@uniweb/core/site-address`.
113
+ export { siteAddress, pageUrl, absoluteUrl } from './site-address.js'
112
114
 
113
115
  /**
114
116
  * The singleton Uniweb instance.
package/src/page.js CHANGED
@@ -9,6 +9,7 @@ import Block from './block.js'
9
9
  import ObservableState from './observable-state.js'
10
10
  import { normalizeSeo } from './seo.js'
11
11
  import { landingRoute } from './resolve-route.js'
12
+ import { absoluteUrl } from './site-address.js'
12
13
 
13
14
  // Normalize a page's nav-visibility into a deduped hideIn array (the layout-area
14
15
  // names the page is suppressed from). Reads the canonical hideIn (array, or a single
@@ -179,29 +180,52 @@ export default class Page {
179
180
  }
180
181
 
181
182
  /**
182
- * Get metadata for head tags
183
+ * The page's head metadata — the one source for both writers of a page's head: the prerendered
184
+ * HTML (`injectPageContent`) and the browser (`useHeadMeta`), so a crawler and a visitor read the
185
+ * same tags.
186
+ *
187
+ * URLs are absolute, on the site's address (`./site-address.js`): the page's own URL is its
188
+ * canonical and `og:url` unless it authors a canonical, and `alternates` is its URL in each locale
189
+ * the site serves. With the address unknown they are left out — a relative `og:url` is not one,
190
+ * and a canonical guessed from where the page was loaded may name another copy of the site.
191
+ * ⛔ Until 2026-10-25 `og:url` was the bare route and a page had a canonical only when it authored
192
+ * one, and the static build wrote the HOME page's into the shell every page is rendered into.
193
+ *
183
194
  * @returns {Object} Head metadata
184
195
  */
185
196
  getHeadMeta() {
197
+ const website = this.website
186
198
  const resolvedTitle = this.getTitle()
187
199
  // Site-level seo provides the defaults; a page overrides any field it sets.
188
200
  // (Website.seo / Website.keywords are the normalized site-root values.)
189
- const site = this.website?.seo || {}
190
- const siteKeywords = this.website?.keywords || null
201
+ const site = website?.seo || {}
202
+ const siteKeywords = website?.keywords || null
203
+ const address = website?.url || null
204
+ const own = this.description || null
205
+ // The site's description describes its home page, and no other: one every page shared would
206
+ // describe none of them.
207
+ const home = this.getNavRoute() === '/' ? website?.description || null : null
208
+ const canonical = this.seo.canonical
209
+ ? absoluteUrl(address, this.seo.canonical) || this.seo.canonical
210
+ : website?.getPageUrl?.(this) ?? null
211
+ const image = this.seo.image || site.image || null
191
212
  return {
192
213
  title: resolvedTitle,
193
- description: this.description,
214
+ description: own || home,
194
215
  keywords: this.keywords || siteKeywords,
195
- canonical: this.seo.canonical,
216
+ canonical,
196
217
  robots: this.seo.noindex || site.noindex ? 'noindex, nofollow' : null,
218
+ alternates: website?.getPageAlternates?.(this) ?? [],
197
219
  og: {
198
220
  // Page content wins; site-level seo only fills gaps. Image is the
199
221
  // exception authors most want cascaded (the shared social card).
200
222
  title: this.seo.ogTitle || resolvedTitle || site.ogTitle,
201
- description: this.seo.ogDescription || this.description || site.ogDescription,
202
- image: this.seo.image || site.image,
203
- url: this.route,
223
+ description: this.seo.ogDescription || own || site.ogDescription || home,
224
+ image: image ? absoluteUrl(address, image) || image : null,
225
+ url: canonical,
226
+ siteName: website?.name || null,
204
227
  },
228
+ twitter: { site: this.seo.twitterHandle || site.twitterHandle || null },
205
229
  }
206
230
  }
207
231
 
package/src/seo.js CHANGED
@@ -8,11 +8,12 @@
8
8
  * Field names are camelCase to match the framework's authored convention
9
9
  * (`defaultLanguage`, etc.). `changefreq`/`priority` are sitemap hints — only
10
10
  * meaningful per-page, but carried uniformly so the shape never drifts.
11
+ * `twitterHandle` names the site's account (`twitter:site`, e.g. `@acme`).
11
12
  *
12
13
  * @param {Object} [raw] - raw `seo:` block from site.yml / page.yml
13
14
  * @returns {{ noindex: boolean, image: string|null, ogTitle: string|null,
14
15
  * ogDescription: string|null, canonical: string|null, changefreq: string|null,
15
- * priority: (string|number)|null }}
16
+ * priority: (string|number)|null, twitterHandle: string|null }}
16
17
  */
17
18
  export function normalizeSeo(raw) {
18
19
  const seo = raw || {}
@@ -24,5 +25,6 @@ export function normalizeSeo(raw) {
24
25
  canonical: seo.canonical || null,
25
26
  changefreq: seo.changefreq || null,
26
27
  priority: seo.priority || null,
28
+ twitterHandle: seo.twitterHandle || null,
27
29
  }
28
30
  }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * A SITE'S ADDRESS — the absolute URL of its root as the public reaches it: scheme, host and base
3
+ * path, `https://octocat.github.io/handbook/`. A zero-dependency leaf, like `base-path.js`,
4
+ * because the runtime, `@uniweb/projections` (Worker-safe) and the build all state absolute URLs and
5
+ * must state them by one rule.
6
+ *
7
+ * Every absolute URL a site states about itself is its address joined with a path: a page's own —
8
+ * its canonical, `og:url`, its hreflang alternates, its sitemap entry — by `pageUrl`, and anything
9
+ * the site's content links to — a social card image — by `absoluteUrl`. ⛔ Until 2026-10-25 five
10
+ * places composed one, three ways: the sitemap and the page tags joined an origin to a route and
11
+ * left the base path out, the agent index put it in, and `og:url` was a bare route. A site served
12
+ * under a base path got wrong absolute URLs whatever it set.
13
+ *
14
+ * ⭐ THE ADDRESS IS STATED BY WHOEVER DEPLOYS THE SITE, and reaches the runtime in the payload as
15
+ * `config.url`, beside `config.base`: a host states its own, and a static build states what its
16
+ * deploy target or its author gives it (`@uniweb/build`). Nothing here derives one — not from a
17
+ * request, not from the document's location — so an unknown address is null, and a caller states no
18
+ * absolute URL rather than a guessed one.
19
+ *
20
+ * ⭐ A PAGE'S URL IS THE ONE THAT ANSWERS WITH THE PAGE. A static build writes `<route>/index.html`,
21
+ * and a static host answers `/books/x/` with that file and `/books/x` with a redirect to it, so there
22
+ * a page's URL ends in `/`. That is `config.trailingSlash`, stated with the address by whoever lays
23
+ * out what is served.
24
+ *
25
+ * @module @uniweb/core/site-address
26
+ */
27
+
28
+ import { applyBasePath } from './base-path.js'
29
+
30
+ /**
31
+ * A site's address as given, normalized: an absolute http(s) URL with a trailing slash and no query
32
+ * or fragment. Null when it is not one.
33
+ *
34
+ * @param {*} value
35
+ * @returns {string|null}
36
+ */
37
+ export function siteAddress(value) {
38
+ if (typeof value !== 'string' || !value.trim()) return null
39
+ let url
40
+ try {
41
+ url = new URL(value.trim())
42
+ } catch {
43
+ return null
44
+ }
45
+ // A string that parses is not yet an address: `localhost:8080` parses, with `localhost:` as its
46
+ // scheme. Only http(s) with a host is one.
47
+ if ((url.protocol !== 'http:' && url.protocol !== 'https:') || !url.host) return null
48
+ url.search = ''
49
+ url.hash = ''
50
+ if (!url.pathname.endsWith('/')) url.pathname += '/'
51
+ return url.href
52
+ }
53
+
54
+ /**
55
+ * A page's URL on the site at `address`: its route joined under the address, ending in `/` when the
56
+ * site's pages are served as directories. The root is the address itself.
57
+ *
58
+ * @param {string} address - the site's address (`siteAddress`)
59
+ * @param {string} route - the page's route from the site's root, as its locale shows it and with no
60
+ * base applied (`/books/x`, `/fr/livres/x`) — or an absolute URL, a locale on a domain of its own
61
+ * (`localeUrl`), returned as it is
62
+ * @param {{ trailingSlash?: boolean }} [options]
63
+ * @returns {string|null} null when the address is unknown
64
+ */
65
+ export function pageUrl(address, route, { trailingSlash = false } = {}) {
66
+ if (typeof route === 'string' && /^https?:\/\//i.test(route)) return route
67
+ const root = siteAddress(address)
68
+ if (!root || typeof route !== 'string') return null
69
+ const path = route.replace(/^\/+/, '').replace(/\/+$/, '')
70
+ if (!path) return root
71
+ // Resolved rather than concatenated, so a route that holds what a URL path cannot is encoded.
72
+ return new URL(`${path}${trailingSlash ? '/' : ''}`, root).href
73
+ }
74
+
75
+ /**
76
+ * The absolute URL of something the site's content links to — a social card image, a path an author
77
+ * wrote — on the site at `address`. A path from the site's root gets the address's base path as any
78
+ * href of the site does (`applyBasePath`, so never twice), a protocol-relative one the address's
79
+ * scheme, and an absolute URL is returned as it is.
80
+ *
81
+ * @param {string} address - the site's address (`siteAddress`)
82
+ * @param {string} href
83
+ * @returns {string|null} null when `href` is not absolute and the address is unknown
84
+ */
85
+ export function absoluteUrl(address, href) {
86
+ if (typeof href !== 'string' || !href) return null
87
+ if (/^[a-z][a-z\d+.-]*:/i.test(href)) return href
88
+ const root = siteAddress(address)
89
+ if (!root) return null
90
+ if (href.startsWith('//')) return new URL(href, root).href
91
+ const { origin, pathname } = new URL(root)
92
+ const path = href.startsWith('/') ? applyBasePath(href, pathname.replace(/\/$/, '')) : `${pathname}${href}`
93
+ return new URL(path, origin).href
94
+ }
package/src/website.js CHANGED
@@ -10,6 +10,7 @@ import EntityStore from './entity-store.js'
10
10
  import FetcherDispatcher from './fetcher-dispatcher.js'
11
11
  import ObservableState from './observable-state.js'
12
12
  import { normalizeSeo } from './seo.js'
13
+ import { siteAddress, pageUrl } from './site-address.js'
13
14
  import { resolveDefaultLocale, localeLabel } from './locale-config.js'
14
15
  import { matchDynamicRoute, recordTitle, recordImage, routeBinding, routeParamName, parentRouteOf } from './route-match.js'
15
16
  import { resolveFetchConfigs, pageRouteQuery, recordPages } from './fetch-config.js'
@@ -93,7 +94,10 @@ export default class Website {
93
94
  // ─── Fields populated by _applyContent (declared up front so Object.seal works) ───
94
95
  this.name = ''
95
96
  this.description = ''
96
- this.url = ''
97
+ // The site's address and how its pages are addressed, as whoever serves it states them
98
+ // (`config.url`, `config.trailingSlash`; `./site-address.js`). Null: unknown.
99
+ this.url = null
100
+ this.trailingSlash = false
97
101
  this._layoutSets = {}
98
102
  this.notFoundPage = null
99
103
  this._dynamicPageData = new Map()
@@ -147,7 +151,8 @@ export default class Website {
147
151
 
148
152
  this.name = config.name || ''
149
153
  this.description = config.description || ''
150
- this.url = config.url || ''
154
+ this.url = siteAddress(config.url)
155
+ this.trailingSlash = config.trailingSlash === true
151
156
 
152
157
  // Layout areas (header/footer/left/right pages scoped per named layout).
153
158
  this._layoutSets = {}
@@ -1008,6 +1013,45 @@ export default class Website {
1008
1013
  return localeUrl(this._routeIndex, localeCode, targetRoute, this._routeContext())
1009
1014
  }
1010
1015
 
1016
+ /**
1017
+ * A page's URL as the public reaches it — absolute, on the site's address — in a locale (default:
1018
+ * the active one): the URL its canonical, `og:url`, hreflang and sitemap entry name. By the rule a
1019
+ * link to another locale is made with (`localeUrl`), so a translated route is the route that
1020
+ * locale shows. Null when the site's address is unknown (`config.url`): a URL guessed from where
1021
+ * the page happens to be loaded could be another copy of the site.
1022
+ *
1023
+ * @param {Object|string} page - A Page, or a route
1024
+ * @param {string} [localeCode]
1025
+ * @returns {string|null}
1026
+ */
1027
+ getPageUrl(page, localeCode = this.activeLocale) {
1028
+ if (!this.url) return null
1029
+ const route = typeof page === 'string' ? page : page?.getNavRoute?.() ?? page?.route
1030
+ if (typeof route !== 'string') return null
1031
+ const path = localeUrl(this._routeIndex, localeCode, route, this._routeContext())
1032
+ return pageUrl(this.url, path, { trailingSlash: this.trailingSlash })
1033
+ }
1034
+
1035
+ /**
1036
+ * A page's URL in each locale the site serves, and the site's default as `x-default` — the
1037
+ * hreflang a crawler reads to send each reader to their own. Empty for a site in one locale, or
1038
+ * whose address is unknown.
1039
+ *
1040
+ * @param {Object|string} page - A Page, or a route
1041
+ * @returns {Array<{ hreflang: string, url: string }>}
1042
+ */
1043
+ getPageAlternates(page) {
1044
+ if (!this.url || this.locales.length < 2) return []
1045
+ const alternates = []
1046
+ for (const { code } of this.locales) {
1047
+ const url = this.getPageUrl(page, code)
1048
+ if (url) alternates.push({ hreflang: code, url })
1049
+ }
1050
+ const fallback = this.getPageUrl(page, this.siteDefaultLocale)
1051
+ if (fallback) alternates.push({ hreflang: 'x-default', url: fallback })
1052
+ return alternates
1053
+ }
1054
+
1011
1055
  /**
1012
1056
  * Show the page in another locale.
1013
1057
  *