@uniweb/core 0.35.1 → 0.35.2
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 +79 -2
package/package.json
CHANGED
package/src/resolve-route.js
CHANGED
|
@@ -293,7 +293,8 @@ export function landingRoute(site, page, seen = new Set()) {
|
|
|
293
293
|
* [Diego]; until then no page matched it.
|
|
294
294
|
* 2. **The page** (`findPage`). None ⇒ `notFound`, with the site's not-found page: its
|
|
295
295
|
* `notFound` content, else the page at `/404`.
|
|
296
|
-
* 3. **An authored `redirect:`** ⇒ a `302` to it,
|
|
296
|
+
* 3. **An authored `redirect:`** ⇒ a `302` to it: a page of the site in this locale, anything
|
|
297
|
+
* else as written (`authoredRedirectLocation`).
|
|
297
298
|
* 4. **An authored `rewrite:`** ⇒ the page is served from there; only a host that proxies can.
|
|
298
299
|
* 5. **A page with no content** ⇒ a `302` to where it lands (`landingRoute`), in this locale
|
|
299
300
|
* (`localeUrl`) — when that is not the page itself.
|
|
@@ -328,7 +329,8 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
328
329
|
|
|
329
330
|
// 3 · 4 · what an author wrote on the page
|
|
330
331
|
if (typeof page.redirect === 'string' && page.redirect) {
|
|
331
|
-
|
|
332
|
+
const location = authoredRedirectLocation(index, found.route, page.redirect, { activeLocale, defaultLocale })
|
|
333
|
+
return { ...redirect(location, RESOLUTION_STATUS.pageRedirect, 'authored'), ...found }
|
|
332
334
|
}
|
|
333
335
|
if (typeof page.rewrite === 'string' && page.rewrite) {
|
|
334
336
|
return { kind: 'rewrite', target: page.rewrite, ...found }
|
|
@@ -350,3 +352,78 @@ export function resolveRoute(site, path, context = {}) {
|
|
|
350
352
|
function redirect(location, status, reason) {
|
|
351
353
|
return { kind: 'redirect', status, location, reason }
|
|
352
354
|
}
|
|
355
|
+
|
|
356
|
+
const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* An authored `redirect:` resolved against the page that carries it, as a browser resolves a
|
|
360
|
+
* relative URL with the page as a FOLDER: a URL with a scheme, or a protocol-relative one, as
|
|
361
|
+
* written; `academic` on `/solutions` is `/solutions/academic`; `./x` and `../x` step as they do
|
|
362
|
+
* in a URL, on a relative target and on a path from the root alike. A trailing slash and an empty
|
|
363
|
+
* segment are kept, as a URL keeps them — the page a path names is matched with a trailing slash
|
|
364
|
+
* or without. Any query or fragment rides along untouched.
|
|
365
|
+
*
|
|
366
|
+
* The build resolves a site's redirects by this rule, and `resolveRoute` resolves a payload's that
|
|
367
|
+
* were not (a pushed page carries its target as written). ⛔ Until 2026-09-30 a relative target was
|
|
368
|
+
* joined to the page's route and no `.` or `..` was resolved, so `../pricing` stayed in the path.
|
|
369
|
+
*
|
|
370
|
+
* @param {string} route - the page's own route
|
|
371
|
+
* @param {string} target - its `redirect:`
|
|
372
|
+
* @returns {string}
|
|
373
|
+
*/
|
|
374
|
+
export function authoredRedirectTarget(route, target) {
|
|
375
|
+
if (typeof target !== 'string' || !target) return target
|
|
376
|
+
if (target.startsWith('//') || HAS_SCHEME.test(target)) return target
|
|
377
|
+
const cut = target.search(/[?#]/)
|
|
378
|
+
const path = cut < 0 ? target : target.slice(0, cut)
|
|
379
|
+
const suffix = cut < 0 ? '' : target.slice(cut)
|
|
380
|
+
const base = route === '/' ? '/' : `${route}/`
|
|
381
|
+
return removeDotSegments(path.startsWith('/') ? path : base + path) + suffix
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** RFC 3986 § 5.2.4 for a path from the root: `.` and `..` segments resolved, nothing else changed. */
|
|
385
|
+
function removeDotSegments(path) {
|
|
386
|
+
const segments = path.split('/').slice(1)
|
|
387
|
+
const out = []
|
|
388
|
+
segments.forEach((segment, i) => {
|
|
389
|
+
const last = i === segments.length - 1
|
|
390
|
+
if (segment === '.' || segment === '..') {
|
|
391
|
+
if (segment === '..') out.pop()
|
|
392
|
+
if (last) out.push('')
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
out.push(segment)
|
|
396
|
+
})
|
|
397
|
+
return '/' + out.join('/')
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* ⭐ WHERE AN AUTHORED REDIRECT SENDS A VISITOR OF THIS LOCALE — a page of the site in their
|
|
402
|
+
* language, as a page with no content sends them (ruled for containers 2026-09-30 [Diego]; for an
|
|
403
|
+
* authored redirect the same day, asked by the host that serves it).
|
|
404
|
+
*
|
|
405
|
+
* - A URL with a scheme, a protocol-relative one, or a path that already carries a served locale's
|
|
406
|
+
* prefix: as written — the author named where.
|
|
407
|
+
* - A path naming a page of the site: that page's URL in this locale (`localeUrl`) — its prefix and
|
|
408
|
+
* its translated slugs — with any query or fragment kept.
|
|
409
|
+
* - Any other path — a file, a route the site does not hold: as written.
|
|
410
|
+
*
|
|
411
|
+
* ⛔ Until 2026-09-30 every authored redirect went as written, so a French visitor was sent to the
|
|
412
|
+
* 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.
|
|
414
|
+
*
|
|
415
|
+
* @returns {string}
|
|
416
|
+
*/
|
|
417
|
+
function authoredRedirectLocation(index, route, target, { activeLocale, defaultLocale }) {
|
|
418
|
+
const resolved = authoredRedirectTarget(route, target)
|
|
419
|
+
if (!resolved.startsWith('/') || resolved.startsWith('//')) return resolved
|
|
420
|
+
const cut = resolved.search(/[?#]/)
|
|
421
|
+
const path = cut < 0 ? resolved : resolved.slice(0, cut)
|
|
422
|
+
const suffix = cut < 0 ? '' : resolved.slice(cut)
|
|
423
|
+
const first = path.split('/')[1] || ''
|
|
424
|
+
if (first && index.locales.includes(first)) return resolved
|
|
425
|
+
const canonical = path === '/' ? '/' : path.replace(/\/$/, '')
|
|
426
|
+
const named = findPage(index, canonical, { activeLocale: index.siteDefaultLocale, defaultLocale: index.siteDefaultLocale })
|
|
427
|
+
if (!named) return resolved
|
|
428
|
+
return localeUrl(index, activeLocale, canonical, { activeLocale, defaultLocale }) + suffix
|
|
429
|
+
}
|