Skip to content

Commit cea9e9c

Browse files
Fennygitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 7894b9a commit cea9e9c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

context.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,60 @@ app.Get("/", func(c *fiber.Ctx) {
221221
})
222222
```
223223

224+
## Compress
225+
226+
Fiber supports global compression using GZip / Deflate using the [Compression Setting](application.md#settings). But with the `c.Compress()` method you can enable / disable compression within handlers.
227+
228+
**Signature**
229+
230+
```go
231+
c.Compress(enable ...bool)
232+
```
233+
234+
**Example**
235+
236+
```go
237+
// Example 1
238+
func main() {
239+
app := fiber.New(&fiber.Settings{
240+
Compression: true, // global compression enabled
241+
})
242+
app.Get("/", func(c *fiber.Ctx) {
243+
c.Send("Hello, World!") // compressed
244+
})
245+
app.Get("/demo", func(c *fiber.Ctx) {
246+
c.Compress(false)
247+
c.Send("hello, World!") // not compressed
248+
})
249+
}
250+
// Example 2
251+
func main() {
252+
app := fiber.New() // compression disabled by default
253+
app.Get("/", func(c *fiber.Ctx) {
254+
c.Compress()
255+
c.Send("Hello, World!") // compressed
256+
})
257+
app.Get("/demo", func(c *fiber.Ctx) {
258+
c.Send("hello, World!") // not compressed
259+
})
260+
}
261+
// Example 3
262+
func gzip(c *fiber.Ctx) {
263+
c.Compress()
264+
c.Next()
265+
}
266+
func main() {
267+
app := fiber.New() // compression disabled by default
268+
app.Get("/", gzip, func(c *fiber.Ctx) {
269+
c.Send("Hello, World!") // compressed
270+
})
271+
app.Get("/demo", func(c *fiber.Ctx) {
272+
c.Send("hello, World!") // not compressed
273+
})
274+
}
275+
```
276+
```
277+
224278
## Cookie
225279
226280
Set cookies

0 commit comments

Comments
 (0)