@uniweb/core 0.35.3 → 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 +64 -17
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
|
|
|
@@ -208,25 +209,31 @@ export function localeOfPath(path, config = {}) {
|
|
|
208
209
|
return { locale: unprefixed, prefixed: false, path: raw }
|
|
209
210
|
}
|
|
210
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
|
+
|
|
211
223
|
/**
|
|
212
224
|
* The page a path names — as `Website#getPage` has always found it — or null.
|
|
213
225
|
*
|
|
214
226
|
* In order: the active locale's prefix comes off; the page whose route is the path as given (a
|
|
215
227
|
* published payload may carry a locale's own routes); else the path reverse-translated and the
|
|
216
|
-
* page of that canonical route (a folder resolving to its `isIndex` child either way); else
|
|
217
|
-
*
|
|
218
|
-
*
|
|
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.
|
|
219
232
|
*
|
|
220
233
|
* @returns {{ page: Object, route: string, params: Object, template: string|null } | null}
|
|
221
234
|
*/
|
|
222
235
|
function findPage(index, path, { activeLocale, defaultLocale }) {
|
|
223
|
-
|
|
224
|
-
if (activeLocale && activeLocale !== defaultLocale) {
|
|
225
|
-
const prefix = `/${activeLocale}`
|
|
226
|
-
if (stripped === prefix || stripped === `${prefix}/`) stripped = '/'
|
|
227
|
-
else if (stripped.startsWith(`${prefix}/`)) stripped = stripped.slice(prefix.length)
|
|
228
|
-
}
|
|
229
|
-
const asGiven = stripped === '/' ? '/' : stripped.replace(/\/$/, '')
|
|
236
|
+
const asGiven = askedRoute(path, activeLocale, defaultLocale)
|
|
230
237
|
|
|
231
238
|
const promoted = (page) => (index.childrenOf.get(page.route) || []).find(isIndex) || page
|
|
232
239
|
const exactly = (route) => index.byRoute.get(route)
|
|
@@ -245,10 +252,18 @@ function findPage(index, path, { activeLocale, defaultLocale }) {
|
|
|
245
252
|
return { page, route: page.route, params: {}, template: null }
|
|
246
253
|
}
|
|
247
254
|
|
|
248
|
-
|
|
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))
|
|
249
264
|
if (indexPage) return { page: indexPage, route: indexPage.route, params: {}, template: null }
|
|
250
265
|
|
|
251
|
-
for (const candidate of new Set([canonical, asGiven])) {
|
|
266
|
+
for (const candidate of new Set([canonical, asGiven, shown])) {
|
|
252
267
|
for (const page of index.pages) {
|
|
253
268
|
if (!page.route.includes(':')) continue
|
|
254
269
|
const hit = matchDynamicRoute(page.route, candidate)
|
|
@@ -305,6 +320,11 @@ export function landingRoute(site, page, seen = new Set()) {
|
|
|
305
320
|
* 6. Otherwise the page. A parametric page's record may still not exist; that is the render's
|
|
306
321
|
* data step to say.
|
|
307
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
|
+
*
|
|
308
328
|
* `status` is what a host that sends one sends. A static host has its own ways to say each kind.
|
|
309
329
|
*
|
|
310
330
|
* @param {Object} site - one locale's site content (`{ pages, config, notFound? }`), or its `routeIndex`
|
|
@@ -331,17 +351,24 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
331
351
|
if (!found) return { kind: 'notFound', status: RESOLUTION_STATUS.notFound, page: index.notFound }
|
|
332
352
|
const { page } = found
|
|
333
353
|
|
|
334
|
-
// 3 ·
|
|
354
|
+
// 3 · what an author wrote on the page: a redirect goes to its own destination, in one hop
|
|
335
355
|
if (typeof page.redirect === 'string' && page.redirect) {
|
|
336
356
|
const location = authoredRedirectLocation(index, found.route, page.redirect, { activeLocale, defaultLocale })
|
|
337
357
|
return { ...redirect(location, RESOLUTION_STATUS.pageRedirect, 'authored'), ...found }
|
|
338
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
|
+
}
|
|
339
366
|
if (typeof page.rewrite === 'string' && page.rewrite) {
|
|
340
|
-
return { kind: 'rewrite', target: page.rewrite, ...found }
|
|
367
|
+
return ownUrl() || { kind: 'rewrite', target: page.rewrite, ...found }
|
|
341
368
|
}
|
|
342
369
|
|
|
343
370
|
// 5 · a page with no content goes to its content. The DECISION is canonical (`page.route`);
|
|
344
|
-
// the DESTINATION is this locale's URL for it.
|
|
371
|
+
// the DESTINATION is this locale's URL for it, in one hop.
|
|
345
372
|
if (!hasContent(page)) {
|
|
346
373
|
const landing = landingRoute(index, page)
|
|
347
374
|
if (landing && landing !== page.route) {
|
|
@@ -350,7 +377,27 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
350
377
|
}
|
|
351
378
|
}
|
|
352
379
|
|
|
353
|
-
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 })
|
|
354
401
|
}
|
|
355
402
|
|
|
356
403
|
function redirect(location, status, reason) {
|