-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Fiber version
v2.25.0
Issue description
I found this issue while searching for a way to do internal redirects. #1398 seems to suggest that this is not supported, however, there is the out-of-tree rewrite middleware that is exactly doing that. It essentially uses the c.Path()
method to set a new path. The code below shows a simplified example.
As also mentioned in #1398, an internal redirect would need to reset c.indexRoute
(and c.indexHandler
). This is currently not done which could lead to unexpected routing decisions. In the example below requesting GET /old
would return Not Found
while I would expect it to be new
. This is because the /new
route was already checked before the internal redirect and is thus skipped. If instead the /new
route would be registered after the redirect handler it would be executed.
I initially thought of resetting c.indexRoute
and c.indexHandler
in c.Path()
, but this could be unexpected as well, e.g. handlers might be executed twice. Making this an explicit choice by the user might be more appropriate.
What do you think about adding a Restart
method to the context that resets c.indexRoute
and then executes c.app.next()
?
Code snippet
package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Get("/new", func(c *fiber.Ctx) error {
return c.SendString("new")
})
app.Use(func(c *fiber.Ctx) error {
c.Path("/new")
return c.Next()
})
app.Use(func(c *fiber.Ctx) error {
// e.g. catch-all for 404
return fiber.ErrNotFound
})
app.Listen(":3000")
}