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}} {{if .Title}}{{.Title}} - {{end}}{{.PageTitleCommon}} - + diff --git a/tests/e2e/site.test.ts b/tests/e2e/site.test.ts new file mode 100644 index 00000000000..cdd749fcdcd --- /dev/null +++ b/tests/e2e/site.test.ts @@ -0,0 +1,10 @@ +import {test, expect} from '@playwright/test'; + +test('browser parses the site manifest', async ({page, browserName}) => { + test.skip(browserName !== 'chromium', 'only CDP exposes the parsed manifest'); // eslint-disable-line playwright/no-skipped-test -- conditional skip, the reason is in the message + await page.goto('/'); + const cdp = await page.context().newCDPSession(page); + const {errors, manifest} = await cdp.send('Page.getAppManifest'); + expect(errors).toEqual([]); + expect(manifest.icons).not.toHaveLength(0); +}); diff --git a/tests/integration/view_test.go b/tests/integration/view_test.go index f2424a8049b..3b7c947b8a4 100644 --- a/tests/integration/view_test.go +++ b/tests/integration/view_test.go @@ -4,6 +4,7 @@ package integration import ( + "encoding/base64" "fmt" "net/http" "strings" @@ -17,6 +18,7 @@ import ( "gitea.dev/tests" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestVersion(t *testing.T) { @@ -104,11 +106,11 @@ func testSecurityHeadersDefaults(t *testing.T) { func testSiteManifest(t *testing.T) { req := NewRequest(t, "GET", "/") resp := MakeRequest(t, req, http.StatusOK) - assert.Contains(t, resp.Body.String(), ``) - - req = NewRequest(t, "GET", "/assets/site-manifest.json") - resp = MakeRequest(t, req, http.StatusOK) - assert.Equal(t, "application/manifest+json", resp.Header().Get("Content-Type")) + manifestURL, _ := NewHTMLParser(t, resp.Body).Find(`link[rel="manifest"]`).Attr("href") + manifestBase64, ok := strings.CutPrefix(manifestURL, "data:application/manifest+json;base64,") + require.True(t, ok) + manifestJSON, err := base64.StdEncoding.DecodeString(manifestBase64) + require.NoError(t, err) assetBase := strings.TrimSuffix(setting.AppURL, "/") expectedJSON := fmt.Sprintf(`{ @@ -126,7 +128,7 @@ func testSiteManifest(t *testing.T) { assetBase+"/assets/img/logo.png", assetBase+"/assets/img/logo.svg", ) - assert.JSONEq(t, expectedJSON, resp.Body.String()) + assert.JSONEq(t, expectedJSON, string(manifestJSON)) } func testViewPageCurrentURL(t *testing.T) {