diff --git a/internal/gui/api.go b/internal/gui/api.go index ced4b8716..1b6dca6ad 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -997,7 +997,14 @@ var onDock func(settings.Settings) 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() { +// addAssetTypes is called by Handler, not init: the mime package reads +// and parses the system's whole type table (/etc/apache2/mime.types, +// ~18,000 allocations) the first time a type is added, and a launch that +// serves no page (`magpie statusline` on every Claude Code message) never +// needs it. +var addAssetTypes = sync.OnceFunc(registerAssetTypes) + +func registerAssetTypes() { for ext, t := range assetTypes { mime.AddExtensionType(ext, t) } @@ -1075,6 +1082,7 @@ func versionedPage(page []byte) []byte { // gw is the gateway this process serves, or nil when another magpie has it // (for now: see startBackend). func Handler(w Windows, gw *gateway.Server) http.Handler { + addAssetTypes() if gw != nil { served.Store(gw) } diff --git a/internal/gui/assets_test.go b/internal/gui/assets_test.go index 59284816f..52bcf701e 100644 --- a/internal/gui/assets_test.go +++ b/internal/gui/assets_test.go @@ -2,10 +2,20 @@ package gui import ( "mime" + "sync" "testing" ) +// Handler sets the page's own types even where the system's table says +// otherwise (Windows' registry, which another program may have changed). func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) { + for ext := range assetTypes { + mime.AddExtensionType(ext, "application/x-other") + } + was := addAssetTypes + addAssetTypes = sync.OnceFunc(registerAssetTypes) + t.Cleanup(func() { addAssetTypes = was; registerAssetTypes() }) + Handler(nil, nil) for ext, want := range assetTypes { if got := mime.TypeByExtension(ext); got != want { t.Errorf("%s served as %q, not %q", ext, got, want)