Skip to content

Commit

Permalink
docs: improved migration guide for onRoute (#4233)
Browse files Browse the repository at this point in the history
* docs: improved migration guide.

* docs: have consistent examples.
  • Loading branch information
ShogunPanda committed Aug 30, 2022
1 parent e2165c7 commit 67bee11
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions docs/Guides/Migration-Guide-V4.md
Expand Up @@ -71,28 +71,60 @@ The route registration has been made synchronous from v4.
This change was done to provide better error reporting for route definition.
As a result if you specify an `onRoute` hook in a plugin you should either:
* wrap your routes in a plugin (recommended)

For example refactor this:

```
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
done();
});
});
fastify.get('/', () => 'hello');
```

Into this:

```
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
done();
});
});
fastify.register((instance, opts, done) => {
instance.get('/', () => 'hello');
done();
});
```

* use `await register(...)`

For example refactor this:
```
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
For example refactor this:
```
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
});
done();
});
done();
});
```
Into this:
```
await fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
```
Into this:
```
await fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
});
done();
});
done();
});
```
```

## Non Breaking Changes

Expand Down

0 comments on commit 67bee11

Please sign in to comment.