Skip to content

Commit

Permalink
Merge pull request #16 from kohkimakimoto/fix-location
Browse files Browse the repository at this point in the history
update location method
  • Loading branch information
kohkimakimoto committed Jul 12, 2023
2 parents 17f2ec1 + ca28fad commit e4f6eeb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inertia

import (
"bytes"
"net/http"
"sync"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -94,10 +95,14 @@ func (i *Inertia) Version() string {
// Location generates 409 response for external redirects
// see https://inertiajs.com/redirects#external-redirects
func (i *Inertia) Location(url string) error {
res := i.c.Response()
res.Header().Set(HeaderXInertiaLocation, url)
res.WriteHeader(409)
return nil
if i.c.Request().Header.Get(HeaderXInertia) != "" {
res := i.c.Response()
res.Header().Set(HeaderXInertiaLocation, url)
res.WriteHeader(409)
return nil
} else {
return i.c.Redirect(http.StatusFound, url)
}
}

func (i *Inertia) Render(code int, component string, props map[string]interface{}) error {
Expand Down
1 change: 1 addition & 0 deletions inertia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func TestInertia_Version(t *testing.T) {
func TestInertia_Location(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(HeaderXInertia, "true")
res := httptest.NewRecorder()
c := e.NewContext(req, res)

Expand Down

0 comments on commit e4f6eeb

Please sign in to comment.