fix(web): манифест PWA отдаётся с правильным типом
Go определяет .webmanifest как text/plain, из-за чего браузер мог отказаться ставить приложение: тип задаём явно вместе с js/css/svg.
This commit is contained in:
@@ -39,7 +39,9 @@ func (h *staticHandler) serve(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if contentType := mime.TypeByExtension(filepath.Ext(rel)); contentType != "" {
|
||||
// Часть расширений (манифест PWA) известна не всем системам: тип задаём
|
||||
// явно, иначе браузер получает text/plain и отказывается ставить приложение.
|
||||
if contentType := staticContentType(rel); contentType != "" {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
if strings.HasPrefix(rel, "assets/") {
|
||||
@@ -50,6 +52,21 @@ func (h *staticHandler) serve(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, filepath.Join(h.root, filepath.FromSlash(rel)))
|
||||
}
|
||||
|
||||
// staticContentType определяет тип файла: сначала явные правила, потом mime.
|
||||
func staticContentType(rel string) string {
|
||||
switch filepath.Ext(rel) {
|
||||
case ".webmanifest":
|
||||
return "application/manifest+json"
|
||||
case ".js":
|
||||
return "text/javascript; charset=utf-8"
|
||||
case ".css":
|
||||
return "text/css; charset=utf-8"
|
||||
case ".svg":
|
||||
return "image/svg+xml"
|
||||
}
|
||||
return mime.TypeByExtension(filepath.Ext(rel))
|
||||
}
|
||||
|
||||
func (h *staticHandler) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
index := filepath.Join(h.root, "index.html")
|
||||
if _, err := os.Stat(index); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user