how to use param matcher in Sveltekit

Svelte

12/22/2022, 8:44:27 PM

by Leo Voon

For example, let’s say we have a dynamic route ends with post/[id]

we want the id to be integer only,

  1. Rename the route directory from [id] to [id=integer]
  2. Go to src folder, create a directory called params, and create a file named integer in it.
  3. And here is where you implement your matcher.
// src/params/integer.ts

import type { ParamMatcher } from '@sveltejs/kit'

export const match: ParamMatcher = (param) => {
  return /^[0-9]+$/.test(param)
}