Skip to content

Commit aead69b

Browse files
refactor(web): embedding runtime head tags
1 parent fc4a041 commit aead69b

File tree

4 files changed

+53
-71
lines changed

4 files changed

+53
-71
lines changed

web/helpers/helpers.go

+2-48
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package helpers
33
import (
44
"encoding/json"
55
"html/template"
6-
"io"
76
"time"
87

98
"github.com/ItsNotGoodName/smtpbridge/internal/build"
@@ -15,11 +14,8 @@ var Map template.FuncMap = template.FuncMap{
1514
"build": func() build.Build {
1615
return build.Current
1716
},
18-
"development": func() bool {
19-
return web.Development
20-
},
21-
"manifest": func() _Manifest {
22-
return manifest
17+
"headTags": func() template.HTML {
18+
return template.HTML(web.HeadTags)
2319
},
2420
"timeFormat": func(date time.Time) string {
2521
return date.Local().Format("Jan _2 2006 15:04:05")
@@ -45,45 +41,3 @@ var Map template.FuncMap = template.FuncMap{
4541
return template.URL(Query(queries, vals...))
4642
},
4743
}
48-
49-
var manifest _Manifest
50-
51-
type _Manifest struct {
52-
CSS []string `json:"css"`
53-
File string `json:"file"`
54-
IsEntry bool `json:"isEntry"`
55-
Src string `json:"src"`
56-
}
57-
58-
func init() {
59-
fs := web.AssetsFS()
60-
if web.Development {
61-
return
62-
}
63-
64-
file, err := fs.Open("manifest.json")
65-
if err != nil {
66-
panic(err)
67-
}
68-
69-
data, err := io.ReadAll(file)
70-
if err != nil {
71-
panic(err)
72-
}
73-
74-
var manifestMap map[string]_Manifest
75-
if err := json.Unmarshal(data, &manifestMap); err != nil {
76-
panic(err)
77-
}
78-
mustSetManifest(manifestMap)
79-
}
80-
81-
func mustSetManifest(manifestMap map[string]_Manifest) {
82-
for _, man := range manifestMap {
83-
if man.IsEntry {
84-
manifest = man
85-
return
86-
}
87-
}
88-
panic("entrypoint not found in manifest")
89-
}

web/views/layouts/index.html

+1-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@
1414
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
1515
<meta name="msapplication-TileColor" content="#da532c" />
1616
<meta name="theme-color" content="#ffffff" />
17-
{{if development}}
18-
<!-- if development -->
19-
<script type="module" src="http://localhost:5173/@vite/client"></script>
20-
<script type="module" src="http://localhost:5173/src/main.ts"></script>
21-
{{else}}
22-
<!-- if production -->
23-
{{range $val := manifest.CSS}}
24-
<link rel="stylesheet" href="/{{$val}}" />
25-
{{end}}
26-
<script type="module" src="/{{manifest.File}}"></script>
27-
{{end}}
17+
{{headTags}}
2818
</head>
2919

3020
<body hx-ext="loading-states">

web/web_dev.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package web
44

55
import (
6-
"io/fs"
7-
"os"
86
"path"
97
"path/filepath"
108
"runtime"
@@ -13,10 +11,11 @@ import (
1311
"github.com/gofiber/template/html/v2"
1412
)
1513

16-
var Development = true
17-
1814
const CacheControl = 0
1915

16+
const HeadTags = `<script type="module" src="http://localhost:5173/@vite/client"></script>
17+
<script type="module" src="http://localhost:5173/src/main.ts"></script>`
18+
2019
var pathAssets string
2120
var pathViews string
2221

@@ -31,10 +30,6 @@ func init() {
3130
pathViews = path.Join(cwd, "views")
3231
}
3332

34-
func AssetsFS() fs.FS {
35-
return os.DirFS(pathAssets)
36-
}
37-
3833
func UseAssets(app *fiber.App) {
3934
app.Static("/", pathAssets)
4035
}

web/web_not_dev.go

+47-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package web
44

55
import (
66
"embed"
7+
"encoding/json"
8+
"fmt"
9+
"io"
710
"io/fs"
811
"net/http"
912

@@ -12,14 +15,14 @@ import (
1215
"github.com/gofiber/template/html/v2"
1316
)
1417

15-
var Development = false
16-
1718
const CacheControl = 3600
1819

20+
var HeadTags = ""
21+
1922
//go:embed dist
2023
var assets embed.FS
2124

22-
func AssetsFS() fs.FS {
25+
func assetsFS() fs.FS {
2326
f, err := fs.Sub(assets, "dist")
2427
if err != nil {
2528
panic(err)
@@ -29,7 +32,7 @@ func AssetsFS() fs.FS {
2932

3033
func UseAssets(app *fiber.App) {
3134
app.Use(filesystem.New(filesystem.Config{
32-
Root: http.FS(AssetsFS()),
35+
Root: http.FS(assetsFS()),
3336
MaxAge: CacheControl,
3437
}))
3538
}
@@ -49,3 +52,43 @@ func Engine() *html.Engine {
4952
engine := html.NewFileSystem(http.FS(viewsFS()), ".html")
5053
return engine
5154
}
55+
56+
func init() {
57+
type Manifest struct {
58+
CSS []string `json:"css"`
59+
File string `json:"file"`
60+
IsEntry bool `json:"isEntry"`
61+
Src string `json:"src"`
62+
}
63+
64+
file, err := assets.Open("dist/manifest.json")
65+
if err != nil {
66+
panic(err)
67+
}
68+
69+
data, err := io.ReadAll(file)
70+
if err != nil {
71+
panic(err)
72+
}
73+
74+
var manifestMap map[string]Manifest
75+
if err := json.Unmarshal(data, &manifestMap); err != nil {
76+
panic(err)
77+
}
78+
79+
var manifest Manifest
80+
func() {
81+
for _, man := range manifestMap {
82+
if man.IsEntry {
83+
manifest = man
84+
return
85+
}
86+
}
87+
panic("entrypoint not found in manifest")
88+
}()
89+
90+
for _, v := range manifest.CSS {
91+
HeadTags += fmt.Sprintf(`<link rel="stylesheet" href="/%s" />`, v)
92+
}
93+
HeadTags += fmt.Sprintf(`<script type="module" src="/%s"></script>`, manifest.File)
94+
}

0 commit comments

Comments
 (0)