Home / Function/ createFileBasedRoutes() — astro Function Reference

createFileBasedRoutes() — astro Function Reference

Architecture documentation for the createFileBasedRoutes() function in create.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  769abfe3_0dff_1eda_a9cd_919cc8ae8621["createFileBasedRoutes()"]
  a7eed989_9f2b_5163_ecdf_85920a8b973f["create.ts"]
  769abfe3_0dff_1eda_a9cd_919cc8ae8621 -->|defined in| a7eed989_9f2b_5163_ecdf_85920a8b973f
  980f5a1b_85ee_0217_7a04_888abfb8f70f["createRoutesList()"]
  980f5a1b_85ee_0217_7a04_888abfb8f70f -->|calls| 769abfe3_0dff_1eda_a9cd_919cc8ae8621
  1238e67c_a57d_fa2d_67fb_033b7f31b2ba["getParts()"]
  769abfe3_0dff_1eda_a9cd_919cc8ae8621 -->|calls| 1238e67c_a57d_fa2d_67fb_033b7f31b2ba
  1b408d2e_6c62_4027_ae00_57b93199c0e0["trailingSlashForPath()"]
  769abfe3_0dff_1eda_a9cd_919cc8ae8621 -->|calls| 1b408d2e_6c62_4027_ae00_57b93199c0e0
  83ef6580_f43d_b634_d2ed_d70e0633249d["joinSegments()"]
  769abfe3_0dff_1eda_a9cd_919cc8ae8621 -->|calls| 83ef6580_f43d_b634_d2ed_d70e0633249d
  style 769abfe3_0dff_1eda_a9cd_919cc8ae8621 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/routing/manifest/create.ts lines 111–258

function createFileBasedRoutes(
	{ settings, cwd, fsMod }: CreateRouteManifestParams,
	logger: Logger,
): RouteData[] {
	const components: string[] = [];
	const routes: RouteData[] = [];
	const validPageExtensions = new Set<string>([
		'.astro',
		...SUPPORTED_MARKDOWN_FILE_EXTENSIONS,
		...settings.pageExtensions,
	]);
	const invalidPotentialPages = new Set<string>(['.tsx', '.jsx', '.vue', '.svelte']);
	const validEndpointExtensions = new Set<string>(['.js', '.ts']);
	const localFs = fsMod ?? nodeFs;
	const prerender = getPrerenderDefault(settings.config);

	function walk(
		fs: typeof nodeFs,
		dir: string,
		parentSegments: RoutePart[][],
		parentParams: string[],
	) {
		let items: Item[] = [];
		const files = fs.readdirSync(dir);
		for (const basename of files) {
			const resolved = path.join(dir, basename);
			const file = slash(path.relative(cwd || fileURLToPath(settings.config.root), resolved));
			const isDir = fs.statSync(resolved).isDirectory();

			const ext = path.extname(basename);
			const name = ext ? basename.slice(0, -ext.length) : basename;
			if (name[0] === '_') {
				continue;
			}
			if (basename[0] === '.' && basename !== '.well-known') {
				continue;
			}
			// filter out "foo.astro_tmp" files, etc
			if (!isDir && !validPageExtensions.has(ext) && !validEndpointExtensions.has(ext)) {
				// Only warn for files that could potentially be interpreted by users has being possible extensions for pages
				// It's otherwise not a problem for users to have other files in their pages directory, for instance colocated images.
				if (invalidPotentialPages.has(ext)) {
					logger.warn(
						null,
						`Unsupported file type ${colors.bold(
							resolved,
						)} found in pages directory. Only Astro files can be used as pages. Prefix filename with an underscore (\`_\`) to ignore this warning, or move the file outside of the pages directory.`,
					);
				}

				continue;
			}
			const segment = isDir ? basename : name;
			validateSegment(segment, file);

			const parts = getParts(segment, file);
			const isIndex = isDir ? false : basename.substring(0, basename.lastIndexOf('.')) === 'index';
			const routeSuffix = basename.slice(basename.indexOf('.'), -ext.length);
			const isPage = validPageExtensions.has(ext);

			items.push({
				basename,
				ext,
				parts,
				file: file.replace(/\\/g, '/'),
				isDir,
				isIndex,
				isPage,
				routeSuffix,
			});
		}

		for (const item of items) {
			const segments = parentSegments.slice();

			if (item.isIndex) {
				if (item.routeSuffix) {
					if (segments.length > 0) {
						const lastSegment = segments[segments.length - 1].slice();
						const lastPart = lastSegment[lastSegment.length - 1];

Domain

Subdomains

Called By

Frequently Asked Questions

What does createFileBasedRoutes() do?
createFileBasedRoutes() is a function in the astro codebase, defined in packages/astro/src/core/routing/manifest/create.ts.
Where is createFileBasedRoutes() defined?
createFileBasedRoutes() is defined in packages/astro/src/core/routing/manifest/create.ts at line 111.
What does createFileBasedRoutes() call?
createFileBasedRoutes() calls 3 function(s): getParts, joinSegments, trailingSlashForPath.
What calls createFileBasedRoutes()?
createFileBasedRoutes() is called by 1 function(s): createRoutesList.

Analyze Your Own Codebase

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

Try Supermodel Free