Home / Function/ createRedirectRoutes() — astro Function Reference

createRedirectRoutes() — astro Function Reference

Architecture documentation for the createRedirectRoutes() function in create.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e28e398f_3a64_8e35_e7de_2406040de612["createRedirectRoutes()"]
  a7eed989_9f2b_5163_ecdf_85920a8b973f["create.ts"]
  e28e398f_3a64_8e35_e7de_2406040de612 -->|defined in| a7eed989_9f2b_5163_ecdf_85920a8b973f
  980f5a1b_85ee_0217_7a04_888abfb8f70f["createRoutesList()"]
  980f5a1b_85ee_0217_7a04_888abfb8f70f -->|calls| e28e398f_3a64_8e35_e7de_2406040de612
  1238e67c_a57d_fa2d_67fb_033b7f31b2ba["getParts()"]
  e28e398f_3a64_8e35_e7de_2406040de612 -->|calls| 1238e67c_a57d_fa2d_67fb_033b7f31b2ba
  83ef6580_f43d_b634_d2ed_d70e0633249d["joinSegments()"]
  e28e398f_3a64_8e35_e7de_2406040de612 -->|calls| 83ef6580_f43d_b634_d2ed_d70e0633249d
  style e28e398f_3a64_8e35_e7de_2406040de612 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/routing/manifest/create.ts lines 321–401

function createRedirectRoutes(
	{ settings }: CreateRouteManifestParams,
	routeMap: Map<string, RouteData>,
): RouteData[] {
	const { config } = settings;
	const trailingSlash = config.trailingSlash;

	const routes: RouteData[] = [];

	for (const [from, to] of Object.entries(settings.config.redirects)) {
		const segments = removeLeadingForwardSlash(from)
			.split(path.posix.sep)
			.filter(Boolean)
			.map((s: string) => {
				validateSegment(s);
				return getParts(s, from);
			});

		const pattern = getPattern(segments, settings.config.base, trailingSlash);
		const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
			? `/${segments.map((segment) => segment[0].content).join('/')}`
			: null;
		const params = segments
			.flat()
			.filter((p) => p.dynamic)
			.map((p) => p.content);
		const route = joinSegments(segments);

		let destination: string;
		if (typeof to === 'string') {
			destination = to;
		} else {
			destination = to.destination;
		}

		// check if the link starts with http or https; if not, throw an error
		if (URL.canParse(destination) && !/^https?:\/\//.test(destination)) {
			throw new AstroError({
				...UnsupportedExternalRedirect,
				message: UnsupportedExternalRedirect.message(from, destination),
			});
		}

		const redirectRoute = routeMap.get(destination);

		// If the source has dynamic params and the redirect will be prerendered,
		// we need a valid redirectRoute to map them. Without it, the build will fail
		// later with a misleading error. Catch this early and provide a clear error message.
		if (
			params.length > 0 &&
			!redirectRoute &&
			!URL.canParse(destination) &&
			getPrerenderDefault(config)
		) {
			throw new AstroError({
				...InvalidRedirectDestination,
				message: InvalidRedirectDestination.message(from, destination),
			});
		}

		routes.push({
			type: 'redirect',
			// For backwards compatibility, a redirect is never considered an index route.
			isIndex: false,
			route,
			pattern,
			segments,
			params,
			component: from,
			pathname: pathname || void 0,
			prerender: getPrerenderDefault(config),
			redirect: to,
			redirectRoute,
			fallbackRoutes: [],
			distURL: [],
			origin: 'project',
		});
	}

	return routes;
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does createRedirectRoutes() do?
createRedirectRoutes() is a function in the astro codebase, defined in packages/astro/src/core/routing/manifest/create.ts.
Where is createRedirectRoutes() defined?
createRedirectRoutes() is defined in packages/astro/src/core/routing/manifest/create.ts at line 321.
What does createRedirectRoutes() call?
createRedirectRoutes() calls 2 function(s): getParts, joinSegments.
What calls createRedirectRoutes()?
createRedirectRoutes() is called by 1 function(s): createRoutesList.

Analyze Your Own Codebase

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

Try Supermodel Free