Home / Function/ createRedirectsFromAstroRoutes() — astro Function Reference

createRedirectsFromAstroRoutes() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  588be6b3_7df5_51d9_4f94_ee8db1fac57e["createRedirectsFromAstroRoutes()"]
  3303a596_77e9_a4f6_1515_7ec93bef6e53["astro.ts"]
  588be6b3_7df5_51d9_4f94_ee8db1fac57e -->|defined in| 3303a596_77e9_a4f6_1515_7ec93bef6e53
  22014c97_c453_2583_890e_012d1c7799de["getRedirectStatus()"]
  588be6b3_7df5_51d9_4f94_ee8db1fac57e -->|calls| 22014c97_c453_2583_890e_012d1c7799de
  39158eea_4bd6_d5fc_552c_319f997fe079["prependForwardSlash()"]
  588be6b3_7df5_51d9_4f94_ee8db1fac57e -->|calls| 39158eea_4bd6_d5fc_552c_319f997fe079
  d01ee091_d30b_6349_ac55_5899bfe9037b["generateDynamicPattern()"]
  588be6b3_7df5_51d9_4f94_ee8db1fac57e -->|calls| d01ee091_d30b_6349_ac55_5899bfe9037b
  style 588be6b3_7df5_51d9_4f94_ee8db1fac57e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/underscore-redirects/src/astro.ts lines 33–131

export function createRedirectsFromAstroRoutes({
	config,
	routeToDynamicTargetMap,
	dir,
	buildOutput,
	assets,
}: CreateRedirectsFromAstroRoutesParams): HostRoutes {
	const base =
		config.base && config.base !== '/'
			? config.base.endsWith('/')
				? config.base.slice(0, -1)
				: config.base
			: '';
	const redirects = new HostRoutes();

	for (const [route, dynamicTarget = ''] of routeToDynamicTargetMap) {
		const distURL = assets?.get(route.pattern);
		// A route with a `pathname` is as static route.
		if (route.pathname) {
			if (route.redirect) {
				// A redirect route without dynamic parts. Get the redirect status
				// from the user if provided.
				redirects.add({
					dynamic: false,
					input: `${base}${route.pathname}`,
					target: typeof route.redirect === 'object' ? route.redirect.destination : route.redirect,
					status: getRedirectStatus(route),
					weight: 2,
				});
				continue;
			}

			// If this is a static build we don't want to add redirects to the HTML file.
			if (buildOutput === 'static') {
				continue;
			}

			if (distURL) {
				redirects.add({
					dynamic: false,
					input: `${base}${route.pathname}`,
					target: prependForwardSlash(distURL.toString().replace(dir.toString(), '')),
					status: 200,
					weight: 2,
				});
			} else {
				redirects.add({
					dynamic: false,
					input: `${base}${route.pathname}`,
					target: dynamicTarget,
					status: 200,
					weight: 2,
				});

				if (route.pattern === '/404') {
					redirects.add({
						dynamic: true,
						input: '/*',
						target: dynamicTarget,
						status: 404,
						weight: 0,
					});
				}
			}
		} else {
			// This is the dynamic route code. This generates a pattern from a dynamic
			// route formatted with *s in place of the Astro dynamic/spread syntax.
			const pattern = generateDynamicPattern(route);

			// This route was prerendered and should be forwarded to the HTML file.
			if (distURL) {
				const targetRoute = route.redirectRoute ?? route;
				let target = generateDynamicPattern(targetRoute);
				if (config.build.format === 'directory') {
					target = pathJoin(target, 'index.html');
				} else {
					target += '.html';
				}
				redirects.add({
					dynamic: true,
					input: `${base}${pattern}`,

Domain

Subdomains

Frequently Asked Questions

What does createRedirectsFromAstroRoutes() do?
createRedirectsFromAstroRoutes() is a function in the astro codebase, defined in packages/underscore-redirects/src/astro.ts.
Where is createRedirectsFromAstroRoutes() defined?
createRedirectsFromAstroRoutes() is defined in packages/underscore-redirects/src/astro.ts at line 33.
What does createRedirectsFromAstroRoutes() call?
createRedirectsFromAstroRoutes() calls 3 function(s): generateDynamicPattern, getRedirectStatus, prependForwardSlash.

Analyze Your Own Codebase

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

Try Supermodel Free