Dynamic route parameters let your app show different pages based on parts of the URL. This helps create pages that change depending on what the user clicks or types.
Dynamic route parameters in Svelte
/src/routes/[parameter]/+page.svelte
The square brackets [] around a name in the filename tell SvelteKit this part is dynamic.
The parameter name inside brackets becomes a variable you can use in your component.
/123 or /abc. The id is dynamic and changes based on the URL./src/routes/[id]/+page.svelte
params. Here, params.id shows the value from the URL.<script> export let params; </script> <h1>User ID: {params.id}</h1>
slug as the dynamic parameter, useful for blog post URLs like /blog/my-first-post./src/routes/blog/[slug]/+page.svelte
This SvelteKit component shows a product page. The URL should look like /product/123, where 123 is the product ID. The component reads this ID from params.productId and displays it.
<script> export let params; </script> <h1>Product Page</h1> <p>Product ID: {params.productId}</p>
You must export params to access dynamic route values.
Dynamic routes can be nested, like /blog/[slug]/comments/+page.svelte.
Use descriptive parameter names to keep your code clear.
Dynamic route parameters let you create flexible pages that change based on the URL.
Use square brackets in filenames to define dynamic parts.
Access the dynamic value inside your component via the params object.
Practice
[id].svelte represent in a SvelteKit project?Solution
Step 1: Understand filename brackets in SvelteKit
Square brackets in filenames define dynamic segments in routes, meaning the part inside brackets is a variable.Step 2: Interpret
This means the page matches URLs like[id].svelte/123or/abc, whereidchanges dynamically.Final Answer:
A dynamic route whereidis a variable part of the URL -> Option AQuick Check:
Filename with brackets = dynamic route [OK]
- Thinking bracket means static filename
- Confusing dynamic route with component name
- Assuming it is for styles or scripts
slug inside a SvelteKit page component?Solution
Step 1: Recall how SvelteKit passes route params
Dynamic route parameters are passed to the page component as aparamsprop object.Step 2: Declare the prop in the script
Inside the page component (+page.svelte) script, useexport let params;to receive it, then accessparams.slug.Final Answer:
Usingexport let params;in the script -> Option AQuick Check:
export let params;receives route params [OK]
- Using
export let slug;(wrong prop name) - Accessing params from window or this
- Confusing with load function params
src/routes/blog/[postId].svelte with this code: <script>
export let params;
</script>
<h1>Post ID: {params.postId}</h1>What will be rendered when visiting
/blog/42?Solution
Step 1: Understand dynamic route parameter passing
The[postId].sveltefile captures the URL segment after /blog/ aspostIdand passes it asparams.postId.Step 2: Check how
The component usesparamsis used in the component{params.postId}inside the heading, so it will display the actual value from the URL.Final Answer:
<h1>Post ID: 42</h1> -> Option BQuick Check:
Dynamic param value shown = 42 [OK]
- Expecting literal string 'postId' instead of value
- Forgetting to export params
- Using params without export or load
[user].svelte but when you visit /john, you get an error: params is undefined. What is the likely cause?Solution
Step 1: Check how params are accessed in SvelteKit
Dynamic route parameters are passed as a prop namedparamswhich must be exported in the script.Step 2: Identify missing export
Iflet paramsis not exported, the component cannot access it, causingparams is undefined.Final Answer:
You forgot to exportlet paramsin the script -> Option CQuick Check:
Export params to access dynamic route data [OK]
- Using curly braces in filename instead of brackets
- Trying to access params from window object
- Assuming dynamic routes don't pass params
/user/123/post/456. Which file structure and parameter access is correct?Solution
Step 1: Understand nested dynamic routes in SvelteKit
To match URL segments like /user/123/post/456, you create nested folders with dynamic filenames for each variable part.Step 2: Confirm file structure and parameter names
The correct structure isuser/[userId]/post/[postId].svelte, and inside the component, you accessparams.userIdandparams.postId.Final Answer:
Files:src/routes/user/[userId]/post/[postId].svelteand accessparams.userIdandparams.postId-> Option DQuick Check:
Nested dynamic folders match URL segments [OK]
- Using wrong param names inside params object
- Missing nested folder for second dynamic segment
- Trying to access params not defined by filename
Ready to prove it?
Pass the test and this pattern moves unknown
