@uniweb/core 0.35.2 → 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 +34 -16
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
|
/**
|
|
@@ -408,14 +412,21 @@ function removeDotSegments(path) {
|
|
|
408
412
|
* its translated slugs — with any query or fragment kept.
|
|
409
413
|
* - Any other path — a file, a route the site does not hold: as written.
|
|
410
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
|
+
*
|
|
411
420
|
* ⛔ Until 2026-09-30 every authored redirect went as written, so a French visitor was sent to the
|
|
412
421
|
* 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.
|
|
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.
|
|
414
425
|
*
|
|
415
426
|
* @returns {string}
|
|
416
427
|
*/
|
|
417
428
|
function authoredRedirectLocation(index, route, target, { activeLocale, defaultLocale }) {
|
|
418
|
-
const resolved = authoredRedirectTarget(route, target)
|
|
429
|
+
const resolved = authoredRedirectTarget(reverseTranslateRoute(index, route, activeLocale), target)
|
|
419
430
|
if (!resolved.startsWith('/') || resolved.startsWith('//')) return resolved
|
|
420
431
|
const cut = resolved.search(/[?#]/)
|
|
421
432
|
const path = cut < 0 ? resolved : resolved.slice(0, cut)
|
|
@@ -423,7 +434,14 @@ function authoredRedirectLocation(index, route, target, { activeLocale, defaultL
|
|
|
423
434
|
const first = path.split('/')[1] || ''
|
|
424
435
|
if (first && index.locales.includes(first)) return resolved
|
|
425
436
|
const canonical = path === '/' ? '/' : path.replace(/\/$/, '')
|
|
426
|
-
|
|
427
|
-
if (!named) return resolved
|
|
437
|
+
if (!namesPage(index, canonical, activeLocale)) return resolved
|
|
428
438
|
return localeUrl(index, activeLocale, canonical, { activeLocale, defaultLocale }) + suffix
|
|
429
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
|
+
}
|