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

Code before:
	fallback: Record<string, string> | undefined;
	fallbackType: 'redirect' | 'rewrite';
};

// NOTE: public function exported to the users via `astro:i18n` module
export function redirectToDefaultLocale({
	trailingSlash,
	format,
	base,
	defaultLocale,
}: MiddlewarePayload) {
	return function (context: APIContext, statusCode?: ValidRedirectStatus) {
		if (shouldAppendForwardSlash(trailingSlash, format)) {
			return context.redirect(`${appendForwardSlash(joinPaths(base, defaultLocale))}`, statusCode);
		} else {
			return context.redirect(`${joinPaths(base, defaultLocale)}`, statusCode);
		}
	};
}

// NOTE: public function exported to the users via `astro:i18n` module
export function notFound({ base, locales }: MiddlewarePayload) {
	return function (context: APIContext, response?: Response): Response | undefined {
		if (response?.headers.get(REROUTE_DIRECTIVE_HEADER) === 'no') return response;

		const url = context.url;
		// We return a 404 if:
		// - the current path isn't a root. e.g. / or /<base>
		// - the URL doesn't contain a locale
		const isRoot = url.pathname === base + '/' || url.pathname === base;
		if (!(isRoot || pathHasLocale(url.pathname, locales))) {
			if (response) {
				response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
				return new Response(response.body, {
					status: 404,
					headers: response.headers,
				});
			} else {
				return new Response(null, {
					status: 404,
					headers: {
						[REROUTE_DIRECTIVE_HEADER]: 'no',
					},
				});
			}
		}

		return undefined;
	};
}


Review comment (suggestion):
Even though we're about to render the internal `404` or `500`, we still want to handle the fallback

Code after:
	fallback: Record<string, string> | undefined;
	fallbackType: 'redirect' | 'rewrite';
};

// NOTE: public function exported to the users via `astro:i18n` module
export function redirectToDefaultLocale({
	trailingSlash,
	format,
	base,
	defaultLocale,
}: MiddlewarePayload) {
	return function (context: APIContext, statusCode?: ValidRedirectStatus) {
		if (shouldAppendForwardSlash(trailingSlash, format)) {
			return context.redirect(`${appendForwardSlash(joinPaths(base, defaultLocale))}`, statusCode);
		} else {
			return context.redirect(`${joinPaths(base, defaultLocale)}`, statusCode);
		}
	};
}

// NOTE: public function exported to the users via `astro:i18n` module
export function notFound({ base, locales, fallback }: MiddlewarePayload) {
	return function (context: APIContext, response?: Response): Response | undefined {
		if (
			response?.headers.get(REROUTE_DIRECTIVE_HEADER) === 'no' &&
			typeof fallback === 'undefined'
		) {
			return response;
		}

		const url = context.url;
		// We return a 404 if:
		// - the current path isn't a root. e.g. / or /<base>
		// - the URL doesn't contain a locale
		const isRoot = url.pathname === base + '/' || url.pathname === base;
		if (!(isRoot || pathHasLocale(url.pathname, locales))) {
			if (response) {
				response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
				return new Response(response.body, {
					status: 404,
					headers: response.headers,
				});
			} else {
				return new Response(null, {
					status: 404,
					headers: {
						[REROUTE_DIRECTIVE_HEADER]: 'no',
					},
				});
			}
		}