Discover how one simple route can replace hundreds of repetitive pages!
Why Dynamic routes with parameters in Astro? - Purpose & Use Cases
Imagine building a website where each user has a unique profile page, and you have to create a separate file or page for every single user manually.
Manually creating pages for each user is slow, wastes time, and makes updating or adding new users a huge headache. It's easy to make mistakes and hard to keep everything organized.
Dynamic routes with parameters let you create one template that automatically adapts to different users or items by reading the URL. This means one page can serve many different contents smoothly.
Create /user/john.html, /user/jane.html, /user/bob.html separately
/user/[username].astro handles all users dynamically
You can build flexible websites that show personalized content for many users or items without extra files or repeated work.
An online store showing product detail pages for thousands of products using just one dynamic route template.
Manual page creation for each item is slow and error-prone.
Dynamic routes use URL parameters to serve many pages from one template.
This makes websites easier to build, update, and scale.
Practice
[id].astro represent in Astro?Solution
Step 1: Understand dynamic route syntax
In Astro, square brackets in filenames like[id].astrodefine a dynamic route parameter namedid.Step 2: Recognize the purpose of the parameter
This parameter captures part of the URL, allowing the page to change based on the URL segment.Final Answer:
A dynamic route that captures theidparameter from the URL -> Option AQuick Check:
Dynamic route = filename with [param] [OK]
- Thinking [id].astro is a static file
- Confusing parameter with component name
- Assuming parameters are optional by default
slug inside an Astro component?Solution
Step 1: Recall Astro parameter access
Astro provides dynamic route parameters inside the component viaAstro.params.Step 2: Use the parameter name as key
To get the value ofslug, useAstro.params.slug.Final Answer:
Astro.params.slug-> Option CQuick Check:
Access params with Astro.params.[name] [OK]
- Using Astro.route or Astro.query instead of Astro.params
- Trying to access parameters via props
- Forgetting to use the exact parameter name
src/pages/blog/[post].astro with this code snippet:--- const postId = Astro.params.post; ---Post ID: {postId}
What will be rendered when visiting
/blog/42?Solution
Step 1: Identify dynamic parameter value
The URL/blog/42matches the dynamic route[post].astro, soAstro.params.postis '42'.Step 2: Render the parameter in the template
The component outputs<h1>Post ID: {postId}</h1>, so it showsPost ID: 42.Final Answer:
<h1>Post ID: 42</h1> -> Option AQuick Check:
URL segment fills param = rendered value [OK]
- Expecting literal 'post' instead of parameter value
- Assuming empty or missing parameter
- Confusing 404 with dynamic route handling
[user].astro but when visiting /john you get a runtime error: Cannot read property 'user' of undefined. What is the likely cause?Solution
Step 1: Understand the error message
The error saysCannot read property 'user' of undefined, meaningAstro.paramsis undefined or not accessed properly.Step 2: Check parameter access syntax
AccessingAstro.params.useris correct. If code tries to accessAstro.params.userbutAstro.paramsis undefined, it meansAstro.paramswas not used or imported correctly.Final Answer:
You forgot to useAstro.params.userand accessedAstro.paramsdirectly -> Option BQuick Check:
Always use Astro.params.[param] to avoid undefined errors [OK]
- Using curly braces instead of square brackets in filename
- Assuming Astro.params is always defined globally
- Misnaming the parameter in code
/products/electronics/123. Which file structure and parameter access is correct?Solution
Step 1: Understand nested dynamic routes
To match URLs like/products/electronics/123, you need nested folders with dynamic parameters:[category]and[id].Step 2: Correct file structure and parameter access
The correct structure issrc/pages/products/[category]/[id].astro. Inside, access parameters asAstro.params.categoryandAstro.params.id.Final Answer:
Createsrc/pages/products/[category]/[id].astroand access withAstro.params.categoryandAstro.params.id-> Option DQuick Check:
Nested folders with [param] = nested params [OK]
- Combining multiple params in one filename incorrectly
- Using static folder names instead of dynamic
- Accessing parameters that don't exist in the route
Ready to prove it?
Pass the test and this pattern moves unknown
