Performance: Dynamic routes with parameters
This affects the page load speed and server response time by determining how routes are matched and content is generated dynamically.
src/pages/[slug].astro --- export async function getStaticPaths() { const res = await fetch('https://api.example.com/all-slugs'); const slugs = await res.json(); return slugs.map(slug => ({ params: { slug } })); } const { slug } = Astro.params; const data = await fetch(`https://api.example.com/data/${slug}`); const json = await data.json(); --- <html><body><h1>{json.title}</h1></body></html>
src/pages/[slug].astro
---
const { slug } = Astro.params;
const data = await fetch(`https://api.example.com/data/${slug}`);
const json = await data.json();
---
<html><body><h1>{json.title}</h1></body></html>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Runtime data fetching in dynamic route | Minimal DOM nodes | 0 reflows | Blocks paint until data arrives | [X] Bad |
| Static generation with pre-built dynamic routes | Minimal DOM nodes | 0 reflows | Paints immediately with preloaded data | [OK] Good |
[id].astro represent in Astro?[id].astro define a dynamic route parameter named id.id parameter from the URL -> Option Aslug inside an Astro component?Astro.params.slug, use Astro.params.slug.Astro.params.slug -> Option Csrc/pages/blog/[post].astro with this code snippet:--- const postId = Astro.params.post; ---Post ID: {postId}
/blog/42?/blog/42 matches the dynamic route [post].astro, so Astro.params.post is '42'.<h1>Post ID: {postId}</h1>, so it shows Post ID: 42.[user].astro but when visiting /john you get a runtime error: Cannot read property 'user' of undefined. What is the likely cause?Cannot read property 'user' of undefined, meaning Astro.params is undefined or not accessed properly.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.Astro.params.user and accessed Astro.params directly -> Option B/products/electronics/123. Which file structure and parameter access is correct?/products/electronics/123, you need nested folders with dynamic parameters: [category] and [id].src/pages/products/[category]/[id].astro. Inside, access parameters as Astro.params.category and Astro.params.id.src/pages/products/[category]/[id].astro and access with Astro.params.category and Astro.params.id -> Option DPass the test and this pattern moves unknown