Skip to content

Commit

Permalink
docs: update onRoute hook docs (#4322)
Browse files Browse the repository at this point in the history
* update docs

* fix lint error
  • Loading branch information
matthyk committed Oct 6, 2022
1 parent 62d2c94 commit da7471f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/Reference/Hooks.md
Expand Up @@ -449,6 +449,33 @@ fastify.addHook('onRoute', (routeOptions) => {
})
```

If you want to add more routes within an onRoute hook, you have to tag these
routes properly. If you don't, the hook will run into an infinite loop. The
recommended approach is shown below.

```js
const kRouteAlreadyProcessed = Symbol('route-already-processed')

fastify.addHook('onRoute', function (routeOptions) {
const { url, method } = routeOptions

const isAlreadyProcessed = (routeOptions.custom && routeOptions.custom[kRouteAlreadyProcessed]) || false

if (!isAlreadyProcessed) {
this.route({
url,
method,
custom: {
[kRouteAlreadyProcessed]: true
},
handler: () => {}
})
}
})
```

For more details, see this [issue](https://github.com/fastify/fastify/issues/4319).

### onRegister
<a id="on-register"></a>

Expand Down

0 comments on commit da7471f

Please sign in to comment.