Repository: withastro/astro
File: packages/astro/src/i18n/index.ts (TypeScript)

Code before:
			// 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}`, ``);
						// Ensure the pathname are non-empty. Redirects like "/fr" => "" may create infinite loops.
						if (newPathname === "") {
							newPathname = "/";
						}
					} 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;
	};
}

// NOTE: public function exported to the users via `astro:i18n` module
export function createMiddleware(
	i18nManifest: SSRManifest['i18n'],
	base: SSRManifest['base'],

Review comment (suggestion):
Can you please add a more generic comment? For example, the `/fr` part has no context and it isn't related to the fix

Code after:
export type RedirectToFallback = (context: APIContext, response: Response) => Promise<Response>;

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}`, ``);
						// Ensure the pathname are non-empty. Redirects like "/fr" => "" may create infinite loops,
						// as the "Location" response header is empty.
						if (newPathname === '') {
							newPathname = '/';
						}
					} else {
						newPathname = context.url.pathname.replace(`/${urlLocale}`, `/`);
					}
				} else {
					newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
				}

				if (fallbackType === 'rewrite') {