@uniweb/core 0.35.1 → 0.35.3
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 +109 -14
package/package.json
CHANGED
package/src/resolve-route.js
CHANGED
|
@@ -112,18 +112,27 @@ function servedLocales(config, siteDefaultLocale) {
|
|
|
112
112
|
|
|
113
113
|
const indexOf = (site) => (site && site.byRoute instanceof Map ? site : routeIndex(site))
|
|
114
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The entry whose route is the LONGEST prefix of `route`, at a segment boundary — a page below
|
|
117
|
+
* translated pages takes its nearest translated ancestor's URL. ⛔ Until 2026-10-01 the first entry
|
|
118
|
+
* in page order won, so under `/docs` → `/documents` and `/docs/intro` → `/documents/introduction`,
|
|
119
|
+
* `/docs/intro/step` became `/documents/intro/step`.
|
|
120
|
+
*/
|
|
121
|
+
function nearestPrefix(map, route) {
|
|
122
|
+
let best = null
|
|
123
|
+
for (const [from, to] of map) {
|
|
124
|
+
if (route.startsWith(from + '/') && (!best || from.length > best[0].length)) best = [from, to]
|
|
125
|
+
}
|
|
126
|
+
return best ? best[1] + route.slice(best[0].length) : route
|
|
127
|
+
}
|
|
128
|
+
|
|
115
129
|
/** A canonical route → the route this locale shows it at (`/blog` → `/noticias`, by prefix too). */
|
|
116
130
|
export function translateRoute(site, canonicalRoute, locale) {
|
|
117
131
|
const index = indexOf(site)
|
|
118
132
|
if (!locale || locale === index.siteDefaultLocale) return canonicalRoute
|
|
119
133
|
const entry = index.translations[locale]
|
|
120
134
|
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
|
|
135
|
+
return entry.forward.get(canonicalRoute) || nearestPrefix(entry.forward, canonicalRoute)
|
|
127
136
|
}
|
|
128
137
|
|
|
129
138
|
/**
|
|
@@ -136,12 +145,7 @@ export function reverseTranslateRoute(site, displayRoute, locale) {
|
|
|
136
145
|
const entry = index.translations[locale]
|
|
137
146
|
if (!entry) return displayRoute
|
|
138
147
|
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
|
|
148
|
+
return entry.reverse.get(route) || nearestPrefix(entry.reverse, route)
|
|
145
149
|
}
|
|
146
150
|
|
|
147
151
|
/**
|
|
@@ -293,7 +297,8 @@ export function landingRoute(site, page, seen = new Set()) {
|
|
|
293
297
|
* [Diego]; until then no page matched it.
|
|
294
298
|
* 2. **The page** (`findPage`). None ⇒ `notFound`, with the site's not-found page: its
|
|
295
299
|
* `notFound` content, else the page at `/404`.
|
|
296
|
-
* 3. **An authored `redirect:`** ⇒ a `302` to it,
|
|
300
|
+
* 3. **An authored `redirect:`** ⇒ a `302` to it: a page of the site in this locale, anything
|
|
301
|
+
* else as written (`authoredRedirectLocation`).
|
|
297
302
|
* 4. **An authored `rewrite:`** ⇒ the page is served from there; only a host that proxies can.
|
|
298
303
|
* 5. **A page with no content** ⇒ a `302` to where it lands (`landingRoute`), in this locale
|
|
299
304
|
* (`localeUrl`) — when that is not the page itself.
|
|
@@ -328,7 +333,8 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
328
333
|
|
|
329
334
|
// 3 · 4 · what an author wrote on the page
|
|
330
335
|
if (typeof page.redirect === 'string' && page.redirect) {
|
|
331
|
-
|
|
336
|
+
const location = authoredRedirectLocation(index, found.route, page.redirect, { activeLocale, defaultLocale })
|
|
337
|
+
return { ...redirect(location, RESOLUTION_STATUS.pageRedirect, 'authored'), ...found }
|
|
332
338
|
}
|
|
333
339
|
if (typeof page.rewrite === 'string' && page.rewrite) {
|
|
334
340
|
return { kind: 'rewrite', target: page.rewrite, ...found }
|
|
@@ -350,3 +356,92 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
350
356
|
function redirect(location, status, reason) {
|
|
351
357
|
return { kind: 'redirect', status, location, reason }
|
|
352
358
|
}
|
|
359
|
+
|
|
360
|
+
const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* An authored `redirect:` resolved against the page that carries it, as a browser resolves a
|
|
364
|
+
* relative URL with the page as a FOLDER: a URL with a scheme, or a protocol-relative one, as
|
|
365
|
+
* written; `academic` on `/solutions` is `/solutions/academic`; `./x` and `../x` step as they do
|
|
366
|
+
* in a URL, on a relative target and on a path from the root alike. A trailing slash and an empty
|
|
367
|
+
* segment are kept, as a URL keeps them — the page a path names is matched with a trailing slash
|
|
368
|
+
* or without. Any query or fragment rides along untouched.
|
|
369
|
+
*
|
|
370
|
+
* The build resolves a site's redirects by this rule, and `resolveRoute` resolves a payload's that
|
|
371
|
+
* were not (a pushed page carries its target as written). ⛔ Until 2026-09-30 a relative target was
|
|
372
|
+
* joined to the page's route and no `.` or `..` was resolved, so `../pricing` stayed in the path.
|
|
373
|
+
*
|
|
374
|
+
* @param {string} route - the page's own route
|
|
375
|
+
* @param {string} target - its `redirect:`
|
|
376
|
+
* @returns {string}
|
|
377
|
+
*/
|
|
378
|
+
export function authoredRedirectTarget(route, target) {
|
|
379
|
+
if (typeof target !== 'string' || !target) return target
|
|
380
|
+
if (target.startsWith('//') || HAS_SCHEME.test(target)) return target
|
|
381
|
+
const cut = target.search(/[?#]/)
|
|
382
|
+
const path = cut < 0 ? target : target.slice(0, cut)
|
|
383
|
+
const suffix = cut < 0 ? '' : target.slice(cut)
|
|
384
|
+
const base = route === '/' ? '/' : `${route}/`
|
|
385
|
+
return removeDotSegments(path.startsWith('/') ? path : base + path) + suffix
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** RFC 3986 § 5.2.4 for a path from the root: `.` and `..` segments resolved, nothing else changed. */
|
|
389
|
+
function removeDotSegments(path) {
|
|
390
|
+
const segments = path.split('/').slice(1)
|
|
391
|
+
const out = []
|
|
392
|
+
segments.forEach((segment, i) => {
|
|
393
|
+
const last = i === segments.length - 1
|
|
394
|
+
if (segment === '.' || segment === '..') {
|
|
395
|
+
if (segment === '..') out.pop()
|
|
396
|
+
if (last) out.push('')
|
|
397
|
+
return
|
|
398
|
+
}
|
|
399
|
+
out.push(segment)
|
|
400
|
+
})
|
|
401
|
+
return '/' + out.join('/')
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* ⭐ WHERE AN AUTHORED REDIRECT SENDS A VISITOR OF THIS LOCALE — a page of the site in their
|
|
406
|
+
* language, as a page with no content sends them (ruled for containers 2026-09-30 [Diego]; for an
|
|
407
|
+
* authored redirect the same day, asked by the host that serves it).
|
|
408
|
+
*
|
|
409
|
+
* - A URL with a scheme, a protocol-relative one, or a path that already carries a served locale's
|
|
410
|
+
* prefix: as written — the author named where.
|
|
411
|
+
* - A path naming a page of the site: that page's URL in this locale (`localeUrl`) — its prefix and
|
|
412
|
+
* its translated slugs — with any query or fragment kept.
|
|
413
|
+
* - Any other path — a file, a route the site does not hold: as written.
|
|
414
|
+
*
|
|
415
|
+
* The author wrote the target against the CANONICAL routes, whatever routes the payload's pages
|
|
416
|
+
* carry: a relative one resolves against the page's canonical route, and a page is found by its
|
|
417
|
+
* canonical route or, on a payload whose pages carry this locale's own routes, through the
|
|
418
|
+
* translations. Both kinds of payload give one answer.
|
|
419
|
+
*
|
|
420
|
+
* ⛔ Until 2026-09-30 every authored redirect went as written, so a French visitor was sent to the
|
|
421
|
+
* English page, and a relative target was left for the browser to resolve against the URL it had
|
|
422
|
+
* asked for rather than the page. ⛔ Until 2026-10-01 the target was looked up only as a route of
|
|
423
|
+
* the payload, so on a payload of French routes `/blog` named no page and the French visitor was
|
|
424
|
+
* still sent to the English one.
|
|
425
|
+
*
|
|
426
|
+
* @returns {string}
|
|
427
|
+
*/
|
|
428
|
+
function authoredRedirectLocation(index, route, target, { activeLocale, defaultLocale }) {
|
|
429
|
+
const resolved = authoredRedirectTarget(reverseTranslateRoute(index, route, activeLocale), target)
|
|
430
|
+
if (!resolved.startsWith('/') || resolved.startsWith('//')) return resolved
|
|
431
|
+
const cut = resolved.search(/[?#]/)
|
|
432
|
+
const path = cut < 0 ? resolved : resolved.slice(0, cut)
|
|
433
|
+
const suffix = cut < 0 ? '' : resolved.slice(cut)
|
|
434
|
+
const first = path.split('/')[1] || ''
|
|
435
|
+
if (first && index.locales.includes(first)) return resolved
|
|
436
|
+
const canonical = path === '/' ? '/' : path.replace(/\/$/, '')
|
|
437
|
+
if (!namesPage(index, canonical, activeLocale)) return resolved
|
|
438
|
+
return localeUrl(index, activeLocale, canonical, { activeLocale, defaultLocale }) + suffix
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Whether a canonical route names a page of the payload: by that route, or by this locale's. */
|
|
442
|
+
function namesPage(index, canonical, locale) {
|
|
443
|
+
const asCanonical = { activeLocale: index.siteDefaultLocale, defaultLocale: index.siteDefaultLocale }
|
|
444
|
+
return Boolean(
|
|
445
|
+
findPage(index, canonical, asCanonical) || findPage(index, translateRoute(index, canonical, locale), asCanonical),
|
|
446
|
+
)
|
|
447
|
+
}
|