cookie ready
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
"git.ma-al.com/goc_marek/ps_shop/internal/assets"
|
||||
"git.ma-al.com/goc_marek/ps_shop/internal/viewmodel"
|
||||
"git.ma-al.com/goc_marek/ps_shop/templates"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Minimize templ's internal buffering so HTML reaches the client as it is rendered.
|
||||
templruntime.DefaultBufferSize = 1
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
assets assets.Manifest
|
||||
}
|
||||
@@ -19,13 +28,13 @@ func New(manifest assets.Manifest) *Engine {
|
||||
func (e *Engine) Product(w http.ResponseWriter, r *http.Request, data viewmodel.ProductPageData) error {
|
||||
startHTMLStream(w)
|
||||
component := templates.ProductPage(data, e.assets.CSSPath("app.css"), e.assets.JSPath("app.js"))
|
||||
return component.Render(r.Context(), w)
|
||||
return streamComponent(r.Context(), w, component)
|
||||
}
|
||||
|
||||
func (e *Engine) Category(w http.ResponseWriter, r *http.Request, data viewmodel.CategoryPageData) error {
|
||||
startHTMLStream(w)
|
||||
component := templates.CategoryPage(data, e.assets.CSSPath("app.css"), e.assets.JSPath("app.js"))
|
||||
return component.Render(r.Context(), w)
|
||||
return streamComponent(r.Context(), w, component)
|
||||
}
|
||||
|
||||
func startHTMLStream(w http.ResponseWriter) {
|
||||
@@ -41,3 +50,15 @@ func startHTMLStream(w http.ResponseWriter) {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func streamComponent(ctx context.Context, w http.ResponseWriter, component templ.Component) error {
|
||||
if component == nil {
|
||||
return nil
|
||||
}
|
||||
var buffer templruntime.Buffer
|
||||
buffer.Reset(w)
|
||||
if err := component.Render(ctx, &buffer); err != nil {
|
||||
return err
|
||||
}
|
||||
return buffer.Flush()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user