astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
80 lines (78 loc) • 2.37 kB
JavaScript
import { matchAllRoutes } from "./match.js";
import { getSortedPreloadedMatches } from "../../prerender/routing.js";
import { getProps } from "../render/index.js";
import { getCustom404Route } from "./helpers.js";
import { NoMatchingStaticPathFound } from "../errors/errors-data.js";
import { isAstroError } from "../errors/errors.js";
import { getErrorRoutePath } from "../../i18n/error-routes.js";
async function matchRoute(pathname, routesList, pipeline, manifest) {
const { logger, routeCache } = pipeline;
const matches = matchAllRoutes(pathname, routesList);
const preloadedMatches = getSortedPreloadedMatches({
matches,
manifest
});
let firstError = null;
for await (const { route: maybeRoute, filePath } of preloadedMatches) {
try {
await getProps({
mod: await pipeline.getComponentByRoute(maybeRoute),
routeData: maybeRoute,
routeCache,
pathname,
logger,
serverLike: pipeline.manifest.serverLike,
base: manifest.base,
trailingSlash: manifest.trailingSlash
});
return {
route: maybeRoute,
filePath,
resolvedPathname: pathname
};
} catch (e) {
if (isAstroError(e) && e.title === NoMatchingStaticPathFound.title) {
continue;
}
firstError ??= e;
continue;
}
}
if (firstError) {
throw firstError;
}
const altPathname = pathname.replace(/\/index\.html$/, "/").replace(/\.html$/, "");
if (altPathname !== pathname) {
return await matchRoute(altPathname, routesList, pipeline, manifest);
}
if (matches.length) {
const possibleRoutes = matches.flatMap((route) => route.component);
logger.warn(
"router",
`${NoMatchingStaticPathFound.message(
pathname
)}
${NoMatchingStaticPathFound.hint(possibleRoutes)}`
);
}
const errorRoutePath = getErrorRoutePath(
pathname,
404,
routesList.routes,
manifest.i18n?.locales,
manifest.trailingSlash === "always"
);
const custom404 = routesList.routes.find((route) => route.route === errorRoutePath) ?? getCustom404Route(routesList);
if (custom404) {
const filePath = new URL(`./${custom404.component}`, manifest.rootDir);
return {
route: custom404,
filePath,
resolvedPathname: pathname
};
}
return void 0;
}
export {
matchRoute
};