astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
23 lines (22 loc) • 865 B
JavaScript
import { pathHasLocale } from "./path.js";
export function isLocalizedErrorRoute(route, status, locales) {
if (!locales) return false;
const suffix = `/${status}`;
if (!route.endsWith(suffix)) return false;
const localeSegment = route.slice(0, -suffix.length);
if (!localeSegment || localeSegment.includes("/", 1)) return false;
return pathHasLocale(localeSegment, locales);
}
export function getErrorRoutePath(pathname, status, routes, locales, appendTrailingSlash = false) {
const suffix = appendTrailingSlash ? "/" : "";
if (locales) {
const firstSegment = pathname.split("/").find(Boolean);
if (firstSegment && pathHasLocale(`/${firstSegment}`, locales)) {
const localized = `/${firstSegment}/${status}`;
if (routes.some((route) => route.route === localized)) {
return `${localized}${suffix}`;
}
}
}
return `/${status}${suffix}`;
}