gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.02k stars 1.62k forks source link

🐛 [Bug]: Middleware Executes Twice When app.Static is Placed Before Any Middleware #3080

Open vnxx opened 1 month ago

vnxx commented 1 month ago

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.

    app.Static("/", "./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")
    })

However, if app.Static is placed after the first middleware, the middleware functions execute as expected.

    app.Use(func(c *fiber.Ctx) error {
        fmt.Println("called")
        return c.Next()
    })

    app.Static("/", "./public")

    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")
    })

How to Reproduce

Steps to reproduce the behavior:

  1. Run Code Snippet
  2. See the log

Expected Behavior

The middleware functions should execute once.

Fiber Version

v2.52.5

Code Snippet (optional)

package main

import (
    "fmt"

    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    app.Static("/", "./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")
}

Checklist:

welcome[bot] commented 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

ReneWerner87 commented 1 month ago

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

ReneWerner87 commented 1 month ago
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")
}

image

it is fixed with v3

vnxx commented 1 month ago

Thanks! It might be a good idea to mention this issue in the docs for v2