This commit is contained in:
@@ -19,7 +19,7 @@ func (s *Server) home(w http.ResponseWriter, r *http.Request) {
|
||||
s.error(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, "home.html", pageData{Title: content.HeroTitle, Active: "home", Content: content, Projects: projects, CurrentPath: r.URL.Path})
|
||||
s.renderPublic(w, r, "home.html", "home_partial.html", pageData{Title: content.HeroTitle, Active: "home", Content: content, Projects: projects, CurrentPath: r.URL.Path})
|
||||
}
|
||||
|
||||
func (s *Server) projects(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -33,7 +33,7 @@ func (s *Server) projects(w http.ResponseWriter, r *http.Request) {
|
||||
s.error(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, "projects.html", pageData{Title: "Projects", Active: "projects", Content: content, Projects: projects, CurrentPath: r.URL.Path})
|
||||
s.renderPublic(w, r, "projects.html", "projects_partial.html", pageData{Title: "Projects", Active: "projects", Content: content, Projects: projects, CurrentPath: r.URL.Path})
|
||||
}
|
||||
|
||||
func (s *Server) projectDetail(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -51,7 +51,7 @@ func (s *Server) projectDetail(w http.ResponseWriter, r *http.Request) {
|
||||
s.error(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, "project.html", pageData{Title: project.Title, Active: "projects", Content: content, Project: project, CurrentPath: r.URL.Path})
|
||||
s.renderPublic(w, r, "project.html", "project_partial.html", pageData{Title: project.Title, Active: "projects", Content: content, Project: project, CurrentPath: r.URL.Path})
|
||||
}
|
||||
|
||||
func (s *Server) projectImageOverlay(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -78,7 +78,7 @@ func (s *Server) about(w http.ResponseWriter, r *http.Request) {
|
||||
s.error(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, "about.html", pageData{Title: "Studio", Active: "about", Content: content, CurrentPath: r.URL.Path})
|
||||
s.renderPublic(w, r, "about.html", "about_partial.html", pageData{Title: "Studio", Active: "about", Content: content, CurrentPath: r.URL.Path})
|
||||
}
|
||||
|
||||
func (s *Server) services(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -97,7 +97,7 @@ func (s *Server) services(w http.ResponseWriter, r *http.Request) {
|
||||
s.error(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, "services.html", pageData{Title: "Services", Active: "services", Content: content, Services: services, FAQs: faqs, CurrentPath: r.URL.Path})
|
||||
s.renderPublic(w, r, "services.html", "services_partial.html", pageData{Title: "Services", Active: "services", Content: content, Services: services, FAQs: faqs, CurrentPath: r.URL.Path})
|
||||
}
|
||||
|
||||
func (s *Server) contactPage(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -106,7 +106,7 @@ func (s *Server) contactPage(w http.ResponseWriter, r *http.Request) {
|
||||
s.error(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, "contact.html", pageData{Title: "Contact", Active: "contact", Content: content, CurrentPath: r.URL.Path})
|
||||
s.renderPublic(w, r, "contact.html", "contact_partial.html", pageData{Title: "Contact", Active: "contact", Content: content, CurrentPath: r.URL.Path})
|
||||
}
|
||||
|
||||
func (s *Server) contact(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -24,6 +24,48 @@ func TestPublicRoutes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicRoutesRenderFullShellOnDirectLoad(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/services", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
srv.Routes().ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected ok, got %d", rec.Code)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Result().Body)
|
||||
text := string(body)
|
||||
for _, want := range []string{"<!doctype html>", `class="drawer drawer-end"`, `id="main-content"`, `id="site-drawer"`} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("direct public route missing %q: %s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicHTMXRoutesReturnMainContentPartial(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/services", nil)
|
||||
req.Header.Set("HX-Request", "true")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
srv.Routes().ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected ok, got %d", rec.Code)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Result().Body)
|
||||
text := string(body)
|
||||
if strings.Contains(text, "<!doctype html>") || strings.Contains(text, `class="drawer drawer-end"`) {
|
||||
t.Fatalf("expected partial response, got full document: %s", text)
|
||||
}
|
||||
for _, want := range []string{`id="main-content"`, `id="site-header"`, `id="site-drawer-nav"`, `hx-swap-oob="true"`} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("HTMX public route missing %q: %s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestServicesRouteRendersServicesAndFAQs(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/services", nil)
|
||||
|
||||
@@ -25,6 +25,14 @@ func (s *Server) renderAdmin(w http.ResponseWriter, r *http.Request, fullTemplat
|
||||
s.render(w, fullTemplate, data)
|
||||
}
|
||||
|
||||
func (s *Server) renderPublic(w http.ResponseWriter, r *http.Request, fullTemplate, partialTemplate string, data pageData) {
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
s.render(w, partialTemplate, data)
|
||||
return
|
||||
}
|
||||
s.render(w, fullTemplate, data)
|
||||
}
|
||||
|
||||
func (s *Server) error(w http.ResponseWriter, err error) {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user