diff --git a/README.md b/README.md
index fd6840c..27a1530 100644
--- a/README.md
+++ b/README.md
@@ -210,11 +210,11 @@ i.ShareTemplateData("title", "Home page")
#### Share template func
```go
-i.ShareTemplateFunc("trim", strings.Trim)
+i.ShareTemplateFunc("trim", strings.TrimSpace)
```
```html
-
{{ trim "foo bar" }}
+{{ trim " foo bar " }}
```
#### Pass template data via context (in middleware)
diff --git a/examples/vue3_tailwind/Makefile b/examples/vue3_tailwind/Makefile
new file mode 100644
index 0000000..502d47b
--- /dev/null
+++ b/examples/vue3_tailwind/Makefile
@@ -0,0 +1,2 @@
+ssr:
+ npm run build && node bootstrap/ssr/ssr.js
\ No newline at end of file
diff --git a/inertia.go b/inertia.go
index 0391a3f..2e87e43 100644
--- a/inertia.go
+++ b/inertia.go
@@ -85,20 +85,41 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin
}
if IsInertiaRequest(r) {
- if err := i.doInertiaResponse(w, page); err != nil {
+ if err = i.doInertiaResponse(w, page); err != nil {
return fmt.Errorf("inertia response: %w", err)
}
return
}
- if err := i.doHTMLResponse(w, r, page); err != nil {
+ if err = i.doHTMLResponse(w, r, page); err != nil {
return fmt.Errorf("html response: %w", err)
}
return nil
}
+type page struct {
+ Component string `json:"component"`
+ Props Props `json:"props"`
+ URL string `json:"url"`
+ Version string `json:"version"`
+}
+
+func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*page, error) {
+ props, err := i.prepareProps(r, component, props)
+ if err != nil {
+ return nil, fmt.Errorf("prepare props: %w", err)
+ }
+
+ return &page{
+ Component: component,
+ Props: props,
+ URL: r.RequestURI,
+ Version: i.version,
+ }, nil
+}
+
func (i *Inertia) doInertiaResponse(w http.ResponseWriter, page *page) error {
pageJSON, err := i.marshallJSON(page)
if err != nil {
diff --git a/inertia_bench_test.go b/inertia_bench_test.go
deleted file mode 100644
index 1f7c62b..0000000
--- a/inertia_bench_test.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package gonertia
-
-import (
- "encoding/json"
- "testing"
-)
-
-func BenchmarkInertia_inertiaContainerHTML(b *testing.B) {
- inertia := Inertia{
- containerID: "foobar",
- }
-
- page, err := json.Marshal(map[string]any{
- "foo": "bar",
- })
- if err != nil {
- b.Fatalf("unexpected error: %#v", err)
- }
-
- b.ResetTimer()
-
- for i := 0; i < b.N; i++ {
- inertia.inertiaContainerHTML(page)
- }
-}
diff --git a/inertia_test.go b/inertia_test.go
index d8476ba..340901b 100644
--- a/inertia_test.go
+++ b/inertia_test.go
@@ -2,6 +2,7 @@ package gonertia
import (
"net/http"
+ "strings"
"testing"
"testing/fstest"
)
@@ -150,6 +151,58 @@ func TestInertia_Render(t *testing.T) {
assertRootTemplateSuccess(t, i)
})
+
+ t.Run("shared funcs", func(t *testing.T) {
+ t.Parallel()
+
+ f := tmpFile(t, `{{ trim " foo bar " }}`)
+ w, r := requestMock(http.MethodGet, "/")
+
+ i := I(func(i *Inertia) {
+ i.rootTemplatePath = f.Name()
+ i.sharedTemplateFuncs = TemplateFuncs{
+ "trim": strings.TrimSpace,
+ }
+ })
+
+ err := i.Render(w, r, "Some/Component")
+ if err != nil {
+ t.Fatalf("unexpected error: %#v", err)
+ }
+
+ got := w.Body.String()
+ want := "foo bar"
+
+ if got != want {
+ t.Fatalf("got=%s, want=%s", got, want)
+ }
+ })
+
+ t.Run("shared template data", func(t *testing.T) {
+ t.Parallel()
+
+ f := tmpFile(t, `Hello, {{ .text }}!`)
+ w, r := requestMock(http.MethodGet, "/")
+
+ i := I(func(i *Inertia) {
+ i.rootTemplatePath = f.Name()
+ i.sharedTemplateData = TemplateData{
+ "text": "world",
+ }
+ })
+
+ err := i.Render(w, r, "Some/Component")
+ if err != nil {
+ t.Fatalf("unexpected error: %#v", err)
+ }
+
+ got := w.Body.String()
+ want := "foo bar"
+
+ if got != want {
+ t.Fatalf("got=%s, want=%s", got, want)
+ }
+ })
})
t.Run("inertia request", func(t *testing.T) {
diff --git a/page.go b/page.go
deleted file mode 100644
index 773460f..0000000
--- a/page.go
+++ /dev/null
@@ -1,27 +0,0 @@
-package gonertia
-
-import (
- "fmt"
- "net/http"
-)
-
-type page struct {
- Component string `json:"component"`
- Props Props `json:"props"`
- URL string `json:"url"`
- Version string `json:"version"`
-}
-
-func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*page, error) {
- props, err := i.prepareProps(r, component, props)
- if err != nil {
- return nil, fmt.Errorf("prepare props: %w", err)
- }
-
- return &page{
- Component: component,
- Props: props,
- URL: r.RequestURI,
- Version: i.version,
- }, nil
-}
diff --git a/page_bench_test.go b/page_bench_test.go
deleted file mode 100644
index 6cef314..0000000
--- a/page_bench_test.go
+++ /dev/null
@@ -1,26 +0,0 @@
-package gonertia
-
-import (
- "context"
- "net/http"
- "testing"
-)
-
-func BenchmarkInertia_buildPage(b *testing.B) {
- inertia := Inertia{}
-
- req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil)
- if err != nil {
- b.Fatalf("unexpected error: %#v", err)
- }
-
- props := Props{
- "foo": "bar",
- }
-
- b.ResetTimer()
-
- for i := 0; i < b.N; i++ {
- _, _ = inertia.buildPage(req, "foobar", props)
- }
-}