Svelte / Advanced: rendering, resources and release evidence / Lesson 21
Route parameter matchers and trust boundaries
Constrain SvelteKit route parameters and distinguish route matching from record authorization.
On this page
Lesson goals
- Define a route parameter matcher.
- Trace matched values into a load function.
- Test unmatched paths and server authorization.
Choose a bounded route
A dynamic segment can accept many strings, including values that do not belong to a small route vocabulary. A parameter matcher limits which routes match and can return the parsed value for the route. Use this for routing shape, not as proof that a user may access a record. Verify the API against the installed SvelteKit version because matcher configuration differs across major versions.
Define the matcher
This SvelteKit 3 example declares a topic matcher in src/params.js. It accepts two public labels and returns undefined for other strings. The definition is intentionally small and has no network or user state. A compatible project and router are required; this file alone does not create a page. Do not read secrets or perform authorization inside a shared route matcher.
import { defineParams } from "@sveltejs/kit/params";
export const params = defineParams({
topic: (value) => {
if (value === "basics" || value === "practice") return value;
return undefined;
}
});
basics matches; unknown returns undefined.
The matcher returns either an accepted topic or undefined when the value does not match.
Use the matched value
Place this load function in src/routes/topics/[topic=topic]/+page.js. The folder associates the topic parameter with the matcher of the same name. The function returns the matched value for a page component to display; a separate page component is still needed. If matching fails, SvelteKit may try another matching route before returning a not-found response.
export function load({ params }) {
return { topic: params.topic };
}
For /topics/basics, load returns topic basics.
The load function forwards the matched parameter as public page data.
Separate shape from authority
A valid identifier or label does not establish that the underlying record exists or belongs to the current user. Validate the actual data and authorize the specific operation at the server boundary. Avoid logging raw sensitive input while tracing rejected routes. If routing syntax or major versions change, follow the corresponding migration guide rather than combining incompatible matcher definitions.
Verify route behavior
Test both accepted labels, an unlisted value and a malformed path in a controlled project. Check whether another route can match an excluded value, then verify the expected not-found behavior when no route does. Test an unauthorized direct request separately from route matching. Run a production build for the intended adapter before treating a development result as deployment evidence.
Common errors and fixes
A route never matches an expected label.
- Why it happens
- The matcher name or returned value does not match the route contract.
- How to fix it
- Align the route segment and parameter definition.
- Prevent it next time
- Test each accepted label directly.
An invalid path reaches another page.
- Why it happens
- Another route matches after this matcher rejects it.
- How to fix it
- Review the complete route tree and intended fallback.
- Prevent it next time
- Test rejected values against all competing routes.
A valid parameter exposes another userโs data.
- Why it happens
- Routing validation was mistaken for authorization.
- How to fix it
- Enforce object-level access at the server operation.
- Prevent it next time
- Test direct requests from two synthetic accounts.
2 practice exercises
1. What happens when the topic matcher returns undefined?
Show worked solution
That route does not match; SvelteKit can try another route and eventually returns not found if none matches. Verify the actual route tree.
2. Does an accepted topic authorize access to a private record?
Show worked solution
No. The matcher constrains route shape. Authenticate and authorize the requested record separately on the server.
Key takeaways
- A matcher controls route shape, not authority.
- Version-specific matcher configuration matters.
- Test accepted and rejected paths against the full route tree.