Home / File/ route-guard.ts — astro Source File

route-guard.ts — astro Source File

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

File typescript CoreAstro RoutingSystem 5 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  a9b78244_c0b0_5d0e_6e0e_88d9a32d883c["route-guard.ts"]
  e9b74c5a_8d34_34a7_e196_5e41b87214aa["../types/astro.js"]
  a9b78244_c0b0_5d0e_6e0e_88d9a32d883c --> e9b74c5a_8d34_34a7_e196_5e41b87214aa
  6b38722c_7b48_0d95_7927_ae62c3991719["../template/4xx.js"]
  a9b78244_c0b0_5d0e_6e0e_88d9a32d883c --> 6b38722c_7b48_0d95_7927_ae62c3991719
  e6a60d16_a973_8dde_286e_5c65924c8e9f["./response.js"]
  a9b78244_c0b0_5d0e_6e0e_88d9a32d883c --> e6a60d16_a973_8dde_286e_5c65924c8e9f
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  a9b78244_c0b0_5d0e_6e0e_88d9a32d883c --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  a9b78244_c0b0_5d0e_6e0e_88d9a32d883c --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style a9b78244_c0b0_5d0e_6e0e_88d9a32d883c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import * as fs from 'node:fs';
import type * as vite from 'vite';
import type { AstroSettings } from '../types/astro.js';
import { notFoundTemplate } from '../template/4xx.js';
import { writeHtmlResponse } from './response.js';

// Vite internal prefixes that should always be allowed through
const VITE_INTERNAL_PREFIXES = [
	'/@vite/',
	'/@fs/',
	'/@id/',
	'/__vite',
	'/@react-refresh',
	'/node_modules/',
	'/.astro/',
];

/**
 * Middleware that prevents Vite from serving files that exist outside
 * of srcDir and publicDir when accessed via direct URL navigation.
 *
 * This fixes the issue where files like /README.md are served
 * when they exist at the project root but aren't part of Astro's routing.
 */
export function routeGuardMiddleware(settings: AstroSettings): vite.Connect.NextHandleFunction {
	const { config } = settings;

	return function devRouteGuard(req, res, next) {
		const url = req.url;
		if (!url) {
			return next();
		}

		// Only intercept requests that look like browser navigation (HTML requests)
		// Let all other requests through (JS modules, assets, Vite transforms, etc.)
		const accept = req.headers.accept || '';
		if (!accept.includes('text/html')) {
			return next();
		}

		let pathname: string;
		try {
			pathname = decodeURI(new URL(url, 'http://localhost').pathname);
		} catch {
			// Malformed URI, let other middleware handle it
			return next();
		}

		// Always allow Vite internal paths through
		if (VITE_INTERNAL_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
			return next();
		}

		// Always allow requests with query params (Vite transform requests like ?url, ?raw)
		if (url.includes('?')) {
			return next();
		}

		// Check if the file exists in publicDir - allow if so
		const publicFilePath = new URL('.' + pathname, config.publicDir);
		if (fs.existsSync(publicFilePath)) {
			return next();
		}

		// Check if the file exists in srcDir - allow if so (potential route)
		const srcFilePath = new URL('.' + pathname, config.srcDir);
		if (fs.existsSync(srcFilePath)) {
			return next();
		}

		// Check if the file exists at project root (outside srcDir/publicDir)
		const rootFilePath = new URL('.' + pathname, config.root);
		if (fs.existsSync(rootFilePath)) {
			// File exists at root but not in srcDir or publicDir - block it
			const html = notFoundTemplate(pathname);
			return writeHtmlResponse(res, 404, html);
		}

		// File doesn't exist anywhere, let other middleware handle it
		return next();
	};
}

Domain

Subdomains

Dependencies

  • ../template/4xx.js
  • ../types/astro.js
  • ./response.js
  • node:fs
  • vite

Frequently Asked Questions

What does route-guard.ts do?
route-guard.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in route-guard.ts?
route-guard.ts defines 1 function(s): routeGuardMiddleware.
What does route-guard.ts depend on?
route-guard.ts imports 5 module(s): ../template/4xx.js, ../types/astro.js, ./response.js, node:fs, vite.
Where is route-guard.ts in the architecture?
route-guard.ts is located at packages/astro/src/vite-plugin-astro-server/route-guard.ts (domain: CoreAstro, subdomain: RoutingSystem, 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