Home / Function/ baseMiddleware() — astro Function Reference

baseMiddleware() — astro Function Reference

Architecture documentation for the baseMiddleware() function in base.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  2d5bb910_4a6c_8f43_6d0e_7a23f797c9d6["baseMiddleware()"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974["base.ts"]
  2d5bb910_4a6c_8f43_6d0e_7a23f797c9d6 -->|defined in| 3d5d82b6_fa01_d874_f359_b0b54bb2e974
  style 2d5bb910_4a6c_8f43_6d0e_7a23f797c9d6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-astro-server/base.ts lines 11–70

export function baseMiddleware(
	settings: AstroSettings,
	logger: Logger,
): vite.Connect.NextHandleFunction {
	const { config } = settings;
	// The base may be an empty string by now, causing the URL creation to fail. We provide a default instead
	const base = config.base || '/';
	const site = config.site ? new URL(base, config.site) : undefined;
	const devRootURL = new URL(base, 'http://localhost');
	const devRoot = site ? site.pathname : devRootURL.pathname;
	const devRootReplacement = devRoot.endsWith('/') ? '/' : '';

	return function devBaseMiddleware(req, res, next) {
		const url = req.url!;
		let pathname: string;
		try {
			pathname = decodeURI(new URL(url, 'http://localhost').pathname);
		} catch (e) {
			/* malformed uri */
			return next(e);
		}

		if (pathname.startsWith(devRoot)) {
			req.url = url.replace(devRoot, devRootReplacement);
			return next();
		}

		if (pathname === '/' || pathname === '/index.html') {
			const html = subpathNotUsedTemplate(devRoot, pathname);
			return writeHtmlResponse(res, 404, html);
		}

		if (req.headers.accept?.includes('text/html')) {
			const html = notFoundTemplate(pathname);
			return writeHtmlResponse(res, 404, html);
		}

		// Check to see if it's in public and if so 404
		const publicPath = new URL('.' + req.url, config.publicDir);
		fs.stat(publicPath, (_err, stats) => {
			if (stats) {
				const publicDir = appendForwardSlash(
					path.posix.relative(config.root.pathname, config.publicDir.pathname),
				);
				const expectedLocation = new URL(devRootURL.pathname + url, devRootURL).pathname;

				logger.error(
					'router',
					`Request URLs for ${colors.bold(
						publicDir,
					)} assets must also include your base. "${expectedLocation}" expected, but received "${url}".`,
				);
				const html = subpathNotUsedTemplate(devRoot, pathname);
				return writeHtmlResponse(res, 404, html);
			} else {
				next();
			}
		});
	};
}

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free