N
Nuxt4mo ago
Artist201

Optional Route parameters ?

can you not add optional route parameters? something like the picture ? as I want to handle the same route but sometimes with parameters and sometimes without
No description
6 Replies
Artist201
Artist201OP4mo ago
so i want this /users and /users/xyz to be routed through the same handler
Cue
Cue4mo ago
Not through file based routing alone, no. You'll need both [slug].get.ts to handle with params, and index.get.ts to handle without. You'll want to create a util that either acts as a wrapper (see https://nuxt.com/docs/guide/directory-structure/server#server-routes) for re-use or a method that executes shared logic and define a handler for both routes executing said util.
Artist201
Artist201OP4mo ago
am not sure how to do that from the given page can i some how just redirect to another handler ? so i make the index.get.ts and [slug].get.ts and make the index redirect to that
Artist201
Artist201OP4mo ago
maybe something like this ?
No description
Cue
Cue4mo ago
Exactly like that. You wouldn’t even need to import, auto imports! In your shared handler, you can use getRouterParam(event, 'slug') which will be undefined in its absence thus you’ll treat it as the index. So on…
Artist201
Artist201OP4mo ago
just sharing that it is working great. Ty for your support
Want results from more Discord servers?
Add your server