Home / Function/ createI18nMiddleware() — astro Function Reference

createI18nMiddleware() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  db3f5a0c_7643_a8c2_6cdf_1e2659eca2df["createI18nMiddleware()"]
  600912f7_037f_fe87_000e_5a651ef00995["middleware.ts"]
  db3f5a0c_7643_a8c2_6cdf_1e2659eca2df -->|defined in| 600912f7_037f_fe87_000e_5a651ef00995
  b0e490ed_fdbd_4d8f_517c_57abc53f5af4["localeHasntDomain()"]
  db3f5a0c_7643_a8c2_6cdf_1e2659eca2df -->|calls| b0e490ed_fdbd_4d8f_517c_57abc53f5af4
  style db3f5a0c_7643_a8c2_6cdf_1e2659eca2df fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/i18n/middleware.ts lines 15–152

export function createI18nMiddleware(
	i18n: SSRManifest['i18n'],
	base: SSRManifest['base'],
	trailingSlash: SSRManifest['trailingSlash'],
	format: SSRManifest['buildFormat'],
): MiddlewareHandler {
	if (!i18n) return (_, next) => next();
	const payload: MiddlewarePayload = {
		...i18n,
		trailingSlash,
		base,
		format,
		domains: {},
	};
	const _redirectToDefaultLocale = redirectToDefaultLocale(payload);
	const _noFoundForNonLocaleRoute = notFound(payload);
	const _requestHasLocale = requestHasLocale(payload.locales);
	const _redirectToFallback = redirectToFallback(payload);

	const prefixAlways = (context: APIContext, response: Response): Response | undefined => {
		const url = context.url;
		if (url.pathname === base + '/' || url.pathname === base) {
			return _redirectToDefaultLocale(context);
		}

		// Astro can't know where the default locale is supposed to be, so it returns a 404.
		else if (!_requestHasLocale(context)) {
			return _noFoundForNonLocaleRoute(context, response);
		}

		return undefined;
	};

	const prefixOtherLocales = (context: APIContext, response: Response): Response | undefined => {
		let pathnameContainsDefaultLocale = false;
		const url = context.url;
		for (const segment of url.pathname.split('/')) {
			if (normalizeTheLocale(segment) === normalizeTheLocale(i18n.defaultLocale)) {
				pathnameContainsDefaultLocale = true;
				break;
			}
		}
		if (pathnameContainsDefaultLocale) {
			const newLocation = url.pathname.replace(`/${i18n.defaultLocale}`, '');
			response.headers.set('Location', newLocation);
			return _noFoundForNonLocaleRoute(context);
		}

		return undefined;
	};

	return async (context, next) => {
		const response = await next();
		const type = response.headers.get(ROUTE_TYPE_HEADER);

		// This is case where we are internally rendering a 404/500, so we need to bypass checks that were done already
		const isReroute = response.headers.get(REROUTE_DIRECTIVE_HEADER);
		if (isReroute === 'no' && typeof i18n.fallback === 'undefined') {
			return response;
		}
		// If the route we're processing is not a page, then we ignore it
		if (type !== 'page' && type !== 'fallback') {
			return response;
		}

		// 404 and 500 are **known** routes (users can have their custom pages), so we need to let them be
		if (requestIs404Or500(context.request, base)) {
			return response;
		}

		// This is a case where the rendering phase belongs to a server island. Server island are
		// special routes, and should be exhempt from i18n routing
		if (isRequestServerIsland(context.request, base)) {
			return response;
		}

		const { currentLocale } = context;
		switch (i18n.strategy) {
			// NOTE: theoretically, we should never hit this code path
			case 'manual': {
				return response;

Domain

Subdomains

Frequently Asked Questions

What does createI18nMiddleware() do?
createI18nMiddleware() is a function in the astro codebase, defined in packages/astro/src/i18n/middleware.ts.
Where is createI18nMiddleware() defined?
createI18nMiddleware() is defined in packages/astro/src/i18n/middleware.ts at line 15.
What does createI18nMiddleware() call?
createI18nMiddleware() calls 1 function(s): localeHasntDomain.

Analyze Your Own Codebase

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

Try Supermodel Free