Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch-all route #1064

Closed
zanmato opened this issue Oct 25, 2024 · 4 comments
Closed

Catch-all route #1064

zanmato opened this issue Oct 25, 2024 · 4 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@zanmato
Copy link

zanmato commented Oct 25, 2024

💬 Question here

Is it possible to add a catch-all route like: https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes#catch-all-segments

E.g. /categories/[...slug] would match /categories/1 and /categories/1/2

Your Environment

fastify/vite & fastify/vue

@zanmato zanmato added the help wanted Extra attention is needed label Oct 25, 2024
@dosubot dosubot bot added the question Further information is requested label Oct 25, 2024
@metcoder95
Copy link
Member

You can use a wildcard for it (e.g. /categories/*).

Depending on your setup, you might need to set it after other routes that share the same resource.

Just be cautious that this kind of wildcard routes (or ones that comes out from a Regex) are expensive in terms of performance

@zanmato
Copy link
Author

zanmato commented Oct 30, 2024

Where would I put that, let's say in the context of the vue-kitchensink example? I tried altering the param regex to allow wildcards, e.g. naming a file [*].vue. But it doesn't seem to work, perhaps because the vue router expects /:slug* and fastify just /*?

@metcoder95
Copy link
Member

Sorry, for that I'm clueless; not really sure how they work 🙁

@zanmato
Copy link
Author

zanmato commented Oct 31, 2024

For a workaround I changed the param pattern to allow + at the end

viteFastifyVue({ paramPattern: /\[([\w]+\+?)\]/ }),

I named my file [slug+].vue which results in a /:slug+ route which is compatible with vue-router, and added a hook to replace it with a wildcard before it is added to the fastify router:

server.addHook("onRoute", (route) => {
  route.url = route.url.replace(/\:[^\+]+\+/, "*");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants