@uniweb/core 0.35.2 → 0.35.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 +1 -1
- package/src/resolve-route.js +98 -33
package/package.json
CHANGED
package/src/resolve-route.js
CHANGED
|
@@ -34,7 +34,8 @@ export const RESOLUTION_STATUS = Object.freeze({
|
|
|
34
34
|
// A page's redirect — authored, or a page with no content going to its content. Temporary:
|
|
35
35
|
// what a container lands on changes when its pages do.
|
|
36
36
|
pageRedirect: 302,
|
|
37
|
-
//
|
|
37
|
+
// A URL other than the locale's own, for the same page, for good: the unprefixed locale asked
|
|
38
|
+
// for with its prefix, or a page asked for at another route than the locale shows it at.
|
|
38
39
|
localeRedirect: 301,
|
|
39
40
|
})
|
|
40
41
|
|
|
@@ -112,18 +113,27 @@ function servedLocales(config, siteDefaultLocale) {
|
|
|
112
113
|
|
|
113
114
|
const indexOf = (site) => (site && site.byRoute instanceof Map ? site : routeIndex(site))
|
|
114
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The entry whose route is the LONGEST prefix of `route`, at a segment boundary — a page below
|
|
118
|
+
* translated pages takes its nearest translated ancestor's URL. ⛔ Until 2026-10-01 the first entry
|
|
119
|
+
* in page order won, so under `/docs` → `/documents` and `/docs/intro` → `/documents/introduction`,
|
|
120
|
+
* `/docs/intro/step` became `/documents/intro/step`.
|
|
121
|
+
*/
|
|
122
|
+
function nearestPrefix(map, route) {
|
|
123
|
+
let best = null
|
|
124
|
+
for (const [from, to] of map) {
|
|
125
|
+
if (route.startsWith(from + '/') && (!best || from.length > best[0].length)) best = [from, to]
|
|
126
|
+
}
|
|
127
|
+
return best ? best[1] + route.slice(best[0].length) : route
|
|
128
|
+
}
|
|
129
|
+
|
|
115
130
|
/** A canonical route → the route this locale shows it at (`/blog` → `/noticias`, by prefix too). */
|
|
116
131
|
export function translateRoute(site, canonicalRoute, locale) {
|
|
117
132
|
const index = indexOf(site)
|
|
118
133
|
if (!locale || locale === index.siteDefaultLocale) return canonicalRoute
|
|
119
134
|
const entry = index.translations[locale]
|
|
120
135
|
if (!entry) return canonicalRoute
|
|
121
|
-
|
|
122
|
-
if (exact) return exact
|
|
123
|
-
for (const [canonical, translated] of entry.forward) {
|
|
124
|
-
if (canonicalRoute.startsWith(canonical + '/')) return translated + canonicalRoute.slice(canonical.length)
|
|
125
|
-
}
|
|
126
|
-
return canonicalRoute
|
|
136
|
+
return entry.forward.get(canonicalRoute) || nearestPrefix(entry.forward, canonicalRoute)
|
|
127
137
|
}
|
|
128
138
|
|
|
129
139
|
/**
|
|
@@ -136,12 +146,7 @@ export function reverseTranslateRoute(site, displayRoute, locale) {
|
|
|
136
146
|
const entry = index.translations[locale]
|
|
137
147
|
if (!entry) return displayRoute
|
|
138
148
|
const route = decodeRouteValue(displayRoute)
|
|
139
|
-
|
|
140
|
-
if (exact) return exact
|
|
141
|
-
for (const [translated, canonical] of entry.reverse) {
|
|
142
|
-
if (route.startsWith(translated + '/')) return canonical + route.slice(translated.length)
|
|
143
|
-
}
|
|
144
|
-
return route
|
|
149
|
+
return entry.reverse.get(route) || nearestPrefix(entry.reverse, route)
|
|
145
150
|
}
|
|
146
151
|
|
|
147
152
|
/**
|
|
@@ -204,25 +209,31 @@ export function localeOfPath(path, config = {}) {
|
|
|
204
209
|
return { locale: unprefixed, prefixed: false, path: raw }
|
|
205
210
|
}
|
|
206
211
|
|
|
212
|
+
/** The route a path asks for: decoded, the active locale's prefix off, no trailing slash. */
|
|
213
|
+
function askedRoute(path, activeLocale, defaultLocale) {
|
|
214
|
+
let stripped = decodeRouteValue(path)
|
|
215
|
+
if (activeLocale && activeLocale !== defaultLocale) {
|
|
216
|
+
const prefix = `/${activeLocale}`
|
|
217
|
+
if (stripped === prefix || stripped === `${prefix}/`) stripped = '/'
|
|
218
|
+
else if (stripped.startsWith(`${prefix}/`)) stripped = stripped.slice(prefix.length)
|
|
219
|
+
}
|
|
220
|
+
return stripped === '/' ? '/' : stripped.replace(/\/$/, '')
|
|
221
|
+
}
|
|
222
|
+
|
|
207
223
|
/**
|
|
208
224
|
* The page a path names — as `Website#getPage` has always found it — or null.
|
|
209
225
|
*
|
|
210
226
|
* In order: the active locale's prefix comes off; the page whose route is the path as given (a
|
|
211
227
|
* published payload may carry a locale's own routes); else the path reverse-translated and the
|
|
212
|
-
* page of that canonical route (a folder resolving to its `isIndex` child either way); else
|
|
213
|
-
*
|
|
214
|
-
*
|
|
228
|
+
* page of that canonical route (a folder resolving to its `isIndex` child either way); else that
|
|
229
|
+
* canonical route as this locale shows it, on a payload of the locale's own routes; else an index
|
|
230
|
+
* page whose own URL is one of them; else the parametric pages in page order, against the
|
|
231
|
+
* canonical route, the path as given and the locale's route. A static page always wins.
|
|
215
232
|
*
|
|
216
233
|
* @returns {{ page: Object, route: string, params: Object, template: string|null } | null}
|
|
217
234
|
*/
|
|
218
235
|
function findPage(index, path, { activeLocale, defaultLocale }) {
|
|
219
|
-
|
|
220
|
-
if (activeLocale && activeLocale !== defaultLocale) {
|
|
221
|
-
const prefix = `/${activeLocale}`
|
|
222
|
-
if (stripped === prefix || stripped === `${prefix}/`) stripped = '/'
|
|
223
|
-
else if (stripped.startsWith(`${prefix}/`)) stripped = stripped.slice(prefix.length)
|
|
224
|
-
}
|
|
225
|
-
const asGiven = stripped === '/' ? '/' : stripped.replace(/\/$/, '')
|
|
236
|
+
const asGiven = askedRoute(path, activeLocale, defaultLocale)
|
|
226
237
|
|
|
227
238
|
const promoted = (page) => (index.childrenOf.get(page.route) || []).find(isIndex) || page
|
|
228
239
|
const exactly = (route) => index.byRoute.get(route)
|
|
@@ -241,10 +252,18 @@ function findPage(index, path, { activeLocale, defaultLocale }) {
|
|
|
241
252
|
return { page, route: page.route, params: {}, template: null }
|
|
242
253
|
}
|
|
243
254
|
|
|
244
|
-
|
|
255
|
+
// A canonical route asked for on a payload whose pages carry this locale's own routes.
|
|
256
|
+
const shown = translateRoute(index, canonical, activeLocale)
|
|
257
|
+
const translated = shown !== asGiven ? exactly(shown) : null
|
|
258
|
+
if (translated) {
|
|
259
|
+
const page = promoted(translated)
|
|
260
|
+
return { page, route: page.route, params: {}, template: null }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const indexPage = index.pages.find((p) => isIndex(p) && (navRoute(p) === canonical || navRoute(p) === shown))
|
|
245
264
|
if (indexPage) return { page: indexPage, route: indexPage.route, params: {}, template: null }
|
|
246
265
|
|
|
247
|
-
for (const candidate of new Set([canonical, asGiven])) {
|
|
266
|
+
for (const candidate of new Set([canonical, asGiven, shown])) {
|
|
248
267
|
for (const page of index.pages) {
|
|
249
268
|
if (!page.route.includes(':')) continue
|
|
250
269
|
const hit = matchDynamicRoute(page.route, candidate)
|
|
@@ -301,6 +320,11 @@ export function landingRoute(site, page, seen = new Set()) {
|
|
|
301
320
|
* 6. Otherwise the page. A parametric page's record may still not exist; that is the render's
|
|
302
321
|
* data step to say.
|
|
303
322
|
*
|
|
323
|
+
* ⭐ **One URL per page per language** (ruled 2026-10-01): a page asked for at another route than
|
|
324
|
+
* this locale shows it at — `/fr/blog` where French shows `/fr/blogue` — is a `301` there
|
|
325
|
+
* (`localeOwnUrl`), before 4 and 6. A redirect (3) and a container (5) already send the visitor
|
|
326
|
+
* to this locale's URL for their destination, in one hop.
|
|
327
|
+
*
|
|
304
328
|
* `status` is what a host that sends one sends. A static host has its own ways to say each kind.
|
|
305
329
|
*
|
|
306
330
|
* @param {Object} site - one locale's site content (`{ pages, config, notFound? }`), or its `routeIndex`
|
|
@@ -327,17 +351,24 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
327
351
|
if (!found) return { kind: 'notFound', status: RESOLUTION_STATUS.notFound, page: index.notFound }
|
|
328
352
|
const { page } = found
|
|
329
353
|
|
|
330
|
-
// 3 ·
|
|
354
|
+
// 3 · what an author wrote on the page: a redirect goes to its own destination, in one hop
|
|
331
355
|
if (typeof page.redirect === 'string' && page.redirect) {
|
|
332
356
|
const location = authoredRedirectLocation(index, found.route, page.redirect, { activeLocale, defaultLocale })
|
|
333
357
|
return { ...redirect(location, RESOLUTION_STATUS.pageRedirect, 'authored'), ...found }
|
|
334
358
|
}
|
|
359
|
+
|
|
360
|
+
// 4 · 6 · asked for at another route than this locale shows the page at: there, for good. The
|
|
361
|
+
// redirect still names the page, so a Website still finds it.
|
|
362
|
+
const ownUrl = () => {
|
|
363
|
+
const location = localeOwnUrl(index, raw, { activeLocale, defaultLocale })
|
|
364
|
+
return location && { ...redirect(location, RESOLUTION_STATUS.localeRedirect, 'locale'), ...found }
|
|
365
|
+
}
|
|
335
366
|
if (typeof page.rewrite === 'string' && page.rewrite) {
|
|
336
|
-
return { kind: 'rewrite', target: page.rewrite, ...found }
|
|
367
|
+
return ownUrl() || { kind: 'rewrite', target: page.rewrite, ...found }
|
|
337
368
|
}
|
|
338
369
|
|
|
339
370
|
// 5 · a page with no content goes to its content. The DECISION is canonical (`page.route`);
|
|
340
|
-
// the DESTINATION is this locale's URL for it.
|
|
371
|
+
// the DESTINATION is this locale's URL for it, in one hop.
|
|
341
372
|
if (!hasContent(page)) {
|
|
342
373
|
const landing = landingRoute(index, page)
|
|
343
374
|
if (landing && landing !== page.route) {
|
|
@@ -346,7 +377,27 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
346
377
|
}
|
|
347
378
|
}
|
|
348
379
|
|
|
349
|
-
return { kind: 'page', status: RESOLUTION_STATUS.page, ...found }
|
|
380
|
+
return ownUrl() || { kind: 'page', status: RESOLUTION_STATUS.page, ...found }
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* ⭐ THE URL THIS LOCALE SHOWS A PAGE AT, when the path asked for it at another route — the
|
|
385
|
+
* canonical route under a translated locale (`/fr/blog` where French shows `/fr/blogue`), or a
|
|
386
|
+
* translated folder with an untranslated child (`/fr/documents/intro`). Null when the path is that
|
|
387
|
+
* route already, which it always is in the site's default locale, whose routes are the canonical
|
|
388
|
+
* ones. A trailing slash or a percent-encoding is not another route.
|
|
389
|
+
*
|
|
390
|
+
* Ruled 2026-10-01 [Diego]: one URL per page per language, a `301` like the unprefixed locale's
|
|
391
|
+
* own prefix. ⛔ Until then `/fr/blog` was the French page on a payload of canonical routes and
|
|
392
|
+
* not found on one whose pages carry the French routes.
|
|
393
|
+
*
|
|
394
|
+
* @returns {string|null}
|
|
395
|
+
*/
|
|
396
|
+
function localeOwnUrl(index, path, { activeLocale, defaultLocale }) {
|
|
397
|
+
const asked = askedRoute(path, activeLocale, defaultLocale)
|
|
398
|
+
const canonical = reverseTranslateRoute(index, asked, activeLocale)
|
|
399
|
+
if (translateRoute(index, canonical, activeLocale) === asked) return null
|
|
400
|
+
return localeUrl(index, activeLocale, canonical, { activeLocale, defaultLocale })
|
|
350
401
|
}
|
|
351
402
|
|
|
352
403
|
function redirect(location, status, reason) {
|
|
@@ -408,14 +459,21 @@ function removeDotSegments(path) {
|
|
|
408
459
|
* its translated slugs — with any query or fragment kept.
|
|
409
460
|
* - Any other path — a file, a route the site does not hold: as written.
|
|
410
461
|
*
|
|
462
|
+
* The author wrote the target against the CANONICAL routes, whatever routes the payload's pages
|
|
463
|
+
* carry: a relative one resolves against the page's canonical route, and a page is found by its
|
|
464
|
+
* canonical route or, on a payload whose pages carry this locale's own routes, through the
|
|
465
|
+
* translations. Both kinds of payload give one answer.
|
|
466
|
+
*
|
|
411
467
|
* ⛔ Until 2026-09-30 every authored redirect went as written, so a French visitor was sent to the
|
|
412
468
|
* English page, and a relative target was left for the browser to resolve against the URL it had
|
|
413
|
-
* asked for rather than the page.
|
|
469
|
+
* asked for rather than the page. ⛔ Until 2026-10-01 the target was looked up only as a route of
|
|
470
|
+
* the payload, so on a payload of French routes `/blog` named no page and the French visitor was
|
|
471
|
+
* still sent to the English one.
|
|
414
472
|
*
|
|
415
473
|
* @returns {string}
|
|
416
474
|
*/
|
|
417
475
|
function authoredRedirectLocation(index, route, target, { activeLocale, defaultLocale }) {
|
|
418
|
-
const resolved = authoredRedirectTarget(route, target)
|
|
476
|
+
const resolved = authoredRedirectTarget(reverseTranslateRoute(index, route, activeLocale), target)
|
|
419
477
|
if (!resolved.startsWith('/') || resolved.startsWith('//')) return resolved
|
|
420
478
|
const cut = resolved.search(/[?#]/)
|
|
421
479
|
const path = cut < 0 ? resolved : resolved.slice(0, cut)
|
|
@@ -423,7 +481,14 @@ function authoredRedirectLocation(index, route, target, { activeLocale, defaultL
|
|
|
423
481
|
const first = path.split('/')[1] || ''
|
|
424
482
|
if (first && index.locales.includes(first)) return resolved
|
|
425
483
|
const canonical = path === '/' ? '/' : path.replace(/\/$/, '')
|
|
426
|
-
|
|
427
|
-
if (!named) return resolved
|
|
484
|
+
if (!namesPage(index, canonical, activeLocale)) return resolved
|
|
428
485
|
return localeUrl(index, activeLocale, canonical, { activeLocale, defaultLocale }) + suffix
|
|
429
486
|
}
|
|
487
|
+
|
|
488
|
+
/** Whether a canonical route names a page of the payload: by that route, or by this locale's. */
|
|
489
|
+
function namesPage(index, canonical, locale) {
|
|
490
|
+
const asCanonical = { activeLocale: index.siteDefaultLocale, defaultLocale: index.siteDefaultLocale }
|
|
491
|
+
return Boolean(
|
|
492
|
+
findPage(index, canonical, asCanonical) || findPage(index, translateRoute(index, canonical, locale), asCanonical),
|
|
493
|
+
)
|
|
494
|
+
}
|