#How to set what route maps to what file
4 messages · Page 1 of 1 (latest)
Hi @frozen musk. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, it is possible to define what route maps to what Astro file. Astro uses file-based routing to generate your build URLs based on the file layout of your project src/pages/ directory. Each .astro page component, as well as Markdown and MDX Files (.md, .mdx) within the src/pages/ directory, automatically become pages on your website. Each page’s route corresponds to its path and filename within the src/pages/ directory.
For example, if you have a file src/pages/about.astro, it will be accessible at mysite.com/about.
Astro also supports dynamic routes. An Astro page file can specify dynamic route parameters in its filename to generate multiple, matching pages. For example, src/pages/authors/[author].astro generates a bio page for every author on your blog. author becomes a parameter that you can access from inside the page.
Additionally, Astro provides an injectRoute option in its Integration API, which allows you to inject routes into an Astro project. Injected routes can be .astro pages or .js and .ts route handlers. injectRoute takes an object with a pattern and an entryPoint. The pattern is where the route should be output in the browser, and the entryPoint is a bare module specifier pointing towards the .astro page or .js/.ts route handler that handles the route denoted in the pattern.
Here's an example usage of injectRoute:
injectRoute({
// Use Astro’s pattern syntax for dynamic routes.
pattern: '/subfolder/[dynamic]',
// Use relative path syntax for a local route.
entryPoint: './src/dynamic-page.astro'
});
Sources:
- Astro Routing Documentation (https://docs.astro.build/en/core-concepts/routing/)
- Astro Integration API Documentation (https://docs.astro.build/en/reference/integrations-reference/)