-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
343 lines (312 loc) · 9.59 KB
/
handler.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package vite
import (
"fmt"
"html/template"
"io/fs"
"log/slog"
"net/http"
"path"
"strings"
)
// Handler serves files from the Vite output directory.
type Handler struct {
fs fs.FS
fsFS http.FileSystem
fsHandler http.Handler
pub fs.FS
pubFS http.FileSystem
pubHandler http.Handler
manifest *Manifest
isDev bool
viteEntry string
viteURL string
viteTemplate Scaffolding
templates map[string]*template.Template
defaultMetadata *Metadata
}
// NewHandler creates a new handler.
//
// fs is the file system to serve files from, the Vite output directory
// (which usually is the "dist" directory). isDev is true if the server is
// running in development mode, false otherwise. viteServer is the URL of the
// Vite server, used to load the Vite client in development mode.
func NewHandler(config Config) (*Handler, error) {
if config.FS == nil {
return nil, fmt.Errorf("vite: fs is nil")
}
h := &Handler{
fs: config.FS,
fsFS: http.FS(config.FS),
fsHandler: http.FileServerFS(config.FS),
isDev: config.IsDev,
viteEntry: config.ViteEntry,
viteURL: config.ViteURL,
viteTemplate: config.ViteTemplate,
templates: make(map[string]*template.Template),
}
// We register a fallback template.
h.templates[fallbackTemplateName] = template.Must(template.New(fallbackTemplateName).Parse(fallbackHTML))
if !h.isDev {
// Production mode.
//
// We expect the output directory to contain a .vite/manifest.json file.
// This file contains the mapping of the original file paths to the
// transformed file paths.
if config.ViteManifest == "" {
config.ViteManifest = ".vite/manifest.json"
}
mf, err := h.fs.Open(config.ViteManifest)
if err != nil {
return nil, fmt.Errorf("vite: open manifest: %w", err)
}
defer mf.Close()
// Read the manifest file.
h.manifest, err = ParseManifest(mf)
if err != nil {
return nil, fmt.Errorf("vite: parse manifest: %w", err)
}
} else {
// Development mode.
if h.viteURL == "" {
h.viteURL = "http://localhost:5173"
}
if config.PublicFS == nil {
// We will peek into the "public" directory of the Vite app, and
// serve files from there (if it exists).
pub, err := fs.Sub(config.FS, "public")
if err == nil {
h.pub = pub
h.pubFS = http.FS(h.pub)
h.pubHandler = http.FileServerFS(h.pub)
}
} else {
h.pub = config.PublicFS
h.pubFS = http.FS(config.PublicFS)
h.pubHandler = http.FileServerFS(config.PublicFS)
}
}
return h, nil
}
// SetDefaultMetadata sets the default metadata to use when rendering the
// page. This metadata is used when the context does not have any metadata.
func (h *Handler) SetDefaultMetadata(md *Metadata) {
h.defaultMetadata = md
}
// RegisterTemplate adds a new template to the handler's template collection.
// The 'name' parameter should match the URL path where the template will be used.
// Use "index.html" for the root URL ("/").
//
// Parameters:
// - name: String identifier for the template, corresponding to its URL path
// - text: String content of the template
//
// Panics if a template with the given name is already registered.
func (h *Handler) RegisterTemplate(name, text string) {
if h.templates == nil {
h.templates = make(map[string]*template.Template)
}
if _, ok := h.templates[name]; ok {
panic(fmt.Sprintf("vite: template %q already registered", name))
}
h.templates[name] = template.Must(template.New(name).Parse(text))
}
// HandlerFunc returns a http.HandlerFunc for h.
func (h *Handler) HandlerFunc() http.HandlerFunc {
return http.HandlerFunc(h.ServeHTTP)
}
// ServeHTTP handles HTTP requests.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Normalize the path, e.g. /..//articles/123/ -> /articles/123
path := path.Clean(r.URL.Path)
isIndexPath := path == "/" || path == "/index.html"
// Check if the file exists in the public directory.
if h.isDev && h.pubFS != nil && h.pubHandler != nil && !isIndexPath {
if _, err := h.pubFS.Open(path); err == nil {
h.pubHandler.ServeHTTP(w, r)
return
}
}
if isIndexPath {
// We didn't find it in the file system, so we generate the HTML
// from the entry point with Go templating.
h.renderPage(w, r, path, nil)
return
}
if _, ok := h.templates[path]; ok {
// We found a template for the path, so we render the page using
// the template.
h.renderPage(w, r, path, nil)
return
}
// Check if the file exists in the file system.
if _, err := h.fsFS.Open(path); err != nil {
// The file does not exist in the file system, so 404.
http.NotFound(w, r)
return
}
// Serve the file using the file server.
h.fsHandler.ServeHTTP(w, r)
}
// pageData is passed to the template when rendering the page.
type pageData struct {
IsDev bool
ViteEntry string
ViteURL string
Metadata template.HTML
PluginReactPreamble template.HTML
StyleSheets template.HTML
Modules template.HTML
PreloadModules template.HTML
Scripts template.HTML
}
// renderPage renders the page using the template.
func (h *Handler) renderPage(w http.ResponseWriter, r *http.Request, path string, chunk *Chunk) {
page := pageData{
IsDev: h.isDev,
ViteEntry: h.viteEntry,
ViteURL: h.viteURL,
}
// Inject metadata in// Check if the specified Vite template requires a preamble and set the
// corresponding preamble string in the plugin configuration.
//
// If the Vite template value is less than 1, it is considered as an
// uninitialized state, and the default React preamble is applied.
// Otherwise, if the template requires a preamble, it uses the
// specific preamble for the given Vite template.to the page.
ctx := r.Context()
md := MetadataFromContext(ctx)
if md == nil {
md = h.defaultMetadata
}
if md != nil {
page.Metadata = template.HTML(md.String())
}
// Inject scripts into the page.
scripts := ScriptsFromContext(ctx)
if scripts != "" {
page.Scripts = template.HTML(scripts)
}
// Handle both development and production modes.
if h.isDev {
// Check if the specified Vite template requires a preamble and set the
// corresponding preamble string in the plugin configuration.
//
// If the Vite template value is less than 1, it is considered as an
// uninitialized state, and the default React preamble is applied.
// Otherwise, if the template requires a preamble, it uses the
// specific preamble for the given Vite template.
if h.viteTemplate < 1 {
page.PluginReactPreamble = template.HTML(React.Preamble(h.viteURL))
} else if h.viteTemplate.RequiresPreamble() {
page.PluginReactPreamble = template.HTML(h.viteTemplate.Preamble(h.viteURL))
}
// page.PluginReactPreamble = template.HTML(PluginReactPreamble(h.viteURL))
} else {
if chunk == nil {
if page.ViteEntry == "" {
chunk = h.manifest.GetEntryPoint()
} else {
entries := h.manifest.GetEntryPoints()
for _, entry := range entries {
if page.ViteEntry == entry.Src {
chunk = entry
break
}
}
}
if chunk == nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
page.StyleSheets = template.HTML(h.manifest.GenerateCSS(chunk.Src))
page.Modules = template.HTML(h.manifest.GenerateModules(chunk.Src))
page.PreloadModules = template.HTML(h.manifest.GeneratePreloadModules(chunk.Src))
}
var tmplName string
if path == "/" {
tmplName = "index.html"
} else {
tmplName = path
}
// Find the template by name.
tmpl, ok := h.templates[tmplName]
// Catch common variations. If a template isn't found by the exact name,
// check for variations like: "page", "page.html", or "/page.html", to match
// how users might have registered the template.
if !ok {
variations := []string{
strings.TrimPrefix(tmplName, "/"),
strings.TrimPrefix(tmplName, "/") + ".html",
strings.TrimSuffix(strings.TrimPrefix(tmplName, "/"), ".html"),
tmplName + ".html",
}
for _, variant := range variations {
if t, found := h.templates[variant]; found {
tmpl = t
ok = true
break
}
}
}
// Handle case when requested template is not found:
// 1. If multiple templates exist, log a warning with the requested and available templates.
// 2. Fall back to a default template.
if !ok {
if len(h.templates) > 1 {
keys := make([]string, 0, len(h.templates))
for k := range h.templates {
keys = append(keys, k)
}
slog.Warn(
"Template not found",
"requestedTemplate", tmplName,
"availableTemplates", strings.Join(keys, ", "),
)
}
tmpl = h.templates[fallbackTemplateName]
}
// Execute the template.
if err := tmpl.Execute(w, page); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
const fallbackTemplateName = "fallback.html"
var (
fallbackHTML = `<!doctype html>
<html lang="en" class="h-full scroll-smooth">
<head>
<meta charset="UTF-8" />
{{- if .Metadata }}
{{ .Metadata }}
{{- end }}
{{- if .IsDev }}
{{ .PluginReactPreamble }}
<script type="module" src="{{ .ViteURL }}/@vite/client"></script>
{{- if ne .ViteEntry "" }}
<script type="module" src="{{ .ViteURL }}/{{ .ViteEntry }}"></script>
{{- else }}
<script type="module" src="{{ .ViteURL }}/src/main.tsx"></script>
{{- end }}
{{- else }}
{{- if .StyleSheets }}
{{ .StyleSheets }}
{{- end }}
{{- if .Modules }}
{{ .Modules }}
{{- end }}
{{- if .PreloadModules }}
{{ .PreloadModules }}
{{- end }}
{{- end }}
{{- if .Scripts }}
{{ .Scripts }}
{{- end }}
</head>
<body class="min-h-screen antialiased">
<div id="root"></div>
</body>
</html>
`
)