Performance: Dynamic route parameters
Dynamic route parameters affect page load speed by determining which content to fetch and render based on the URL, impacting initial rendering and navigation responsiveness.
export async function load({ params }) { const id = params.id; const item = await fetch(`/api/items/${id}`).then(res => res.json()); return { item }; }
export async function load({ params }) { const id = params.id; const data = await fetch('/api/items').then(res => res.json()); return { item: data.find(i => i.id === id) }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetch all data then filter | High (many nodes if rendering all) | Multiple reflows as content updates | High paint cost due to large DOM | [X] Bad |
| Fetch only needed data by parameter | Low (only relevant nodes) | Single reflow on content insert | Low paint cost with small DOM | [OK] Good |
[id].svelte represent in a SvelteKit project?[id].svelte/123 or /abc, where id changes dynamically.id is a variable part of the URL -> Option Aslug inside a SvelteKit page component?params prop object.export let params; to receive it, then access params.slug.export let params; in the script -> Option Aexport let params; receives route params [OK]export let slug; (wrong prop name)src/routes/blog/[postId].svelte with this code: <script>
export let params;
</script>
<h1>Post ID: {params.postId}</h1>/blog/42?[postId].svelte file captures the URL segment after /blog/ as postId and passes it as params.postId.params is used in the component{params.postId} inside the heading, so it will display the actual value from the URL.[user].svelte but when you visit /john, you get an error: params is undefined. What is the likely cause?params which must be exported in the script.let params is not exported, the component cannot access it, causing params is undefined.let params in the script -> Option C/user/123/post/456. Which file structure and parameter access is correct?user/[userId]/post/[postId].svelte, and inside the component, you access params.userId and params.postId.src/routes/user/[userId]/post/[postId].svelte and access params.userId and params.postId -> Option DPass the test and this pattern moves unknown