A dynamic route in Astro is a page path that includes parameters, allowing the URL to change based on the parameter value. It uses square brackets in the filename, like [param].astro, to capture parts of the URL.
Click to reveal answer
beginner
How do you define a dynamic route parameter in Astro?
You define a dynamic route parameter by naming the file or folder with square brackets, for example, src/pages/blog/[slug].astro. The part inside brackets (slug) becomes the parameter name.
Click to reveal answer
intermediate
How can you access the dynamic route parameter inside an Astro component?
Inside an Astro component, you can access the dynamic route parameter from the <code>Astro.params</code> object. For example, <code>const { slug } = Astro.params;</code> gets the value of the <code>slug</code> parameter.
Click to reveal answer
beginner
Why are dynamic routes useful in web development?
Dynamic routes let you create pages that change based on the URL, like blog posts or user profiles, without making a separate file for each page. This saves time and keeps your project organized.
Click to reveal answer
intermediate
What happens if you create a file named [id].astro inside src/pages/products/?
Astro treats this as a dynamic route where id is a parameter. URLs like /products/123 or /products/abc will render this page, and you can use Astro.params.id to get the value (e.g., '123' or 'abc').
Click to reveal answer
How do you name a file to create a dynamic route parameter called 'userId' in Astro?
A[userId].astro
BuserId.astro
C{userId}.astro
D(userId).astro
✗ Incorrect
Astro uses square brackets to define dynamic route parameters, so the correct file name is [userId].astro.
Where do you find the value of a dynamic route parameter inside an Astro page?
AAstro.props
BAstro.params
CAstro.state
DAstro.route
✗ Incorrect
Dynamic route parameters are available inside the Astro component via Astro.params.
What URL would match the dynamic route file src/pages/blog/[post].astro?
A/blog/hello-world
B/blog
C/blog/
D/blog-post
✗ Incorrect
The dynamic segment [post] matches any value after /blog/, so /blog/hello-world matches.
If you want to create a route for user profiles with URLs like /users/123, what should the file path be?
Asrc/pages/users/{id}.astro
Bsrc/pages/users/id.astro
Csrc/pages/users/(id).astro
Dsrc/pages/users/[id].astro
✗ Incorrect
Using square brackets in the filename creates a dynamic route parameter, so [id].astro is correct.
What is the main benefit of using dynamic routes in Astro?
AAvoid writing any code
BMake pages load faster
CCreate many pages with one file using URL parameters
DDisable routing
✗ Incorrect
Dynamic routes let you handle many URLs with one file by using parameters.
Explain how to create and use a dynamic route with a parameter in Astro.
Think about how the file name relates to the URL and how to get the parameter inside the page.
You got /4 concepts.
Describe a real-life example where dynamic routes with parameters are helpful in a website.
Imagine a website with many similar pages that differ only by some ID or name in the URL.
You got /4 concepts.
Practice
(1/5)
1. What does a filename like [id].astro represent in Astro?
easy
A. A dynamic route that captures the id parameter from the URL
B. A static page named literally [id]
C. A component that cannot receive parameters
D. A special Astro configuration file
Solution
Step 1: Understand dynamic route syntax
In Astro, square brackets in filenames like [id].astro define a dynamic route parameter named id.
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 the id parameter from the URL -> Option A
Quick Check:
Dynamic route = filename with [param] [OK]
Hint: Look for square brackets in filename for dynamic parameters [OK]
Common Mistakes:
Thinking [id].astro is a static file
Confusing parameter with component name
Assuming parameters are optional by default
2. Which is the correct way to access a dynamic route parameter named slug inside an Astro component?
easy
A. Astro.query.slug
B. Astro.route.slug
C. Astro.params.slug
D. Astro.props.slug
Solution
Step 1: Recall Astro parameter access
Astro provides dynamic route parameters inside the component via Astro.params.
Step 2: Use the parameter name as key
To get the value of slug, use Astro.params.slug.
Final Answer:
Astro.params.slug -> Option C
Quick Check:
Access params with Astro.params.[name] [OK]
Hint: Use Astro.params to get dynamic route values [OK]
Common Mistakes:
Using Astro.route or Astro.query instead of Astro.params
Trying to access parameters via props
Forgetting to use the exact parameter name
3. Given a file named 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?
medium
A.
Post ID: 42
B.
Post ID: post
C.
Post ID:
D. 404 Not Found
Solution
Step 1: Identify dynamic parameter value
The URL /blog/42 matches the dynamic route [post].astro, so Astro.params.post is '42'.
Step 2: Render the parameter in the template
The component outputs <h1>Post ID: {postId}</h1>, so it shows Post ID: 42.
Final Answer:
<h1>Post ID: 42</h1> -> Option A
Quick Check:
URL segment fills param = rendered value [OK]
Hint: URL segment replaces [param] and shows in Astro.params [OK]
Common Mistakes:
Expecting literal 'post' instead of parameter value
Assuming empty or missing parameter
Confusing 404 with dynamic route handling
4. You have a dynamic route file named [user].astro but when visiting /john you get a runtime error: Cannot read property 'user' of undefined. What is the likely cause?
medium
A. Astro.params is only available in server components
B. You forgot to use Astro.params.user and accessed Astro.params directly
C. Dynamic routes cannot have parameters named 'user'
D. The filename should be {user}.astro instead of [user].astro
Solution
Step 1: Understand the error message
The error says Cannot read property 'user' of undefined, meaning Astro.params is undefined or not accessed properly.
Step 2: Check parameter access syntax
Accessing Astro.params.user is correct. If code tries to access Astro.params.user but Astro.params is undefined, it means Astro.params was not used or imported correctly.
Final Answer:
You forgot to use Astro.params.user and accessed Astro.params directly -> Option B
Quick Check:
Always use Astro.params.[param] to avoid undefined errors [OK]
Hint: Always access parameters via Astro.params.[name] [OK]
Common Mistakes:
Using curly braces instead of square brackets in filename
Assuming Astro.params is always defined globally
Misnaming the parameter in code
5. You want to create a nested dynamic route in Astro to handle URLs like /products/electronics/123. Which file structure and parameter access is correct?
hard
A. Create src/pages/products/[category]/id.astro and access with Astro.params.id
B. Create src/pages/products/[category-id].astro and access with Astro.params['category-id']
C. Create src/pages/products/category/id.astro and access with Astro.params.category and Astro.params.id
D. Create src/pages/products/[category]/[id].astro and access with Astro.params.category and Astro.params.id
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 is src/pages/products/[category]/[id].astro. Inside, access parameters as Astro.params.category and Astro.params.id.
Final Answer:
Create src/pages/products/[category]/[id].astro and access with Astro.params.category and Astro.params.id -> Option D
Quick Check:
Nested folders with [param] = nested params [OK]
Hint: Use nested folders with [param] for multi-level dynamic routes [OK]
Common Mistakes:
Combining multiple params in one filename incorrectly
Using static folder names instead of dynamic
Accessing parameters that don't exist in the route
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.