UNPKG

astro

Version:

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

41 lines (40 loc) • 1.7 kB
import { SERVER_ISLAND_COMPONENT } from "../server-islands/endpoint.js"; import { isRoute404, isRoute500 } from "./internal/route-errors.js"; /** Find matching route from pathname */ export function matchRoute(pathname, manifest) { // Error pages (404/500) take precedence over dynamic routes that might // capture the same path (e.g. [locale] matching /404). See #15098. if (isRoute404(pathname)) { const errorRoute = manifest.routes.find((route) => isRoute404(route.route)); if (errorRoute) return errorRoute; } if (isRoute500(pathname)) { const errorRoute = manifest.routes.find((route) => isRoute500(route.route)); if (errorRoute) return errorRoute; } return manifest.routes.find((route) => { return route.pattern.test(pathname) || route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(pathname)); }); } /** Finds all matching routes from pathname */ export function matchAllRoutes(pathname, manifest) { return manifest.routes.filter((route) => route.pattern.test(pathname)); } /** * Determines if the given route matches a 404 or 500 error page. * * @param {RouteData} route - The route data to check. * @returns {boolean} `true` if the route matches a 404 or 500 error page; otherwise, `false`. */ export function isRoute404or500(route) { return isRoute404(route.route) || isRoute500(route.route); } /** * Determines if a given route is associated with the server island component. * * @param {RouteData} route - The route data object to evaluate. * @return {boolean} Returns true if the route's component is the server island component; otherwise, false. */ export function isRouteServerIsland(route) { return route.component === SERVER_ISLAND_COMPONENT; }