Performance: Dynamic route segments
This affects the page load speed and server response time by determining how routes are matched and pages are rendered dynamically.
Jump into concepts and practice - no test required
export async function getStaticPaths() { const res = await fetch('https://api.example.com/users'); const users = await res.json(); const paths = users.map(user => ({ params: { id: user.id.toString() } })); return { paths, fallback: 'blocking' }; } export async function getStaticProps({ params }) { const res = await fetch(`https://api.example.com/users/${params.id}`); const user = await res.json(); return { props: { user }, revalidate: 60 }; } export default function UserPage({ user }) { return <div>{user.name}</div>; }
export async function getServerSideProps(context) { const { id } = context.params; const res = await fetch(`https://api.example.com/users/${id}`); const user = await res.json(); return { props: { user } }; } export default function UserPage({ user }) { return <div>{user.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Server-side rendering on every request | Minimal DOM nodes | 1 reflow per page load | High paint cost due to blocking | [X] Bad |
| Static generation with fallback blocking | Minimal DOM nodes | 1 reflow per page load | Low paint cost, fast rendering | [OK] Good |
[id].js inside the pages folder represent?pages folder define routes. Square brackets [] indicate dynamic segments.[id].js meaning[id].js matches any URL segment in that position and passes it as a parameter named id.pages folder.[username].js to capture the username dynamically.pages/blog/[slug].js/blog/hello-world, what will be the value of the slug parameter inside the page component?[slug].js captures the URL segment after /blog/ as slug./blog/hello-world, the segment hello-world is assigned to slug.pages/product/[id].js. Which of the following code snippets correctly accesses the id parameter inside the component?useRouter hook from next/router to access query parameters.idconst router = useRouter(); const { id } = router.query;./dashboard/user/123 where 123 is a user ID. Which file structure correctly implements this?/dashboard/user/123 has three segments: dashboard, user, and a dynamic id.dashboard and user are folders, and [id].js captures the dynamic user ID.user folder.