
π Dynamic Routes with Server Rendering in Astro
"Astro is a modern web framework that focuses on performance, simplicity, and flexibility. One of its powerful features is server-side rendering (SSR) combined with dynamic routing, which enables developers to build highly dynamic, SEO-friendly pages with real-time data."

Gideon-Yebei
Frontend Developer
In this post, weβll walk through:
What dynamic routes are in Astro
How to create dynamic routes
How to use server rendering to fetch dynamic data
A practical example using a blog post page
π§ What Are Dynamic Routes?
Dynamic routes are routes that change based on URL parameters. For example:
/blog/hello-world.astro
/blog/my-second-post.astro
Instead of hardcoding these routes, you define a dynamic route in Astro like this:
/blog/[slug].astro
The **[slug] ** part is a dynamic segment, which Astro interprets as a variable.
βοΈ Setting Up a Dynamic Route in Astro
Letβs say youβre building a blog and want to render posts based on their slug.
Step 1: Create the File
Create a file under src/pages/blog/[slug].astro:
touch src/pages/blog/[slug].astro
Step 2: Enable SSR
Astro supports SSR out of the box if you're using an SSR adapter like @astrojs/node, @astrojs/vercel, or @astrojs/netlify.
In astro.config.mjs, enable SSR:
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server',
adapter: node()
});
Enable SSR
π Fetching Dynamic Data with getStaticPaths or getServerSideProps
Option A: getStaticPaths (SSG - Static Site Generation)
Use this if your content is known at build time:
---
export async function getStaticPaths() {
const posts = await fetchPosts(); // From local data or CMS
return posts.map(post => ({
params: { slug: post.slug }
}));
}
const { slug } = Astro.params;
const post = await getPostBySlug(slug);
---
{post.title}
{post.title}
{post.content}
Using getStaticPaths
π¦ Deployment Considerations
If using SSR, ensure you're deploying to a platform that supports SSR, such as:
Vercel with* @astrojs/vercel*
Netlify with @astrojs/netlify
Node.js with @astrojs/node adapter
β Benefits of Server-Rendered Dynamic Routes
SEO-Friendly: Content is delivered as fully rendered HTML.
Real-Time: Can show up-to-date content from a database or API.
Scalable: Each page only fetches the data it needs.
β¨ Useful Links
How to Implement Dynamic Routing in Astro Static Sites
Want to make your Astro static site more dynamic? In this tutorial, weβll walk you through how to implement dynamic routing in Astro using real data from an API. Youβll learn how to generate multiple pages from a single template, use the [slug].astro pattern, and dynamically fetch content to display on each route. This approach helps you build fast, content-rich sites with the performance benefits of static site generation.

About Gideon-Yebei
Frontend Developer
Frontend developer and UI/UX designer with a focus on modern web technologies and user experience.