Home / Function/ redirectToFallback() — astro Function Reference

redirectToFallback() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  36f44c90_d8b0_06bb_56d5_2aa4474c23dd["redirectToFallback()"]
  e4a1ef92_c598_36b5_dc33_9ab2535360bc["index.ts"]
  36f44c90_d8b0_06bb_56d5_2aa4474c23dd -->|defined in| e4a1ef92_c598_36b5_dc33_9ab2535360bc
  c08b3bda_012f_60ce_98b8_ee2d9f91b5d8["getPathByLocale()"]
  36f44c90_d8b0_06bb_56d5_2aa4474c23dd -->|calls| c08b3bda_012f_60ce_98b8_ee2d9f91b5d8
  style 36f44c90_d8b0_06bb_56d5_2aa4474c23dd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/i18n/index.ts lines 366–418

export function redirectToFallback({
	fallback,
	locales,
	defaultLocale,
	strategy,
	base,
	fallbackType,
}: MiddlewarePayload) {
	return async function (context: APIContext, response: Response): Promise<Response> {
		if (response.status >= 300 && fallback) {
			const fallbackKeys = fallback ? Object.keys(fallback) : [];
			// we split the URL using the `/`, and then check in the returned array we have the locale
			const segments = context.url.pathname.split('/');
			const urlLocale = segments.find((segment) => {
				for (const locale of locales) {
					if (typeof locale === 'string') {
						if (locale === segment) {
							return true;
						}
					} else if (locale.path === segment) {
						return true;
					}
				}
				return false;
			});

			if (urlLocale && fallbackKeys.includes(urlLocale)) {
				const fallbackLocale = fallback[urlLocale];
				// the user might have configured the locale using the granular locales, so we want to retrieve its corresponding path instead
				const pathFallbackLocale = getPathByLocale(fallbackLocale, locales);
				let newPathname: string;
				// If a locale falls back to the default locale, we want to **remove** the locale because
				// the default locale doesn't have a prefix
				if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') {
					if (context.url.pathname.includes(`${base}`)) {
						newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
					} else {
						newPathname = context.url.pathname.replace(`/${urlLocale}`, `/`);
					}
				} else {
					newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
				}

				if (fallbackType === 'rewrite') {
					return await context.rewrite(newPathname + context.url.search);
				} else {
					return context.redirect(newPathname + context.url.search);
				}
			}
		}
		return response;
	};
}

Domain

Subdomains

Frequently Asked Questions

What does redirectToFallback() do?
redirectToFallback() is a function in the astro codebase, defined in packages/astro/src/i18n/index.ts.
Where is redirectToFallback() defined?
redirectToFallback() is defined in packages/astro/src/i18n/index.ts at line 366.
What does redirectToFallback() call?
redirectToFallback() calls 1 function(s): getPathByLocale.

Analyze Your Own Codebase

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

Try Supermodel Free