You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Joins the links followed by the property to populate the responseβs [Link](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) HTTP header field.
1844
+
Joins the links followed by the property to populate the responseβs [Link HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) field.
1845
1845
1846
1846
```go title="Signature"
1847
1847
func(cfiber.Ctx) Links(link ...string)
@@ -2040,7 +2040,7 @@ For sending multiple files from an embedded file system, [this functionality](..
2040
2040
Sets the status code and the correct status message in the body if the response body is **empty**.
2041
2041
2042
2042
:::tip
2043
-
You can find all used status codes and messages [here](https://github.com/gofiber/fiber/blob/dffab20bcdf4f3597d2c74633a7705a517d2c8c2/utils.go#L183-L244).
2043
+
You can find all used status codes and messages [in the Fiber source code](https://github.com/gofiber/fiber/blob/dffab20bcdf4f3597d2c74633a7705a517d2c8c2/utils.go#L183-L244).
Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) HTTP header to the MIME type listed [here](https://github.com/nginx/nginx/blob/master/conf/mime.types) specified by the file **extension**.
2197
+
Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) HTTP header to the MIME type listed [in the Nginx MIME types configuration](https://github.com/nginx/nginx/blob/master/conf/mime.types) specified by the file **extension**.
Copy file name to clipboardExpand all lines: docs/middleware/compress.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ id: compress
7
7
Compression middleware for [Fiber](https://github.com/gofiber/fiber) that will compress the response using `gzip`, `deflate`, `brotli`, and `zstd` compression depending on the [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header.
8
8
9
9
:::note
10
-
The compression middleware refrains from compressing bodies that are smaller than 200 bytes. This decision is based on the observation that, in such cases, the compressed size is likely to exceed the original size, making compression inefficient. [more](https://github.com/valyala/fasthttp/blob/497922a21ef4b314f393887e9c6147b8c3e3eda4/http.go#L1713-L1715)
10
+
The compression middleware refrains from compressing bodies that are smaller than 200 bytes. This decision is based on the observation that, for small bodies, the compressed size is likely to exceed the original size, making compression inefficient and consuming unnecessary CPU time. [More details in fasthttp source](https://github.com/valyala/fasthttp/blob/497922a21ef4b314f393887e9c6147b8c3e3eda4/http.go#L1713-L1715).
Can be used for middleware packages and prefix catchers. These routes will only match the beginning of each path i.e. `/john` will match `/john/doe`, `/johnnnnn` etc
### Important Change for Accessing Middleware Data
825
+
826
+
In Fiber v3, many middlewares that previously set values in `c.Locals()` using string keys (e.g., `c.Locals("requestid")`) have been updated. To align with Go's context best practices and prevent key collisions, these middlewares now store their specific data in the request's context using unexported keys of custom types.
827
+
828
+
This means that directly accessing these values via `c.Locals("some_string_key")` will no longer work for such middleware-provided data.
829
+
830
+
**How to Access Middleware Data in v3:**
831
+
832
+
Each affected middleware now provides dedicated exported functions to retrieve its specific data from the context. You should use these functions instead of relying on string-based lookups in `c.Locals()`.
833
+
834
+
Examples include:
835
+
836
+
-`requestid.FromContext(c)`
837
+
-`csrf.TokenFromContext(c)`
838
+
-`csrf.HandlerFromContext(c)`
839
+
-`session.FromContext(c)`
840
+
-`basicauth.UsernameFromContext(c)`
841
+
-`basicauth.PasswordFromContext(c)`
842
+
-`keyauth.TokenFromContext(c)`
843
+
844
+
When used with the Logger middleware, the recommended approach is to use the `CustomTags` feature of the logger, which allows you to call these specific `FromContext` functions. See the [Logger](#logger) section for more details.
845
+
819
846
### Adaptor
820
847
821
848
The adaptor middleware has been significantly optimized for performance and efficiency. Key improvements include reduced response times, lower memory usage, and fewer memory allocations. These changes make the middleware more reliable and capable of handling higher loads effectively. Enhancements include the introduction of a `sync.Pool` for managing `fasthttp.RequestCtx` instances and better HTTP request and response handling between net/http and fasthttp contexts.
@@ -933,6 +960,79 @@ func main() {
933
960
934
961
</details>
935
962
963
+
#### Logging Middleware Values (e.g., Request ID)
964
+
965
+
In Fiber v3, middleware (like `requestid`) now stores values in the request context using unexported keys of custom types. This aligns with Go's context best practices to prevent key collisions between packages.
966
+
967
+
As a result, directly accessing these values using string keys with `c.Locals("your_key")` or in the logger format string with `${locals:your_key}` (e.g., `${locals:requestid}`) will no longer work for values set by such middleware.
968
+
969
+
**Recommended Solution: `CustomTags`**
970
+
971
+
The cleanest and most maintainable way to include these middleware-specific values in your logs is by using the `CustomTags` option in the logger middleware configuration. This allows you to define a custom function to retrieve the value correctly from the context.
972
+
973
+
<details>
974
+
<summary>Example: Logging Request ID with CustomTags</summary>
If you have existing logging patterns that rely on `c.Locals` or prefer to manage these values in `Locals` for other reasons, you can manually copy the value from the context to `c.Locals` in a preceding middleware:
1015
+
1016
+
<details>
1017
+
<summary>Example: Manually setting requestid in Locals</summary>
1018
+
1019
+
```go
1020
+
app.Use(requestid.New()) // Request ID middleware
1021
+
app.Use(func(c fiber.Ctx) error {
1022
+
// Manually copy the request ID to Locals
1023
+
c.Locals("requestid", requestid.FromContext(c))
1024
+
return c.Next()
1025
+
})
1026
+
app.Use(logger.New(logger.Config{
1027
+
// Now ${locals:requestid} can be used, but CustomTags is generally preferred
Both approaches ensure your logger can access these values while respecting Go's context practices.
1035
+
936
1036
The `Skip` is a function to determine if logging is skipped or written to `Stream`.
937
1037
938
1038
<details>
@@ -1060,10 +1160,16 @@ func main() {
1060
1160
-[π App](#-app-1)
1061
1161
-[πΊ Router](#-router-1)
1062
1162
-[π§ Context](#-context-1)
1063
-
-[π Parser](#-parser)
1163
+
-[π Binding (was Parser)](#-parser)
1064
1164
-[π Redirect](#-redirect-1)
1065
1165
-[π Client package](#-client-package-1)
1066
1166
-[𧬠Middlewares](#-middlewares-1)
1167
+
-[Important Change for Accessing Middleware Data](#important-change-for-accessing-middleware-data)
1168
+
-[CORS](#cors-1)
1169
+
-[CSRF](#csrf)
1170
+
-[Filesystem](#filesystem-1)
1171
+
-[Healthcheck](#healthcheck-1)
1172
+
-[Monitor](#monitor-1)
1067
1173
1068
1174
### π App
1069
1175
@@ -1497,6 +1603,32 @@ DRAFT section
1497
1603
1498
1604
### 𧬠Middlewares
1499
1605
1606
+
#### ImportantChangeforAccessingMiddlewareData
1607
+
1608
+
**Change:** InFiber v2, some middlewares set data in `c.Locals()` using stringkeys (e.g., `c.Locals("requestid")`). InFiber v3, to align with Go's context best practices and prevent key collisions, these middlewares now store their specific data in the request's context using unexported keys of custom types.
1609
+
1610
+
**Impact:** Directly accessing these middleware-provided values via `c.Locals("some_string_key")` will no longer work.
1611
+
1612
+
**Migration Action:**
1613
+
You must update your code to use the dedicated exported functions provided by each affected middleware to retrieve its data from the context.
1614
+
1615
+
**Examples of new helper functions to use:**
1616
+
1617
+
- `requestid.FromContext(c)`
1618
+
- `csrf.TokenFromContext(c)`
1619
+
- `csrf.HandlerFromContext(c)`
1620
+
- `session.FromContext(c)`
1621
+
- `basicauth.UsernameFromContext(c)`
1622
+
- `basicauth.PasswordFromContext(c)`
1623
+
- `keyauth.TokenFromContext(c)`
1624
+
1625
+
**For logging these values:**
1626
+
The recommended approach is to use the `CustomTags` feature of the Logger middleware, which allows you to call these specific `FromContext` functions. Refer to the [Logger section in "What's New"](#logger) for detailed examples.
1627
+
1628
+
:::note
1629
+
If you were manually setting and retrieving your own application-specific values in `c.Locals()` using string keys, that functionality remains unchanged. This change specifically pertains to how Fiber's built-in (and some contrib) middlewares expose their data.
1630
+
:::
1631
+
1500
1632
#### CORS
1501
1633
1502
1634
The CORS middleware has been updated to use slices instead of strings for the `AllowOrigins`, `AllowMethods`, `AllowHeaders`, and `ExposeHeaders` fields. Here's how you can update your code:
0 commit comments