UNPKG

astro

Version:

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

94 lines (93 loc) • 3.29 kB
import { removeTrailingForwardSlash } from "@astrojs/internal-helpers/path"; import { isLocalizedErrorRoute } from "../../i18n/error-routes.js"; import { isRoute404, isRoute500 } from "./internal/route-errors.js"; /** * Function guard that checks if a route is redirect. If so, `RouteData.redirectRoute` and * `RouteData.redirect` aren't `undefined` anymore * @param route */ export function routeIsRedirect(route) { return route?.type === "redirect"; } /** * True if the route represents a fallback entry. */ export function routeIsFallback(route) { return route?.type === "fallback"; } /** * Give a route, it returns its fallback routes from a `list` of `RouteInfo[]`. * * It throws an error if no fallback routes were found. This means there's an error * when we construct the list of routes * @param route * @param routeList */ export function getFallbackRoute(route, routeList) { const fallbackRoute = routeList.find((r) => { // The index doesn't have a fallback route if (route.route === "/" && r.routeData.route === "/") { return true; } return r.routeData.fallbackRoutes.find((f) => { return f.route === route.route; }); }); if (!fallbackRoute) { throw new Error(`No fallback route found for route ${route.route}`); } return fallbackRoute.routeData; } /** * Return a user-provided 404 route if one exists. */ export function getCustom404Route(manifestData) { return manifestData.routes.find((r) => isRoute404(r.route)); } /** * Return a user-provided 500 route if one exists. */ export function getCustom500Route(manifestData) { return manifestData.routes.find((r) => isRoute500(r.route)); } /** * Computes the default HTTP status code a route renders with: * `302` for i18n fallback matches, `404`/`500` for (possibly localized) * error routes, `200` otherwise. */ export function getDefaultStatusCode(manifest, routeData, pathname) { if (!routeData.pattern.test(pathname)) { for (const fallbackRoute of routeData.fallbackRoutes) { if (fallbackRoute.pattern.test(pathname)) { return 302; } } } const route = removeTrailingForwardSlash(routeData.route); const locales = manifest.i18n?.locales; if (isRoute404(route) || isLocalizedErrorRoute(route, 404, locales)) { return 404; } if (isRoute500(route) || isLocalizedErrorRoute(route, 500, locales)) { return 500; } return 200; } /** * Returns true if the route definition contains `.html` as a static segment part, * as is the case for routes like `[slug].html.astro`. Used to avoid stripping the * `.html` suffix from pathnames that intentionally include it. */ export function routeHasHtmlExtension(route) { return route.segments.some((segment) => segment.some((part) => !part.dynamic && part.content.includes(".html"))); } export function hasNonPrerenderedRoute(routes, options) { const includeEndpoints = options?.includeEndpoints ?? true; const includeExternal = options?.includeExternal ?? false; const routeTypes = includeEndpoints ? ["page", "endpoint"] : ["page"]; const origins = includeExternal ? ["project", "external"] : ["project"]; return routes.some((route) => { const isPrerendered = "isPrerendered" in route ? route.isPrerendered : route.prerender; return routeTypes.includes(route.type) && origins.includes(route.origin) && !isPrerendered; }); }