Performance: Dynamic routes with [param]
This affects page load speed and rendering performance by controlling how pages are generated and served based on URL parameters.
export async function getStaticPaths() { return { paths: [{ params: { param: 'a' } }, { params: { param: 'b' } }], fallback: false }; } export async function getStaticProps({ params }) { const data = await fetchData(params.param); return { props: { data } }; } export default function Page({ data }) { return <div>{data.content}</div>; }
export default function Page({ params }) { const [data, setData] = React.useState(null); React.useEffect(() => { fetch(`/api/data?param=${params.param}`) .then(res => res.json()) .then(data => setData(data)); }, [params.param]); if (!data) return <div>Loading...</div>; return <div>{data.content}</div>; } // No getStaticPaths or getServerSideProps used
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side data fetch in dynamic route | Minimal DOM nodes | 1 reflow after data loads | High paint cost due to delayed content | [X] Bad |
| Static generation with getStaticProps and getStaticPaths | Minimal DOM nodes | Single reflow on initial paint | Low paint cost, fast content display | [OK] Good |
[id].js inside the pages folder in Next.js represent?[id].js represent dynamic routes where the part inside brackets is a variable.[id].jsid, and the page will render accordingly.id is a variable part of the URL -> Option B[param] file name [OK]slug in a Next.js page component inside pages/[slug].js?useRouter hook to access route parameters via router.query.const slug = useRouter().query.slug; to get the dynamic slug value.pages/blog/[postId].js with this code:import { useRouter } from 'next/router';
export default function Post() {
const router = useRouter();
return <div>Post ID: {router.query.postId}</div>;
}/blog/42?[postId].js matches URLs like /blog/42 and sets postId to '42'.router.query.postId to display the dynamic value, so it will show '42'.pages/product/[productId].js. The component tries to access params.productId directly but gets an error. What is the likely cause?useRouter().query in client components, not directly from params.params.productId causes errorparams.productId without proper context or props causes undefined errors.useRouter().query.productId instead of params.productId -> Option A/user/123/profile. Which folder and file structure inside app directory correctly implements this using dynamic segments?[id]./user/123/profile to foldersuser/[id]/profile with a page.js inside.app/user/[id]/profile/page.js -> Option A[param] folder [OK]Pass the test and this pattern moves unknown