Home / File/ trailing-slash.ts — astro Source File

trailing-slash.ts — astro Source File

Architecture documentation for trailing-slash.ts, a typescript file in the astro codebase. 5 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 5 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  883a80dd_99db_76ef_c6ed_7ca089712d0a["trailing-slash.ts"]
  6b38722c_7b48_0d95_7927_ae62c3991719["../template/4xx.js"]
  883a80dd_99db_76ef_c6ed_7ca089712d0a --> 6b38722c_7b48_0d95_7927_ae62c3991719
  e9b74c5a_8d34_34a7_e196_5e41b87214aa["../types/astro.js"]
  883a80dd_99db_76ef_c6ed_7ca089712d0a --> e9b74c5a_8d34_34a7_e196_5e41b87214aa
  e6a60d16_a973_8dde_286e_5c65924c8e9f["./response.js"]
  883a80dd_99db_76ef_c6ed_7ca089712d0a --> e6a60d16_a973_8dde_286e_5c65924c8e9f
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  883a80dd_99db_76ef_c6ed_7ca089712d0a --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  883a80dd_99db_76ef_c6ed_7ca089712d0a --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 883a80dd_99db_76ef_c6ed_7ca089712d0a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import {
	collapseDuplicateTrailingSlashes,
	hasFileExtension,
	isInternalPath,
} from '@astrojs/internal-helpers/path';
import type * as vite from 'vite';
import { trailingSlashMismatchTemplate } from '../template/4xx.js';
import type { AstroSettings } from '../types/astro.js';
import { writeHtmlResponse, writeRedirectResponse } from './response.js';

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

Dependencies

  • ../template/4xx.js
  • ../types/astro.js
  • ./response.js
  • path
  • vite

Frequently Asked Questions

What does trailing-slash.ts do?
trailing-slash.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in trailing-slash.ts?
trailing-slash.ts defines 1 function(s): trailingSlashMiddleware.
What does trailing-slash.ts depend on?
trailing-slash.ts imports 5 module(s): ../template/4xx.js, ../types/astro.js, ./response.js, path, vite.
Where is trailing-slash.ts in the architecture?
trailing-slash.ts is located at packages/astro/src/vite-plugin-astro-server/trailing-slash.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-astro-server).

Analyze Your Own Codebase

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

Try Supermodel Free