diff --git a/internal/gui/api.go b/internal/gui/api.go index aed444244..6331a6989 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -8,6 +8,7 @@ import ( "context" "embed" "encoding/json" + "mime" "net/http" "os" "strings" @@ -120,6 +121,18 @@ func settingsState() settingsJSON { // page changes that; set by the process that has the app. var onDock func(bool) +// assetTypes are the page's own files' types. The file server takes them +// from Windows' registry, which another program may have changed — an SVG +// served as something else draws no icon. +var assetTypes = map[string]string{".svg": "image/svg+xml", ".png": "image/png", ".css": "text/css; charset=utf-8", + ".js": "text/javascript; charset=utf-8", ".html": "text/html; charset=utf-8"} + +func init() { + for ext, t := range assetTypes { + mime.AddExtensionType(ext, t) + } +} + // Handler serves the embedded UI and the JSON API. // gw is the gateway this process serves, or nil when another magpie has it // (for now: see startBackend). diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index c175a0cad..b29b0ccc1 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -112,6 +112,13 @@ function icon(name) { const m = el("span", "mask"); m.style.setProperty("--i", `url(icons/${name}.svg)`); e.append(m); + // a mask that fails to load draws nothing, and says nothing + const probe = new Image(); + probe.onerror = () => { + e.classList.add("generic"); + e.replaceChildren(...icon("generic").childNodes); + }; + probe.src = `icons/${name}.svg`; } return e; } diff --git a/internal/gui/assets_test.go b/internal/gui/assets_test.go new file mode 100644 index 000000000..59284816f --- /dev/null +++ b/internal/gui/assets_test.go @@ -0,0 +1,14 @@ +package gui + +import ( + "mime" + "testing" +) + +func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) { + for ext, want := range assetTypes { + if got := mime.TypeByExtension(ext); got != want { + t.Errorf("%s served as %q, not %q", ext, got, want) + } + } +}