diff --git a/routers/web/misc/misc.go b/routers/web/misc/misc.go index aadf65818f9..ef5b84f20b7 100644 --- a/routers/web/misc/misc.go +++ b/routers/web/misc/misc.go @@ -7,11 +7,8 @@ import ( "net/http" "path" "strconv" - "strings" "gitea.dev/modules/httpcache" - "gitea.dev/modules/httplib" - "gitea.dev/modules/json" "gitea.dev/modules/log" "gitea.dev/modules/setting" "gitea.dev/modules/util" @@ -20,29 +17,6 @@ import ( "gitea.dev/services/context" ) -func SiteManifest(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Content-Type", "application/manifest+json") - if httpcache.HandleGenericETagPublicCache(req, w, "", &setting.AppStartTime) { - return - } - if req.Method == http.MethodHead { - return - } - - ctx := req.Context() - absoluteAssetURL := strings.TrimSuffix(httplib.MakeAbsoluteURL(ctx, setting.StaticURLPrefix), "/") - manifest := map[string]any{ - "name": setting.AppName, - "short_name": setting.AppName, - "start_url": httplib.GuessCurrentAppURL(ctx), - "icons": []map[string]string{ - {"src": absoluteAssetURL + "/assets/img/logo.png", "type": "image/png", "sizes": "512x512"}, - {"src": absoluteAssetURL + "/assets/img/logo.svg", "type": "image/svg+xml", "sizes": "512x512"}, - }, - } - _ = json.NewEncoder(w).Encode(manifest) -} - func SSHInfo(rw http.ResponseWriter, req *http.Request) { rw.Header().Set("content-type", "text/json;charset=UTF-8") _, err := rw.Write([]byte(agit.SshInfoJson)) diff --git a/routers/web/web.go b/routers/web/web.go index 36250213fa7..1c2ddbb2cda 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -261,7 +261,6 @@ func Routes() *web.Router { routes.BeforeRouting(user.UsernamePreRoute) routes.Head("/", misc.DummyOK) // for health check - doesn't need to be passed through gzip handler - routes.Methods("GET, HEAD", "/assets/site-manifest.json", misc.SiteManifest) routes.Methods("GET, HEAD, OPTIONS", "/assets/*", routing.MarkLogLevelTrace, public.AssetsCors(), public.FileHandlerFunc()) routes.Methods("GET, HEAD", "/avatars/*", avatarStorageHandler(setting.Avatar.Storage, "avatars", storage.Avatars)) routes.Methods("GET, HEAD", "/repo-avatars/*", avatarStorageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars)) diff --git a/services/context/context_template.go b/services/context/context_template.go index e981ad9afe2..2bfab75ab96 100644 --- a/services/context/context_template.go +++ b/services/context/context_template.go @@ -5,6 +5,7 @@ package context import ( "context" + "encoding/base64" "html" "html/template" "net/http" @@ -14,6 +15,7 @@ import ( user_model "gitea.dev/models/user" "gitea.dev/modules/httplib" + "gitea.dev/modules/json" "gitea.dev/modules/public" "gitea.dev/modules/reqctx" "gitea.dev/modules/setting" @@ -99,6 +101,21 @@ func (c TemplateContext) AppFullLink(link ...string) template.URL { return template.URL(s + "/" + strings.TrimPrefix(link[0], "/")) } +func (c TemplateContext) SiteManifestURL() template.URL { + ctx := c.parentContext() + absoluteAssetURL := strings.TrimSuffix(httplib.MakeAbsoluteURL(ctx, setting.StaticURLPrefix), "/") + manifest, _ := json.Marshal(map[string]any{ + "name": setting.AppName, + "short_name": setting.AppName, + "start_url": httplib.GuessCurrentAppURL(ctx), + "icons": []map[string]string{ + {"src": absoluteAssetURL + "/assets/img/logo.png", "type": "image/png", "sizes": "512x512"}, + {"src": absoluteAssetURL + "/assets/img/logo.svg", "type": "image/svg+xml", "sizes": "512x512"}, + }, + }) + return template.URL("data:application/manifest+json;base64," + base64.StdEncoding.EncodeToString(manifest)) +} + func (c TemplateContext) ScriptImport(path string, typ ...string) template.HTML { if len(typ) > 0 { if typ[0] == "module" { diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 33789130f89..2f8c8be7581 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -4,7 +4,7 @@ {{ctx.HeadMetaContentSecurityPolicy}}