Skip to content

Commit b0327ab

Browse files
committed
fix: after handle example
1 parent 5b69f55 commit b0327ab

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

docs/life-cycle/after-handle.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ head:
1515
---
1616

1717
# After Handle
18+
1819
Execute after the main handler, for mapping a returned value of **before handle** and **route handler** into a proper response.
1920

2021
It's recommended to use After Handle in the following situations:
21-
- Transform requests into a new value, eg. Compression, Event Stream
22-
- Add custom headers based on the response value, eg. **Content-Type**
22+
23+
- Transform requests into a new value, eg. Compression, Event Stream
24+
- Add custom headers based on the response value, eg. **Content-Type**
2325

2426
## Example
27+
2528
Below is an example of using the after handle to add HTML content type to response headers.
2629

2730
```typescript
@@ -32,7 +35,7 @@ new Elysia()
3235
.get('/', () => '<h1>Hello World</h1>', {
3336
afterHandle({ response, set }) {
3437
if (isHtml(response))
35-
set.headers['Content-Type'] = 'text/html; charset=utf8'
38+
set.headers['content-type'] = 'text/html; charset=utf8'
3639
}
3740
})
3841
.get('/hi', () => '<h1>Hello World</h1>')
@@ -47,28 +50,34 @@ The response should be listed as follows:
4750
| /hi | text/plain; charset=utf8 |
4851

4952
## Returned Value
53+
5054
If a value is returned After Handle will use a return value as a new response value unless the value is **undefined**
5155

5256
The above example could be rewritten as the following:
57+
5358
```typescript
5459
import { Elysia } from 'elysia'
5560
import { isHtml } from '@elysiajs/html'
5661

5762
new Elysia()
5863
.get('/', () => '<h1>Hello World</h1>', {
5964
afterHandle({ response, set }) {
60-
if (isHtml(response))
65+
if (isHtml(response)) {
6166
set.headers['content-type'] = 'text/html; charset=utf8'
67+
return new Response(response)
68+
}
6269
}
6370
})
6471
.get('/hi', () => '<h1>Hello World</h1>')
6572
.listen(3000)
6673
```
6774

68-
Unlike **beforeHandle**, after a value is returned from **afterHandle**, the iteration of afterHandle **will __NOT__ be skipped.**
75+
Unlike **beforeHandle**, after a value is returned from **afterHandle**, the iteration of afterHandle **will **NOT** be skipped.**
6976

7077
## Context
78+
7179
`onAfterHandle` Context is extends from `Context` with additional properties of the following:
72-
- response: Response to return to the client
80+
81+
- response: Response to return to the client
7382

7483
All of the context is based on normal context and can be used like normal context in route handler.

0 commit comments

Comments
 (0)