Home / Function/ astroPluginRoutes() — astro Function Reference

astroPluginRoutes() — astro Function Reference

Architecture documentation for the astroPluginRoutes() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  7709c418_1015_fc21_7a1b_c59a6267e964["astroPluginRoutes()"]
  63c98f38_799c_f0f6_b3c0_59e116a89b1d["index.ts"]
  7709c418_1015_fc21_7a1b_c59a6267e964 -->|defined in| 63c98f38_799c_f0f6_b3c0_59e116a89b1d
  style 7709c418_1015_fc21_7a1b_c59a6267e964 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-routes/index.ts lines 33–235

export default async function astroPluginRoutes({
	settings,
	logger,
	fsMod,
	routesList: initialRoutesList,
	command,
}: Payload): Promise<Plugin> {
	logger.debug('update', 'Re-calculate routes');

	let serializedRouteInfo: SerializedRouteInfo[] = initialRoutesList.routes.map(
		(r): SerializedRouteInfo => {
			return {
				file: '',
				links: [],
				scripts: [],
				styles: [],
				routeData: serializeRouteData(r, settings.config.trailingSlash),
			};
		},
	);

	async function rebuildRoutes(path: string | null = null, server: ViteDevServer) {
		if (path != null && path.startsWith(settings.config.srcDir.pathname)) {
			logger.debug(
				'update',
				`Re-calculating routes for ${path.slice(settings.config.srcDir.pathname.length)}`,
			);
			const file = pathToFileURL(normalizePath(path));
			const newRoutesList = await createRoutesList(
				{
					settings,
					fsMod,
				},
				logger,
				{ dev: command === 'dev' },
			);

			// IMPORTANT: Mutate the shared routesList object so all plugins see the update.
			// Other plugins (pluginPage, pluginPages, astroDevCssPlugin) capture routesList
			// at creation time, so we must mutate the array in place rather than replacing it.
			initialRoutesList.routes.length = 0;
			initialRoutesList.routes.push(...newRoutesList.routes);

			serializedRouteInfo = initialRoutesList.routes.map((r): SerializedRouteInfo => {
				return {
					file: fileURLToPath(file),
					links: [],
					scripts: [],
					styles: [],
					routeData: serializeRouteData(r, settings.config.trailingSlash),
				};
			});
			let environment = server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr];
			const virtualMod = environment.moduleGraph.getModuleById(ASTRO_ROUTES_MODULE_ID_RESOLVED);
			if (!virtualMod) return;

			environment.moduleGraph.invalidateModule(virtualMod);

			// Signal that routes have changed so running apps can update
			// NOTE: Consider adding debouncing here if rapid file changes cause performance issues
			environment.hot.send('astro:routes-updated', {});
		}
	}
	return {
		name: 'astro:routes',
		configureServer(server) {
			server.watcher.on('add', (path) => rebuildRoutes(path, server));
			server.watcher.on('unlink', (path) => rebuildRoutes(path, server));
			server.watcher.on('change', (path) => rebuildRoutes(path, server));
		},

		applyToEnvironment(environment) {
			return (
				environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.astro ||
				environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.ssr ||
				environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.prerender
			);
		},

		resolveId: {
			filter: {

Domain

Subdomains

Frequently Asked Questions

What does astroPluginRoutes() do?
astroPluginRoutes() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-routes/index.ts.
Where is astroPluginRoutes() defined?
astroPluginRoutes() is defined in packages/astro/src/vite-plugin-routes/index.ts at line 33.

Analyze Your Own Codebase

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

Try Supermodel Free