@uniweb/core 1.4.9 → 1.4.11
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 +3 -3
- package/src/entity-store.js +6 -1
- package/src/page-data.js +42 -7
- package/src/page-shell.js +5 -3
- package/src/resolve-route.js +34 -0
- package/src/website.js +8 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/core",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.11",
|
|
4
4
|
"description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"vitest": "^4.1.7"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@uniweb/
|
|
57
|
-
"@uniweb/
|
|
56
|
+
"@uniweb/theming": "~1.2.1",
|
|
57
|
+
"@uniweb/semantic-parser": "~1.13.0"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"test": "vitest run"
|
package/src/entity-store.js
CHANGED
|
@@ -178,7 +178,10 @@ export default class EntityStore {
|
|
|
178
178
|
peekRecord: (id) => dispatcher.peekRecord?.(id),
|
|
179
179
|
})
|
|
180
180
|
const ctx = this._ctx(probe)
|
|
181
|
-
return runPlan(plan, {
|
|
181
|
+
return runPlan(plan, {
|
|
182
|
+
dispatch: (request) => dispatcher.dispatch(request, ctx),
|
|
183
|
+
peek: (request) => dispatcher.peek?.(request, ctx),
|
|
184
|
+
})
|
|
182
185
|
}
|
|
183
186
|
|
|
184
187
|
/**
|
|
@@ -340,6 +343,8 @@ export default class EntityStore {
|
|
|
340
343
|
const ctx = this._ctx(block, { signal })
|
|
341
344
|
const { data, errors } = await runPlan(this._plan(block, fills, route, dispatcher), {
|
|
342
345
|
dispatch: (request) => dispatcher.dispatch(request, ctx),
|
|
346
|
+
// What the plan uses without asking, when it is held (`keyProgram`'s held steps).
|
|
347
|
+
peek: (request) => dispatcher.peek?.(request, ctx),
|
|
343
348
|
onFailure: (key, cfg, message) => reportFetchFailure(this.dev, block, key, cfg, message),
|
|
344
349
|
})
|
|
345
350
|
|
package/src/page-data.js
CHANGED
|
@@ -200,6 +200,8 @@ function heldWhole(peekRecord, match) {
|
|
|
200
200
|
export function* keyProgram(cfg, { dynamicContext = null, route = null, held = undefined, peekRecord = null, current: forced = null, opportunistic = false, whole = false, single = false } = {}) {
|
|
201
201
|
/** A step this caller can do without: a miss answers `{ missing: true }` instead of stopping. */
|
|
202
202
|
const maybe = (request) => (opportunistic ? { request, optional: true } : request)
|
|
203
|
+
/** A step answered from what is held, on both drivers: never dispatched, `{ missing: true }` when not held. */
|
|
204
|
+
const fromHeld = (request) => ({ request, held: true })
|
|
203
205
|
// The block holds this fetch's answer already — a static build prerendered it.
|
|
204
206
|
if (held !== undefined) return { value: held }
|
|
205
207
|
|
|
@@ -232,6 +234,20 @@ export function* keyProgram(cfg, { dynamicContext = null, route = null, held = u
|
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
if (current === 'only' && cfg.ask) {
|
|
237
|
+
// ⭐ WHAT IS HELD, FIRST. A page that lists this query keeps its set, and the record is in it:
|
|
238
|
+
// its brief, and — when another question brought it whole — the record itself, in the record
|
|
239
|
+
// index. Either answers without a request. Only a positive match counts: a held set without the
|
|
240
|
+
// record proves nothing, since the service may answer the set in pages, so the record is asked.
|
|
241
|
+
// ⛔ Until 2026-10-27 this lane asked the record's question even with its query's set held, where
|
|
242
|
+
// the file lane finds the record in the set and asks for it whole only when it is not held whole.
|
|
243
|
+
const set = yield fromHeld(routeSelection(cfg))
|
|
244
|
+
const found = Array.isArray(set.data)
|
|
245
|
+
? set.data.find(isPageRecord(dynamicContext, cfg.routeField)) ?? null
|
|
246
|
+
: null
|
|
247
|
+
if (found && !whole) return { value: [found] }
|
|
248
|
+
const foundWhole = found ? heldWhole(peekRecord, found) : null
|
|
249
|
+
if (foundWhole) return { value: [foundWhole] }
|
|
250
|
+
|
|
235
251
|
// ⭐ THE RECORDS SERVICE needs no list to find the record: the record is the query's set
|
|
236
252
|
// narrowed by the route's handle, so its one question answers the record if the set holds it
|
|
237
253
|
// and `[]` if not — whole or its brief, as the component expects.
|
|
@@ -351,13 +367,24 @@ function recordRequest(cfg, record) {
|
|
|
351
367
|
return buildDetailConfig(cfg, { paramName: 'slug', paramValue: String(handle), record })
|
|
352
368
|
}
|
|
353
369
|
|
|
354
|
-
/**
|
|
370
|
+
/**
|
|
371
|
+
* A yielded step as `{ request, optional, held }` entries: one request, or several asked together.
|
|
372
|
+
* `held` is answered from the cache on both drivers and never dispatched.
|
|
373
|
+
*/
|
|
355
374
|
function asSteps(value) {
|
|
356
375
|
return (Array.isArray(value) ? value : [value]).map((step) =>
|
|
357
|
-
step && step.request
|
|
376
|
+
step && step.request
|
|
377
|
+
? { request: step.request, optional: !!step.optional, held: !!step.held }
|
|
378
|
+
: { request: step, optional: false, held: false }
|
|
358
379
|
)
|
|
359
380
|
}
|
|
360
381
|
|
|
382
|
+
/** A held step's answer: what the cache holds, or `{ missing: true }` — never a request. */
|
|
383
|
+
function fromCache(peek, request) {
|
|
384
|
+
const cached = typeof peek === 'function' ? peek(request) : null
|
|
385
|
+
return cached ? { data: cached.data } : { missing: true }
|
|
386
|
+
}
|
|
387
|
+
|
|
361
388
|
/**
|
|
362
389
|
* Every filling key's program, ready to run.
|
|
363
390
|
*
|
|
@@ -399,11 +426,11 @@ export function runPlanSync(plan, { peek }) {
|
|
|
399
426
|
}
|
|
400
427
|
const answers = []
|
|
401
428
|
let missed = false
|
|
402
|
-
for (const { request, optional } of asSteps(step.value)) {
|
|
429
|
+
for (const { request, optional, held } of asSteps(step.value)) {
|
|
403
430
|
const cached = peek(request)
|
|
404
431
|
if (cached) answers.push({ data: cached.data })
|
|
405
432
|
// The program can do without this one and says what it makes of that.
|
|
406
|
-
else if (optional) answers.push({ missing: true })
|
|
433
|
+
else if (optional || held) answers.push({ missing: true })
|
|
407
434
|
else {
|
|
408
435
|
missed = true
|
|
409
436
|
break
|
|
@@ -429,13 +456,17 @@ export function runPlanSync(plan, { peek }) {
|
|
|
429
456
|
* a page's questions into one POST. A key whose second request depends on its first answer takes a
|
|
430
457
|
* later tick, and only that key waits; the requests of one step are issued together.
|
|
431
458
|
*
|
|
459
|
+
* A step the program marks held is answered from `peek` and never dispatched: what is already here
|
|
460
|
+
* is used, and its absence asks nothing.
|
|
461
|
+
*
|
|
432
462
|
* @param {Map<string, Generator>} plan
|
|
433
463
|
* @param {Object} io
|
|
434
464
|
* @param {(request: Object) => Promise<{ data: *, error?: string }>} io.dispatch
|
|
465
|
+
* @param {(request: Object) => ({ data: * }|null|undefined)} [io.peek] - what is held, for a held step
|
|
435
466
|
* @param {(key: string, cfg: Object|null, message: string) => void} [io.onFailure]
|
|
436
467
|
* @returns {Promise<{ data: Object, errors: Object|null }>}
|
|
437
468
|
*/
|
|
438
|
-
export async function runPlan(plan, { dispatch, onFailure = null }) {
|
|
469
|
+
export async function runPlan(plan, { dispatch, peek = null, onFailure = null }) {
|
|
439
470
|
const data = {}
|
|
440
471
|
const errors = {}
|
|
441
472
|
|
|
@@ -452,8 +483,12 @@ export async function runPlan(plan, { dispatch, onFailure = null }) {
|
|
|
452
483
|
}
|
|
453
484
|
return
|
|
454
485
|
}
|
|
455
|
-
const results = await Promise.all(
|
|
456
|
-
|
|
486
|
+
const results = await Promise.all(
|
|
487
|
+
asSteps(step.value).map(({ request, held }) => (held ? fromCache(peek, request) : dispatch(request)))
|
|
488
|
+
)
|
|
489
|
+
const answers = results.map((result) =>
|
|
490
|
+
result?.missing ? { missing: true } : { data: result?.data, error: result?.error }
|
|
491
|
+
)
|
|
457
492
|
sent = Array.isArray(step.value) ? answers : answers[0]
|
|
458
493
|
}
|
|
459
494
|
}))
|
package/src/page-shell.js
CHANGED
|
@@ -11,9 +11,11 @@
|
|
|
11
11
|
* host wrote its own shell, and one escaped `__DATA__` its own way, as invalid JSON, so no page
|
|
12
12
|
* of the site it served could start.
|
|
13
13
|
*
|
|
14
|
-
* Its output is a function of its inputs only — no request, no clock —
|
|
15
|
-
*
|
|
16
|
-
* only this code changes it, and `pageShellFingerprint` says when.
|
|
14
|
+
* Its output is a function of its inputs only — no request, no clock — so a host can cache it under
|
|
15
|
+
* a key of what it hands in, one entry serving every hostname that hands in the same. Besides its
|
|
16
|
+
* inputs, only this code changes it, and `pageShellFingerprint` says when. ⛔ Not every input is a
|
|
17
|
+
* publish-time fact: the site's address (`content.config.url`), which `__DATA__` carries, is stated
|
|
18
|
+
* by whoever serves the site and can change between publishes.
|
|
17
19
|
*
|
|
18
20
|
* ⛔ A change to what this writes that an older runtime's browser build cannot read — a new
|
|
19
21
|
* `__DATA__` encoding, a new split shape — is a breaking change: mark the commit `!` and name the
|
package/src/resolve-route.js
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
import { decodeRouteValue, matchDynamicRoute, parentRouteOf } from './route-match.js'
|
|
25
25
|
import { resolveDefaultLocale } from './locale-config.js'
|
|
26
|
+
import { siteAddress } from './site-address.js'
|
|
26
27
|
|
|
27
28
|
/** The not-found page's route. It is never a routable page of its own. */
|
|
28
29
|
export const NOT_FOUND_ROUTE = '/404'
|
|
@@ -99,9 +100,18 @@ export function routeIndex(content = {}) {
|
|
|
99
100
|
activeLocale: config.activeLocale || defaultLocale,
|
|
100
101
|
locales: servedLocales(config, siteDefaultLocale),
|
|
101
102
|
domainLocales: config.domainLocales || null,
|
|
103
|
+
// The locale the site's address (`config.url`) serves unprefixed, whichever host serves this
|
|
104
|
+
// copy: the address's domain's own, else the site's default.
|
|
105
|
+
addressLocale: addressLocaleOf(config, siteDefaultLocale),
|
|
102
106
|
}
|
|
103
107
|
}
|
|
104
108
|
|
|
109
|
+
function addressLocaleOf(config, siteDefaultLocale) {
|
|
110
|
+
const address = siteAddress(config.url)
|
|
111
|
+
const host = address ? new URL(address).host : null
|
|
112
|
+
return (host && config.domainLocales?.[host]) || siteDefaultLocale
|
|
113
|
+
}
|
|
114
|
+
|
|
105
115
|
function servedLocales(config, siteDefaultLocale) {
|
|
106
116
|
const codes = [siteDefaultLocale]
|
|
107
117
|
for (const entry of Array.isArray(config.languages) ? config.languages : []) {
|
|
@@ -202,6 +212,30 @@ export function localeUrl(site, localeCode, route, context = {}) {
|
|
|
202
212
|
return translated === '/' ? `/${localeCode}/` : `/${localeCode}${translated}`
|
|
203
213
|
}
|
|
204
214
|
|
|
215
|
+
/**
|
|
216
|
+
* ⭐ **A PAGE'S URL IN A LOCALE, AS THE SITE'S ADDRESS SERVES IT** — what its canonical, `og:url`,
|
|
217
|
+
* hreflang and sitemap entry join to `config.url` (`./site-address.js`). `localeUrl` made on the
|
|
218
|
+
* address's host rather than on the host serving this copy: the route is read as this copy shows
|
|
219
|
+
* it (`context`), and the URL is made with the locale the address serves unprefixed — its domain's
|
|
220
|
+
* own (`config.domainLocales`), else the site's default. A locale with a domain of its own is that
|
|
221
|
+
* domain, as `localeUrl` has it.
|
|
222
|
+
*
|
|
223
|
+
* ⛔ Until 2026-10-27 these URLs were made as the serving host shows the site. On a domain pinned
|
|
224
|
+
* to French, an English page's canonical, and every page's `en` and `x-default` alternates, named
|
|
225
|
+
* `/en/…` on an address that answers it with a 301 to the unprefixed URL.
|
|
226
|
+
*
|
|
227
|
+
* @param {Object} site - a locale's site content, or its `routeIndex`
|
|
228
|
+
* @param {string} localeCode - the locale the URL is for
|
|
229
|
+
* @param {string} route - the page's route, canonical or as the active locale shows it
|
|
230
|
+
* @param {{ activeLocale?: string, defaultLocale?: string }} [context] - this copy's
|
|
231
|
+
* @returns {string} a path from the address's root, or an absolute URL on a locale's own domain
|
|
232
|
+
*/
|
|
233
|
+
export function localeUrlOnAddress(site, localeCode, route, context = {}) {
|
|
234
|
+
const index = indexOf(site)
|
|
235
|
+
const target = canonicalRoute(index, route, context)
|
|
236
|
+
return localeUrl(index, localeCode, target, { activeLocale: index.siteDefaultLocale, defaultLocale: index.addressLocale })
|
|
237
|
+
}
|
|
238
|
+
|
|
205
239
|
/**
|
|
206
240
|
* The locale a path names by its prefix, and the path without it — for a host choosing which
|
|
207
241
|
* locale's content to resolve a request against. A path with no served locale's prefix is the
|
package/src/website.js
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
routeIndex,
|
|
24
24
|
resolveRoute as resolveRouteIn,
|
|
25
25
|
localeUrl,
|
|
26
|
+
localeUrlOnAddress,
|
|
26
27
|
canonicalRoute,
|
|
27
28
|
translateRoute as translateRouteIn,
|
|
28
29
|
reverseTranslateRoute as reverseTranslateRouteIn,
|
|
@@ -1049,10 +1050,12 @@ export default class Website {
|
|
|
1049
1050
|
|
|
1050
1051
|
/**
|
|
1051
1052
|
* A page's URL as the public reaches it — absolute, on the site's address — in a locale (default:
|
|
1052
|
-
* the active one): the URL its canonical, `og:url`, hreflang and sitemap entry name.
|
|
1053
|
-
*
|
|
1054
|
-
* locale
|
|
1055
|
-
*
|
|
1053
|
+
* the active one): the URL its canonical, `og:url`, hreflang and sitemap entry name. As the address
|
|
1054
|
+
* serves the site (`localeUrlOnAddress`): a translated route is the route that locale shows, a
|
|
1055
|
+
* locale with a domain of its own is that domain, and the rest are prefixed as on the address —
|
|
1056
|
+
* which is not how a domain pinned to another locale links to them. Null when the site's address is
|
|
1057
|
+
* unknown (`config.url`): a URL guessed from where the page happens to be loaded could be another
|
|
1058
|
+
* copy of the site.
|
|
1056
1059
|
*
|
|
1057
1060
|
* @param {Object|string} page - A Page, or a route
|
|
1058
1061
|
* @param {string} [localeCode]
|
|
@@ -1062,7 +1065,7 @@ export default class Website {
|
|
|
1062
1065
|
if (!this.url) return null
|
|
1063
1066
|
const route = typeof page === 'string' ? page : page?.getNavRoute?.() ?? page?.route
|
|
1064
1067
|
if (typeof route !== 'string') return null
|
|
1065
|
-
const path =
|
|
1068
|
+
const path = localeUrlOnAddress(this._routeIndex, localeCode, route, this._routeContext())
|
|
1066
1069
|
return pageUrl(this.url, path, { trailingSlash: this.trailingSlash })
|
|
1067
1070
|
}
|
|
1068
1071
|
|