Home / Function/ trailingSlashMiddleware() — astro Function Reference

trailingSlashMiddleware() — astro Function Reference

Architecture documentation for the trailingSlashMiddleware() function in trailing-slash.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e4b677c0_b1e3_e081_aebb_860875daf2d7["trailingSlashMiddleware()"]
  883a80dd_99db_76ef_c6ed_7ca089712d0a["trailing-slash.ts"]
  e4b677c0_b1e3_e081_aebb_860875daf2d7 -->|defined in| 883a80dd_99db_76ef_c6ed_7ca089712d0a
  style e4b677c0_b1e3_e081_aebb_860875daf2d7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-astro-server/trailing-slash.ts lines 11–41

export function trailingSlashMiddleware(settings: AstroSettings): vite.Connect.NextHandleFunction {
	const { trailingSlash } = settings.config;

	return function devTrailingSlash(req, res, next) {
		const url = new URL(`http://localhost${req.url}`);
		let pathname: string;
		try {
			pathname = decodeURI(url.pathname);
		} catch (e) {
			/* malformed uri */
			return next(e);
		}
		if (isInternalPath(pathname)) {
			return next();
		}

		const destination = collapseDuplicateTrailingSlashes(pathname, true);
		if (pathname && destination !== pathname) {
			return writeRedirectResponse(res, 301, `${destination}${url.search}`);
		}

		if (
			(trailingSlash === 'never' && pathname.endsWith('/') && pathname !== '/') ||
			(trailingSlash === 'always' && !pathname.endsWith('/') && !hasFileExtension(pathname))
		) {
			const html = trailingSlashMismatchTemplate(pathname, trailingSlash);
			return writeHtmlResponse(res, 404, html);
		}
		return next();
	};
}

Domain

Subdomains

Frequently Asked Questions

What does trailingSlashMiddleware() do?
trailingSlashMiddleware() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-astro-server/trailing-slash.ts.
Where is trailingSlashMiddleware() defined?
trailingSlashMiddleware() is defined in packages/astro/src/vite-plugin-astro-server/trailing-slash.ts at line 11.

Analyze Your Own Codebase

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

Try Supermodel Free