Tillitsdone
down Scroll to discover

Routing Basics in Astro.js: Static & Dynamic Routes

Learn the fundamentals of Astro.js routing system, from basic static routes to dynamic route generation.

Discover how file-based routing works and implement flexible URL structures.
thumbnail

Routing Basics in Astro.js: Static and Dynamic Routes

A minimalist geometric path through a crystal maze featuring interconnected translucent crystal structures dominant light blue and silver colors sharp crystalline formations representing routes aerial view perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Ever wondered how Astro.js handles routing so seamlessly? Let’s dive into the world of static and dynamic routes in Astro.js - a topic that might seem complex at first but becomes crystal clear once you understand the basics.

Understanding File-Based Routing

At its core, Astro.js follows a file-based routing system, which means your file structure directly maps to your website’s URLs. It’s like organizing your room - every item has its designated place, and you know exactly where to find it.

Think of your src/pages directory as the blueprint of your website. When you create a file here, Astro automatically generates a corresponding route. For example, src/pages/about.astro becomes /about, and src/pages/blog.astro transforms into /blog. Simple, right?

Abstract flowing pathways made of light trails against a dark background featuring bright cobalt blue and silver streaks forming interconnected network patterns low angle shot looking upward high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Static Routes: The Foundation

Static routes are like the reliable friends in your life - they’re consistent and predictable. These are your basic pages with fixed URLs that don’t change based on user input or data.

Here’s how you might structure your static routes:

src/pages/
├── index.astro // homepage (/)
├── about.astro // about page (/about)
└── contact.astro // contact page (/contact)

Dynamic Routes: The Game Changer

Now, let’s talk about dynamic routes - the flexible powerhouse of Astro.js routing. They’re perfect when you need URLs that adapt to your content, like blog posts or product pages.

A futuristic crystal landscape with floating geometric shapes featuring bright blue and silver crystals casting light reflections dominant bright colors shot from ground level perspective looking up at floating elements high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

To create dynamic routes, use square brackets in your file names to define parameters. For instance, src/pages/blog/[slug].astro will match URLs like /blog/my-first-post or /blog/astro-is-awesome.

Here’s a practical example:

---
export async function getStaticPaths() {
const posts = await getPosts();
return posts.map(post => ({
params: { slug: post.slug },
props: { post }
}));
}
const { post } = Astro.props;
---
<h1>{post.title}</h1>

Rest Parameters: The Swiss Army Knife

For those situations where you need even more flexibility, Astro.js offers rest parameters using [...path]. This is particularly useful for catch-all routes or nested dynamic content.

Remember, while this power is amazing, use it wisely to maintain a clean and intuitive URL structure for your users.

A vast landscape of interconnected geometric crystal formations stretching to horizon featuring metallic silver and bright blue crystal structures catching light panoramic view from elevated perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/code-outline.svg AstroJs Blogs
Astro is an all-in-one web framework. It includes everything you need to create a website, built-in.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.