1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 02:44:08 +00:00
fiber/middleware/etag/README.md

89 lines
2.0 KiB
Markdown
Raw Normal View History

2020-11-21 12:36:16 -05:00
# ETag Middleware
2020-10-15 12:32:54 +08:00
ETag middleware for [Fiber](https://github.com/gofiber/fiber) that lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content has not changed.
2020-11-21 12:36:16 -05:00
## Table of Contents
- [ETag Middleware](#etag-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Custom Config](#custom-config)
2020-11-21 12:36:16 -05:00
- [Config](#config)
- [Default Config](#default-config-2)
2020-10-15 12:32:54 +08:00
2020-11-21 12:36:16 -05:00
## Signatures
2020-10-15 12:32:54 +08:00
```go
func New(config ...Config) fiber.Handler
```
2020-11-21 12:36:16 -05:00
## Examples
Import the middleware package that is part of the Fiber web framework
2020-11-21 12:36:16 -05:00
2020-10-15 12:32:54 +08:00
```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
)
```
After you initiate your Fiber app, you can use the following possibilities:
2020-11-21 12:36:16 -05:00
### Default Config
2020-10-15 12:32:54 +08:00
```go
app.Use(etag.New())
// Get / receives Etag: "13-1831710635" in response header
app.Get("/", func(c fiber.Ctx) error {
2020-10-15 12:32:54 +08:00
return c.SendString("Hello, World!")
})
```
### Custom Config
2020-11-21 12:36:16 -05:00
```go
app.Use(etag.New(etag.Config{
Weak: true,
}))
2020-11-21 12:36:16 -05:00
// Get / receives Etag: "W/"13-1831710635" in response header
app.Get("/", func(c fiber.Ctx) error {
2020-11-21 12:36:16 -05:00
return c.SendString("Hello, World!")
})
```
## Config
2020-10-15 12:32:54 +08:00
```go
// Config defines the config for middleware.
type Config struct {
2020-11-16 14:22:44 +01:00
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c fiber.Ctx) bool
2020-11-16 14:22:44 +01:00
2020-10-15 12:32:54 +08:00
// Weak indicates that a weak validator is used. Weak etags are easy
// to generate, but are far less useful for comparisons. Strong
// validators are ideal for comparisons but can be very difficult
// to generate efficiently. Weak ETag values of two representations
// of the same resources might be semantically equivalent, but not
// byte-for-byte identical. This means weak etags prevent caching
// when byte range requests are used, but strong etags mean range
// requests can still be cached.
Weak bool
}
```
2020-11-21 12:36:16 -05:00
## Default Config
2020-10-15 12:32:54 +08:00
```go
var ConfigDefault = Config{
Next: nil,
2020-11-16 14:22:44 +01:00
Weak: false,
2020-10-15 12:32:54 +08:00
}
```