-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
90 lines (77 loc) · 1.81 KB
/
Copy pathrender.go
File metadata and controls
90 lines (77 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package gowebi
import (
"context"
"encoding/json"
"fmt"
"html/template"
"net/http"
)
type Render struct {
bundleMap map[string]*Bundle
tmpl *template.Template
suppressConsoleLogs bool
isDev bool
}
func NewRenderer(bMap map[string]*Bundle, tmpl *template.Template, suppressConsoleLogs, isDev bool) *Render {
return &Render{
bundleMap: bMap,
tmpl: tmpl,
isDev: isDev,
suppressConsoleLogs: suppressConsoleLogs,
}
}
func (r *Render) Render(ctx context.Context, w http.ResponseWriter, status int, opts RenderOptions) error {
if err := ctx.Err(); err != nil {
return err
}
b, ok := r.bundleMap[opts.Name]
if !ok {
return fmt.Errorf("page bundle not found")
}
vm := newVM(r.suppressConsoleLogs)
clear := attachVMInterrupt(ctx, vm)
defer func() {
clear()
vm.ClearInterrupt()
}()
runtime, err := runWithVM(vm, b.Program)
if err != nil {
if r.isDev {
return writeDebugError(w, err)
}
return err
}
appHtml, err := getStaticHTML(runtime, opts.Props)
if err != nil {
if r.isDev {
return writeDebugError(w, err)
}
return err
}
metadata := opts.Meta
if metadata == nil {
metadata, err = getMetadata(runtime, opts.Props)
if err != nil {
if r.isDev {
return writeDebugError(w, err)
}
return err
}
}
raw, err := json.Marshal(opts.Props)
if err != nil {
return err
}
script := getHydrationScript(raw, r.isDev)
w.Header().Add("Content-Type", "text/html; charset=utf8")
w.WriteHeader(status)
return r.tmpl.Execute(w, map[string]any{
"Meta": metadata,
"Script": template.HTML(script),
"App": template.HTML(appHtml),
"HydrationScriptSrc": b.ClientPath,
"NoHydrate": opts.NoHydrate,
"IsDev": r.isDev,
"Props": opts.Props,
})
}