Yulian
Yulian

Reputation: 6769

Can you apply a regular expression for a param in Svelte's Routify?

I currently have a route which I have configured to be (foo|bar). So now my child route of example accepts the following combinations:

/foo/example
/bar/example

However, when I navigate to my example page, $page.path is /(foo|bar)/example so I'm not able to tell if the first param is foo or bar unless I check location.pathname.

On the other hand, if I rename my directory to be called [myParam] instead of (foo|bar), all types of strings would match this route, like:

/i-dont-want-that/example
/or-that/example

So what directory name should I use to create a param called myParam that satisfies the regular expression (foo|bar)?

Upvotes: 0

Views: 146

Answers (1)

JakeDK
JakeDK

Reputation: 1246

There's no way native way to do this in Routify2, but you could do something like this.

<!-- root _layout.svelte -->
<script>
  import { params, goto } from '@roxi/routify'
  if (!['foo', 'bar'].includes($params.myParam)
    $goto('/404')
</script>

It's worth noting, static paths always take precedence over dynamic paths, so you don't have to worry about conflicting paths.

Upvotes: 1

Related Questions