@uniweb/core 1.4.3 → 1.4.5
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 +2 -1
- package/src/block.js +1 -1
- package/src/datastore.js +18 -0
- package/src/index.js +2 -0
- package/src/page.js +32 -8
- package/src/resolve-route.js +23 -9
- package/src/seo.js +3 -1
- package/src/site-address.js +94 -0
- package/src/uniweb.js +4 -0
- package/src/website.js +80 -2
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/core",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
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
|
@@ -95,7 +95,7 @@ export default class Block {
|
|
|
95
95
|
// Whether the section stays in view as the page scrolls — what the author wrote (`true` or
|
|
96
96
|
// `false`; anything else is no answer), else its type's default from `meta.js`, which
|
|
97
97
|
// `initComponent` reads, else not. The runtime applies it to the box that can stick: a
|
|
98
|
-
// component's
|
|
98
|
+
// component's outermost element sits in boxes as tall as itself, where `sticky` cannot move
|
|
99
99
|
// (2026-10-19).
|
|
100
100
|
this.stickySetting = typeof stickyParam === 'boolean' ? stickyParam : null
|
|
101
101
|
this.sticky = this.stickySetting ?? false
|
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
|
-
*
|
|
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 =
|
|
190
|
-
const siteKeywords =
|
|
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:
|
|
214
|
+
description: own || home,
|
|
194
215
|
keywords: this.keywords || siteKeywords,
|
|
195
|
-
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 ||
|
|
202
|
-
image:
|
|
203
|
-
url:
|
|
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/resolve-route.js
CHANGED
|
@@ -150,29 +150,43 @@ export function reverseTranslateRoute(site, displayRoute, locale) {
|
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
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}
|
|
160
|
+
* @returns {string}
|
|
162
161
|
*/
|
|
163
|
-
export function
|
|
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
|
-
|
|
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/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/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
|
@@ -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'
|
|
@@ -22,9 +23,11 @@ import {
|
|
|
22
23
|
routeIndex,
|
|
23
24
|
resolveRoute as resolveRouteIn,
|
|
24
25
|
localeUrl,
|
|
26
|
+
canonicalRoute,
|
|
25
27
|
translateRoute as translateRouteIn,
|
|
26
28
|
reverseTranslateRoute as reverseTranslateRouteIn,
|
|
27
29
|
} from './resolve-route.js'
|
|
30
|
+
import { applyBasePath } from './base-path.js'
|
|
28
31
|
|
|
29
32
|
/**
|
|
30
33
|
* Services whose provider is not simply an address, keyed by name.
|
|
@@ -91,7 +94,10 @@ export default class Website {
|
|
|
91
94
|
// ─── Fields populated by _applyContent (declared up front so Object.seal works) ───
|
|
92
95
|
this.name = ''
|
|
93
96
|
this.description = ''
|
|
94
|
-
|
|
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
|
|
95
101
|
this._layoutSets = {}
|
|
96
102
|
this.notFoundPage = null
|
|
97
103
|
this._dynamicPageData = new Map()
|
|
@@ -145,7 +151,8 @@ export default class Website {
|
|
|
145
151
|
|
|
146
152
|
this.name = config.name || ''
|
|
147
153
|
this.description = config.description || ''
|
|
148
|
-
this.url = config.url
|
|
154
|
+
this.url = siteAddress(config.url)
|
|
155
|
+
this.trailingSlash = config.trailingSlash === true
|
|
149
156
|
|
|
150
157
|
// Layout areas (header/footer/left/right pages scoped per named layout).
|
|
151
158
|
this._layoutSets = {}
|
|
@@ -1006,6 +1013,77 @@ export default class Website {
|
|
|
1006
1013
|
return localeUrl(this._routeIndex, localeCode, targetRoute, this._routeContext())
|
|
1007
1014
|
}
|
|
1008
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
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* Show the page in another locale.
|
|
1057
|
+
*
|
|
1058
|
+
* A locale is its own document, with its own content and routes, so this is a page load of
|
|
1059
|
+
* `getLocaleUrl()` with the base path applied. A host that hands the runtime each locale's
|
|
1060
|
+
* content itself, like an editor's preview, switches its own way instead
|
|
1061
|
+
* (`uniweb.localeSwitch`): it is told the locale, the page's canonical route and the URL a
|
|
1062
|
+
* page load would go to.
|
|
1063
|
+
*
|
|
1064
|
+
* For a menu that switches from code, like a `<select>`. A menu of links needs no call:
|
|
1065
|
+
* `<Link reload href={website.getLocaleUrl(code)}>` loads the page, and the runtime hands a
|
|
1066
|
+
* click on a link to another locale's page to such a host the same way.
|
|
1067
|
+
*
|
|
1068
|
+
* @param {string} localeCode - One of `getLocales()`
|
|
1069
|
+
* @param {string} [route] - The page, canonical or as the active locale shows it
|
|
1070
|
+
* (default: the current page)
|
|
1071
|
+
* @returns {boolean} false when the site has no such locale, and nothing happens
|
|
1072
|
+
*/
|
|
1073
|
+
switchLocale(localeCode, route = null) {
|
|
1074
|
+
if (!this.getLocale(localeCode)) return false
|
|
1075
|
+
const context = this._routeContext()
|
|
1076
|
+
const page = route || this.activePage.getNavRoute()
|
|
1077
|
+
const url = applyBasePath(localeUrl(this._routeIndex, localeCode, page, context), this.basePath)
|
|
1078
|
+
const hostSwitch = globalThis.uniweb?.localeSwitch
|
|
1079
|
+
if (hostSwitch) {
|
|
1080
|
+
hostSwitch({ locale: localeCode, route: canonicalRoute(this._routeIndex, page, context), url })
|
|
1081
|
+
} else if (typeof window !== 'undefined') {
|
|
1082
|
+
window.location.assign(url)
|
|
1083
|
+
}
|
|
1084
|
+
return true
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1009
1087
|
/**
|
|
1010
1088
|
* Get locale info by code
|
|
1011
1089
|
* @param {string} localeCode - Locale code
|