Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
3 tasks done
vnxx opened this issue Jul 21, 2024 · 4 comments
Open
3 tasks done

Comments

@vnxx
Copy link

vnxx commented Jul 21, 2024

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:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
Copy link

welcome bot commented Jul 21, 2024

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
Copy link
Member

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
Copy link
Member

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
Copy link
Author

vnxx commented Jul 29, 2024

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants