From 24ae49b54fa53805fcac6c0b335825f630ab8954 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Tue, 12 Apr 2022 20:56:00 -0400 Subject: [PATCH] Allow HTMX to handle redirects if the request is boosted. --- controller/controller.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/controller/controller.go b/controller/controller.go index 4104c29..172ccf4 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/mikestefanello/pagoda/context" + "github.com/mikestefanello/pagoda/htmx" "github.com/mikestefanello/pagoda/middleware" "github.com/mikestefanello/pagoda/services" @@ -154,7 +155,16 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer) // Redirect redirects to a given route name with optional route parameters func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error { url := ctx.Echo().Reverse(route, routeParams) - return ctx.Redirect(http.StatusFound, url) + + if htmx.GetRequest(ctx).Boosted { + htmx.Response{ + Redirect: url, + }.Apply(ctx) + + return nil + } else { + return ctx.Redirect(303, url) + } } // Fail is a helper to fail a request by returning a 500 error and logging the error