Skip to content

Commit

Permalink
Update v4 migration guide and route docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mateus4k committed Apr 14, 2023
1 parent 662706b commit eaac1b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/Guides/Migration-Guide-V4.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ As a result, if you specify an `onRoute` hook in a plugin you should now either:
});
```

### Optional URL parameters

If you've already used any implicitly optional parameters, you'll get a 404 error when trying to access the route. Now you will need to declare the optional parameters explicitly:

Check failure on line 135 in docs/Guides/Migration-Guide-V4.md

View workflow job for this annotation

GitHub Actions / Lint Markdown

Line length [Expected: 80; Actual: 179]

For example, if you have the same route for listing and showing a post, refactor this:

Check failure on line 137 in docs/Guides/Migration-Guide-V4.md

View workflow job for this annotation

GitHub Actions / Lint Markdown

Line length [Expected: 80; Actual: 86]
```js
fastify.get('/posts/:id', (request, reply) => {
const { id } = request.params;
});
```

Into this:
```js
fastify.get('/posts/:id?', (request, reply) => {
const { id } = request.params;
});
```

## Non-Breaking Changes

### Deprecation of variadic `.listen()` signature
Expand Down
9 changes: 9 additions & 0 deletions docs/Reference/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ fastify.get('/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', function (request, r
In this case as parameter separator it is possible to use whatever character is
not matched by the regular expression.

The last parameter can be made optional if you add a question mark ("?") at the end of the parameters name.

Check failure on line 293 in docs/Reference/Routes.md

View workflow job for this annotation

GitHub Actions / Lint Markdown

Line length [Expected: 80; Actual: 107]
```js
fastify.get('/example/posts/:id?', function (request, reply) {
const { id } = request.params;
// your code here
})
```
In this case you can request `/example/posts` as well as `/example/posts/1`. The optional param will be undefined if not specified.

Check failure on line 300 in docs/Reference/Routes.md

View workflow job for this annotation

GitHub Actions / Lint Markdown

Line length [Expected: 80; Actual: 131]

Having a route with multiple parameters may negatively affect performance, so
prefer a single parameter approach whenever possible, especially on routes that
are on the hot path of your application. If you are interested in how we handle
Expand Down

0 comments on commit eaac1b5

Please sign in to comment.