Home / Function/ matchRoute() — astro Function Reference

matchRoute() — astro Function Reference

Architecture documentation for the matchRoute() function in dev.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  c86db6f4_bdaf_93d2_d909_fff442b0c98c["matchRoute()"]
  3f373c63_ced2_f6ce_9a33_2b040b86d1b3["dev.ts"]
  c86db6f4_bdaf_93d2_d909_fff442b0c98c -->|defined in| 3f373c63_ced2_f6ce_9a33_2b040b86d1b3
  style c86db6f4_bdaf_93d2_d909_fff442b0c98c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/routing/dev.ts lines 21–97

export async function matchRoute(
	pathname: string,
	routesList: RoutesList,
	pipeline: RunnablePipeline,
	manifest: SSRManifest,
): Promise<MatchedRoute | undefined> {
	const { logger, routeCache } = pipeline;
	const matches = matchAllRoutes(pathname, routesList);

	const preloadedMatches = getSortedPreloadedMatches({
		pipeline,
		matches,
		manifest,
	});

	for await (const { route: maybeRoute, filePath } of preloadedMatches) {
		// attempt to get static paths
		// if this fails, we have a bad URL match!
		try {
			await getProps({
				mod: await pipeline.getComponentByRoute(maybeRoute),
				routeData: maybeRoute,
				routeCache,
				pathname: pathname,
				logger,
				serverLike: pipeline.manifest.serverLike,
				base: manifest.base,
				trailingSlash: manifest.trailingSlash,
			});
			return {
				route: maybeRoute,
				filePath,
				resolvedPathname: pathname,
			};
		} catch (e) {
			// Ignore error for no matching static paths
			if (isAstroError(e) && e.title === NoMatchingStaticPathFound.title) {
				continue;
			}
			throw e;
		}
	}

	// Try without `.html` extensions or `index.html` in request URLs to mimic
	// routing behavior in production builds. This supports both file and directory
	// build formats, and is necessary based on how the manifest tracks build targets.
	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,
			)}\n\n${NoMatchingStaticPathFound.hint(possibleRoutes)}`,
		);
	}

	const custom404 = getCustom404Route(routesList);

	if (custom404) {
		const filePath = new URL(`./${custom404.component}`, manifest.rootDir);

		return {
			route: custom404,
			filePath,
			resolvedPathname: pathname,
		};
	}

	return undefined;
}

Domain

Subdomains

Frequently Asked Questions

What does matchRoute() do?
matchRoute() is a function in the astro codebase, defined in packages/astro/src/core/routing/dev.ts.
Where is matchRoute() defined?
matchRoute() is defined in packages/astro/src/core/routing/dev.ts at line 21.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free