Open vnxx opened 1 month ago
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
the cause is the current design, as your api function and the function for the static files are on the same route
since we wanted to make it possible to use the middlewares after executing the static delivery, we also executed a next there, which probably leads to this effect
if necessary, you could see if you could implement a different behavior for mismatch
this design has been reworked for version 3 and should work better there
@efectn can you verify this briefly
package main
import (
"fmt"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
)
func main() {
app := fiber.New()
app.Use("/", static.New("./public"))
app.Use(func(c fiber.Ctx) error {
fmt.Println("called")
return c.Next()
})
app.Use(func(c fiber.Ctx) error {
fmt.Println("called 2")
return c.Next()
})
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello World")
})
app.Use(func(c fiber.Ctx) error {
return c.SendString("Not Found 404")
})
app.Listen(":3000")
}
it is fixed with v3
Thanks! It might be a good idea to mention this issue in the docs for v2
Bug Description
When placing app.Static before any app.Use middleware, the middleware functions execute twice. This causes unexpected behavior and redundancy in middleware execution.
However, if app.Static is placed after the first middleware, the middleware functions execute as expected.
How to Reproduce
Steps to reproduce the behavior:
Expected Behavior
The middleware functions should execute once.
Fiber Version
v2.52.5
Code Snippet (optional)
Checklist: