Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
petaki committed Feb 14, 2025
2 parents be28424 + bee0ced commit 73bfe20
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: '1.24'

- name: Test
run: go test -v ./...
Expand All @@ -24,5 +24,5 @@ jobs:
- name: Staticcheck
uses: dominikh/[email protected]
with:
version: '2024.1.1'
version: '2025.1'
install-go: false
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/petaki/inertia-go

go 1.23
go 1.24
9 changes: 9 additions & 0 deletions header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package inertia

const (
HeaderInertia = "X-Inertia"
HeaderLocation = "X-Inertia-Location"
HeaderVersion = "X-Inertia-Version"
HeaderPartialComponent = "X-Inertia-Partial-Component"
HeaderPartialOnly = "X-Inertia-Partial-Data"
)
10 changes: 5 additions & 5 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func (i *Inertia) WithViewData(ctx context.Context, key string, value interface{
// Render function.
func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component string, props map[string]interface{}) error {
only := make(map[string]string)
partial := r.Header.Get("X-Inertia-Partial-Data")
partial := r.Header.Get(HeaderPartialOnly)

if partial != "" && r.Header.Get("X-Inertia-Partial-Component") == component {
if partial != "" && r.Header.Get(HeaderPartialComponent) == component {
for _, value := range strings.Split(partial, ",") {
only[value] = value
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin
}

w.Header().Set("Vary", "Accept")
w.Header().Set("X-Inertia", "true")
w.Header().Set(HeaderInertia, "true")
w.Header().Set("Content-Type", "application/json")

_, err = w.Write(js)
Expand Down Expand Up @@ -220,15 +220,15 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin
// Location function.
func (i *Inertia) Location(w http.ResponseWriter, r *http.Request, url string) {
if i.isInertiaRequest(r) {
w.Header().Set("X-Inertia-Location", url)
w.Header().Set(HeaderLocation, url)
w.WriteHeader(http.StatusConflict)
} else {
http.Redirect(w, r, url, http.StatusFound)
}
}

func (i *Inertia) isInertiaRequest(r *http.Request) bool {
return r.Header.Get("X-Inertia") != ""
return r.Header.Get(HeaderInertia) != ""
}

func (i *Inertia) createRootTemplate() (*template.Template, error) {
Expand Down
6 changes: 3 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "net/http"
// Middleware function.
func (i *Inertia) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Inertia") == "" {
if r.Header.Get(HeaderInertia) == "" {
next.ServeHTTP(w, r)

return
}

if r.Method == "GET" && r.Header.Get("X-Inertia-Version") != i.version {
w.Header().Set("X-Inertia-Location", i.url+r.RequestURI)
if r.Method == "GET" && r.Header.Get(HeaderVersion) != i.version {
w.Header().Set(HeaderLocation, i.url+r.RequestURI)
w.WriteHeader(http.StatusConflict)

return
Expand Down

0 comments on commit 73bfe20

Please sign in to comment.