Skip to content

Commit

Permalink
Merge pull request #16 from romsar/fix-hard-reload-on-409
Browse files Browse the repository at this point in the history
fix hard reloading
  • Loading branch information
romsar authored Sep 27, 2024
2 parents 450d363 + a455067 commit c615c79
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
12 changes: 11 additions & 1 deletion helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func assertHeaderMissing(t *testing.T, w *httptest.ResponseRecorder, key string)
t.Helper()

if got := w.Header().Get(key); got != "" {
t.Fatalf("header=%s, want=%s", got, "")
t.Fatalf("unexpected header %s=%s, want=empty", key, got)
}
}

Expand Down Expand Up @@ -133,6 +133,16 @@ func assertInertiaVary(t *testing.T, w *httptest.ResponseRecorder) {
}
}

func assertInertiaNotVary(t *testing.T, w *httptest.ResponseRecorder) {
t.Helper()

gotVary := w.Header().Get("Vary")

if gotVary != "" {
t.Fatal("unexpected Vary header found")
}
}

func assertHandlerServed(t *testing.T, handlers ...http.HandlerFunc) http.HandlerFunc {
t.Helper()

Expand Down
42 changes: 30 additions & 12 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,48 @@ import (
"strings"
)

const (
headerInertia = "X-Inertia"
headerInertiaLocation = "X-Inertia-Location"
headerInertiaPartialData = "X-Inertia-Partial-Data"
headerInertiaPartialExcept = "X-Inertia-Partial-Except"
headerInertiaPartialComponent = "X-Inertia-Partial-Component"
headerInertiaVersion = "X-Inertia-Version"
headerVary = "Vary"
headerContentType = "Content-Type"
)

// IsInertiaRequest returns true if the request is an Inertia request.
func IsInertiaRequest(r *http.Request) bool {
return r.Header.Get("X-Inertia") != ""
return r.Header.Get(headerInertia) != ""
}

func setInertiaInResponse(w http.ResponseWriter) {
w.Header().Set("X-Inertia", "true")
w.Header().Set(headerInertia, "true")
}

func deleteInertiaInResponse(w http.ResponseWriter) {
w.Header().Del(headerInertia)
}

func setInertiaVaryInResponse(w http.ResponseWriter) {
w.Header().Set("Vary", "X-Inertia")
w.Header().Set(headerVary, headerInertia)
}

func deleteVaryInResponse(w http.ResponseWriter) {
w.Header().Del(headerVary)
}

func setInertiaLocationInResponse(w http.ResponseWriter, url string) {
w.Header().Set("X-Inertia-Location", url)
setResponseStatus(w, http.StatusConflict)
w.Header().Set(headerInertiaLocation, url)
}

func setResponseStatus(w http.ResponseWriter, status int) {
w.WriteHeader(status)
}

func onlyFromRequest(r *http.Request) []string {
header := r.Header.Get("X-Inertia-Partial-Data")
header := r.Header.Get(headerInertiaPartialData)
if header == "" {
return nil
}
Expand All @@ -37,7 +55,7 @@ func onlyFromRequest(r *http.Request) []string {
}

func exceptFromRequest(r *http.Request) []string {
header := r.Header.Get("X-Inertia-Partial-Except")
header := r.Header.Get(headerInertiaPartialExcept)
if header == "" {
return nil
}
Expand All @@ -46,27 +64,27 @@ func exceptFromRequest(r *http.Request) []string {
}

func partialComponentFromRequest(r *http.Request) string {
return r.Header.Get("X-Inertia-Partial-Component")
return r.Header.Get(headerInertiaPartialComponent)
}

func inertiaVersionFromRequest(r *http.Request) string {
return r.Header.Get("X-Inertia-Version")
return r.Header.Get(headerInertiaVersion)
}

func redirectResponse(w http.ResponseWriter, r *http.Request, url string, status ...int) {
http.Redirect(w, r, url, firstOr[int](status, http.StatusFound))
}

func setJSONResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set(headerContentType, "application/json")
}

func setJSONRequest(r *http.Request) {
r.Header.Set("Content-Type", "application/json")
r.Header.Set(headerContentType, "application/json")
}

func setHTMLResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html")
w.Header().Set(headerContentType, "text/html")
}

func isSeeOtherRedirectMethod(method string) bool {
Expand Down
9 changes: 7 additions & 2 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ func TestInertia_Middleware(t *testing.T) {
asInertiaRequest(r)
withInertiaVersion(r, "bar")

i.Middleware(assertHandlerServed(t, successJSONHandler)).ServeHTTP(w, r)
i.Middleware(assertHandlerServed(t, setInertiaResponseHandler, successJSONHandler)).ServeHTTP(w, r)

assertInertiaVary(t, w)
assertInertiaNotVary(t, w)
assertNotInertiaResponse(t, w)
assertResponseStatusCode(t, w, http.StatusConflict)
assertInertiaLocation(t, w, "/home")

Expand Down Expand Up @@ -272,3 +273,7 @@ func setHeadersHandler(headers map[string]string) http.HandlerFunc {
}
}
}

func setInertiaResponseHandler(w http.ResponseWriter, _ *http.Request) {
setInertiaInResponse(w)
}
3 changes: 3 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (i *Inertia) Location(w http.ResponseWriter, r *http.Request, url string, s

if IsInertiaRequest(r) {
setInertiaLocationInResponse(w, url)
deleteInertiaInResponse(w)
deleteVaryInResponse(w)
setResponseStatus(w, http.StatusConflict)
return
}

Expand Down

0 comments on commit c615c79

Please sign in to comment.