Introduction to Dynamic Routing

Learn how to work with dynamic routes to programmatically generate pages.

Dynamic routes in Astro can be generated statically during the build process, aligning with the SSG approach. This is the opposite of SSR,It stands for server-side rendering. where pages are rendered on the server on demand. When using SSR, usually a single entry point is generated that handles dynamic routes on the server. For SSR, however, dynamic routes are generated into static HTML files during build time.

Astro uses file-based routing to generate pages. By using custom file names, we can dynamically generate routes using a single file with different types of data—for example, to generate posts using a single file, we can create a new file inside the pages folder called [post].astro. The brackets signal Astro that this is a dynamic route. Of course, we need a way to tell Astro what data should be used with this template so it can generate the URLs accordingly.

Static vs. dynamic routing

Before we jump into looking at how dynamic routing works in Astro, let’s do a quick recap on how static routes are generated for a quick comparison. Astro uses file-based routing to generate URLs. Each file inside the src/pages folder will be generated into a route based on the file name.

Get hands-on with 1400+ tech skills courses.