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

Plugin guide missing onRoute example #4282

Closed
2 tasks done
philsch opened this issue Sep 16, 2022 · 1 comment · Fixed by #4285
Closed
2 tasks done

Plugin guide missing onRoute example #4282

philsch opened this issue Sep 16, 2022 · 1 comment · Fixed by #4285
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@philsch
Copy link
Contributor

philsch commented Sep 16, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Thanks for the extensive Plugin Guide in your documentation.

I'm new to the Fastify ecosystem so please correct wrong assumptions. Imho a typical usecase for plugins is to extend preHandler logics for routes. You briefly touch this topic in your docs to register a hook just for a "subset of routes", but this only seems to fit for the routes that are introduced by the plugin itself (and the route logic belonging to my app should be at another place in the code structure). The way more powerful approach is combining plugins with the onRoute hook to attach logic when an application route is registered (for example like this plugin code https://github.com/Eomm/fastify-raw-body/blob/master/plugin.js#L29.

My questions:

  • are my assumptions correct and do you think extending the guide would be useful?
  • can you share your opinion about the best practise how to limit the plugin to only apply to certain routes
    • via a path filter in the plugin options?
    • via config property when defining a route?
    • ...?
@mcollina
Copy link
Member

are my assumptions correct and do you think extending the guide would be useful?

I think extending the guide to cover onRoute will be appreciated by everybody. It's a very powerful technique that we added after the guide was written.

can you share your opinion about the best practise how to limit the plugin to only apply to certain routes

via a path filter in the plugin options?

Unfortunately this will really be bad for performance because you'd need to match all incoming request and see if a hook applies.

via config property when defining a route?

This is the approach I recommend and it's used throughout the plugins in this organization.

@mcollina mcollina added documentation Improvements or additions to documentation good first issue Good for newcomers labels Sep 16, 2022
@Fdawgs Fdawgs linked a pull request Sep 20, 2022 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants