Dynamic routes let your website show different pages based on the URL. This means you can create one page that changes depending on what the user clicks or types.
Dynamic routes with parameters in Astro
src/pages/[param].astro
---
const { params } = Astro;
const value = params.param;
---
<h1>Value is: {value}</h1>Use square brackets [param] in the filename to mark a dynamic part of the URL.
Access the parameter inside the component with Astro.params.param.
/john or /alice.src/pages/[username].astro
---
const { params } = Astro;
const username = params.username;
---
<h1>Welcome, {username}!</h1>/blog/my-first-post.src/pages/blog/[slug].astro
---
const { params } = Astro;
const slug = params.slug;
---
<h1>Blog post: {slug}</h1>This component creates a product page that changes based on the product ID in the URL. For example, visiting /product/123 will show "You are viewing product with ID: 123".
src/pages/product/[id].astro
---
const { params } = Astro;
const productId = params.id;
---
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Product {productId}</title>
</head>
<body>
<main>
<h1>Product Page</h1>
<p>You are viewing product with ID: {productId}</p>
</main>
</body>
</html>Dynamic route files must be inside the src/pages folder.
Parameter names in brackets should be simple and descriptive.
You can have multiple parameters like [category]/[id].astro for nested dynamic routes.
Dynamic routes let you create flexible pages that change based on the URL.
Use square brackets in filenames to define parameters.
Access parameters inside your component with Astro.params.
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
