fix(viewer): serve static js with script mime type
This commit is contained in:
2
bible
2
bible
Submodule bible updated: 688b87e98d...d2600f1279
@@ -82,3 +82,22 @@ func TestStandaloneHandlerRootShowsUploadForm(t *testing.T) {
|
||||
t.Fatalf("expected standalone handler root to include upload form")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticJSUsesScriptContentType(t *testing.T) {
|
||||
handler := NewHandler(HandlerOptions{Title: "Reanimator Chart"})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/static/view.js", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
contentType := rec.Header().Get("Content-Type")
|
||||
if !strings.Contains(contentType, "javascript") {
|
||||
t.Fatalf("Content-Type = %q, want javascript MIME type", contentType)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "DOMContentLoaded") {
|
||||
t.Fatalf("expected static JS asset body to be served")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@@ -19,6 +20,12 @@ var pageTemplate = template.Must(template.New("view.html").Funcs(template.FuncMa
|
||||
|
||||
var uploadTemplate = template.Must(template.New("upload.html").ParseFS(content, "templates/upload.html"))
|
||||
|
||||
func init() {
|
||||
if err := mime.AddExtensionType(".js", "text/javascript; charset=utf-8"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Render(data any) ([]byte, error) {
|
||||
var out strings.Builder
|
||||
if err := pageTemplate.ExecuteTemplate(&out, "view.html", data); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user