Home / File/ middleware.ts — astro Source File

middleware.ts — astro Source File

Architecture documentation for middleware.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 6 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  600912f7_037f_fe87_000e_5a651ef00995["middleware.ts"]
  a4f2698c_5256_262a_ba7c_f72b51878d10["../core/app/types.js"]
  600912f7_037f_fe87_000e_5a651ef00995 --> a4f2698c_5256_262a_ba7c_f72b51878d10
  7216d952_4e4a_2d18_a85b_74b4ace79e2b["../core/constants.js"]
  600912f7_037f_fe87_000e_5a651ef00995 --> 7216d952_4e4a_2d18_a85b_74b4ace79e2b
  a3d56727_13c4_68fd_1bf6_c0b09d68466f["../core/routing/match.js"]
  600912f7_037f_fe87_000e_5a651ef00995 --> a3d56727_13c4_68fd_1bf6_c0b09d68466f
  135a8084_d596_67c2_9209_cca6693604e6["../types/public/common.js"]
  600912f7_037f_fe87_000e_5a651ef00995 --> 135a8084_d596_67c2_9209_cca6693604e6
  28857b9f_4720_3f29_4abb_a7eec34dcca5["../types/public/context.js"]
  600912f7_037f_fe87_000e_5a651ef00995 --> 28857b9f_4720_3f29_4abb_a7eec34dcca5
  fe669073_2e4e_f283_db9d_18a6d7df3f04["../i18n/index.js"]
  600912f7_037f_fe87_000e_5a651ef00995 --> fe669073_2e4e_f283_db9d_18a6d7df3f04
  style 600912f7_037f_fe87_000e_5a651ef00995 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { SSRManifest, SSRManifestI18n } from '../core/app/types.js';
import { REROUTE_DIRECTIVE_HEADER, ROUTE_TYPE_HEADER } from '../core/constants.js';
import { isRequestServerIsland, requestIs404Or500 } from '../core/routing/match.js';
import type { MiddlewareHandler } from '../types/public/common.js';
import type { APIContext } from '../types/public/context.js';
import {
	type MiddlewarePayload,
	normalizeTheLocale,
	notFound,
	redirectToDefaultLocale,
	redirectToFallback,
	requestHasLocale,
} from './index.js';

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);
// ... (107 more lines)

Domain

Subdomains

Dependencies

  • ../core/app/types.js
  • ../core/constants.js
  • ../core/routing/match.js
  • ../i18n/index.js
  • ../types/public/common.js
  • ../types/public/context.js

Frequently Asked Questions

What does middleware.ts do?
middleware.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in middleware.ts?
middleware.ts defines 2 function(s): createI18nMiddleware, localeHasntDomain.
What does middleware.ts depend on?
middleware.ts imports 6 module(s): ../core/app/types.js, ../core/constants.js, ../core/routing/match.js, ../i18n/index.js, ../types/public/common.js, ../types/public/context.js.
Where is middleware.ts in the architecture?
middleware.ts is located at packages/astro/src/i18n/middleware.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/astro/src/i18n).

Analyze Your Own Codebase

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

Try Supermodel Free