UNPKG

astro

Version:

Astro is a modern site builder with web best practices, performance, and DX front-of-mind.

59 lines (58 loc) 2.18 kB
import { appendForwardSlash, joinPaths, prependForwardSlash, removeTrailingForwardSlash, stripRequestBase } from "@astrojs/internal-helpers/path"; import { normalizeTheLocale } from "../../i18n/path.js"; function computePathnameFromDomain(request, url, i18n, base, trailingSlash, logger, pathnameFromRequest) { let pathname = void 0; if (i18n && (i18n.strategy === "domains-prefix-always" || i18n.strategy === "domains-prefix-other-locales" || i18n.strategy === "domains-prefix-always-no-redirect")) { let host = request.headers.get("X-Forwarded-Host"); let protocol = request.headers.get("X-Forwarded-Proto"); if (protocol) { protocol = protocol + ":"; } else { protocol = url.protocol; } if (!host) { host = request.headers.get("Host"); } if (host && protocol) { host = host.split(":")[0]; try { let locale; const hostAsUrl = new URL(`${protocol}//${host}`); for (const [domainKey, localeValue] of Object.entries(i18n.domainLookupTable)) { const domainKeyAsUrl = new URL(domainKey); if (hostAsUrl.host === domainKeyAsUrl.host && hostAsUrl.protocol === domainKeyAsUrl.protocol) { locale = localeValue; break; } } if (locale) { const requestPathname = pathnameFromRequest ?? stripRequestBase(url.pathname, base); pathname = prependForwardSlash(joinPaths(normalizeTheLocale(locale), requestPathname)); if (trailingSlash === "always") { pathname = appendForwardSlash(pathname); } else if (trailingSlash === "never") { pathname = removeTrailingForwardSlash(pathname); } else if (url.pathname.endsWith("/")) { pathname = appendForwardSlash(pathname); } } } catch (e) { logger.error( "router", `Astro tried to parse ${protocol}//${host} as an URL, but it threw a parsing error. Check the X-Forwarded-Host and X-Forwarded-Proto headers.` ); logger.error("router", `Error: ${e}`); } } } return pathname; } export { computePathnameFromDomain };