summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2020-12-22 12:13:50 +0100
committerGitHub <noreply@github.com>2020-12-22 19:13:50 +0800
commit3a21f8a9865d1112d1b847fb20710e6b735174ec (patch)
treea0dd55fa92457fec77dabc6d3e83311ec3af8bb9
parentcd607b5f98ccf66acd99a47309a3411a8e5c8993 (diff)
downloadgitea-3a21f8a9865d1112d1b847fb20710e6b735174ec.tar.gz
gitea-3a21f8a9865d1112d1b847fb20710e6b735174ec.zip
Inline manifest.json (#14038)
* Inline manifest.json Improve performance by eliminating this separate request and just inline this small JSON in HTML directly as a data uri. Also update previously static app name scripts to use AppName. I've confirmed this as working via "Add to Homescreen" feature which offered to save the shortcut under the new app name. * prerender manifest data on startup * move to settings * restore setting.AppStartTime and use it on admin page * use double quotes because template.URL escapes everything * fix lint * move variable to global context variable * delete template file Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: zeripath <art27@cantab.net>
-rw-r--r--modules/context/context.go2
-rw-r--r--modules/setting/setting.go13
-rw-r--r--routers/admin/admin.go6
-rw-r--r--routers/routes/macaron.go9
-rw-r--r--templates/base/head.tmpl2
-rw-r--r--templates/pwa/manifest_json.tmpl31
6 files changed, 17 insertions, 46 deletions
diff --git a/modules/context/context.go b/modules/context/context.go
index c6597cf024..2e43088ff8 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -345,6 +345,8 @@ func Contexter() macaron.Handler {
ctx.Data["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
+ ctx.Data["ManifestData"] = setting.ManifestData
+
c.Map(ctx)
}
}
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 63ae15af97..290ec94c44 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -8,6 +8,7 @@ package setting
import (
"encoding/base64"
"fmt"
+ "html/template"
"io"
"io/ioutil"
"math"
@@ -293,6 +294,8 @@ var (
CSRFCookieName = "_csrf"
CSRFCookieHTTPOnly = true
+ ManifestData template.URL
+
// Mirror settings
Mirror struct {
DefaultInterval time.Duration
@@ -642,6 +645,8 @@ func NewContext() {
LandingPageURL = LandingPageHome
}
+ ManifestData = makeManifestData()
+
if len(SSH.Domain) == 0 {
SSH.Domain = Domain
}
@@ -1040,6 +1045,14 @@ func loadOrGenerateInternalToken(sec *ini.Section) string {
return token
}
+func makeManifestData() template.URL {
+ name := url.QueryEscape(AppName)
+ prefix := url.QueryEscape(StaticURLPrefix)
+ subURL := url.QueryEscape(AppSubURL) + "/"
+
+ return template.URL(`data:application/json,{"short_name":"` + name + `","name":"` + name + `","icons":[{"src":"` + prefix + `/img/logo-lg.png","type":"image/png","sizes":"880x880"},{"src":"` + prefix + `/img/logo-sm.png","type":"image/png","sizes":"120x120"},{"src":"` + prefix + `/img/logo-512.png","type":"image/png","sizes":"512x512"},{"src":"` + prefix + `/img/logo-192.png","type":"image/png","sizes":"192x192"}],"start_url":"` + subURL + `","scope":"` + subURL + `","background_color":"%23FAFAFA","display":"standalone"}`)
+}
+
// NewServices initializes the services
func NewServices() {
InitDBConfig()
diff --git a/routers/admin/admin.go b/routers/admin/admin.go
index d0f027f5f8..4180076b08 100644
--- a/routers/admin/admin.go
+++ b/routers/admin/admin.go
@@ -39,10 +39,6 @@ const (
tplQueue base.TplName = "admin/queue"
)
-var (
- startTime = time.Now()
-)
-
var sysStatus struct {
Uptime string
NumGoroutine int
@@ -85,7 +81,7 @@ var sysStatus struct {
}
func updateSystemStatus() {
- sysStatus.Uptime = timeutil.TimeSincePro(startTime, "en")
+ sysStatus.Uptime = timeutil.TimeSincePro(setting.AppStartTime, "en")
m := new(runtime.MemStats)
runtime.ReadMemStats(m)
diff --git a/routers/routes/macaron.go b/routers/routes/macaron.go
index 170bc7d493..16977b9470 100644
--- a/routers/routes/macaron.go
+++ b/routers/routes/macaron.go
@@ -6,12 +6,10 @@ package routes
import (
"encoding/gob"
- "net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/options"
@@ -977,13 +975,6 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
private.RegisterRoutes(m)
})
- // Progressive Web App
- m.Get("/manifest.json", templates.JSONRenderer(), func(ctx *context.Context) {
- ctx.Resp.Header().Set("Cache-Control", httpcache.GetCacheControl())
- ctx.Resp.Header().Set("Last-Modified", setting.AppStartTime.Format(http.TimeFormat))
- ctx.HTML(200, "pwa/manifest_json")
- })
-
// Not found handler.
m.NotFound(routers.NotFound)
}
diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl
index 51d60c46f8..99b942bcf4 100644
--- a/templates/base/head.tmpl
+++ b/templates/base/head.tmpl
@@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>{{if .Title}}{{.Title | RenderEmojiPlain}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}} </title>
- <link rel="manifest" href="{{AppSubUrl}}/manifest.json" crossorigin="use-credentials">
+ <link rel="manifest" href="{{.ManifestData}}"/>
<meta name="theme-color" content="{{ThemeColorMetaTag}}">
<meta name="default-theme" content="{{DefaultTheme}}" />
<meta name="author" content="{{if .Repository}}{{.Owner.Name}}{{else}}{{MetaAuthor}}{{end}}" />
diff --git a/templates/pwa/manifest_json.tmpl b/templates/pwa/manifest_json.tmpl
deleted file mode 100644
index 7eba1f45bb..0000000000
--- a/templates/pwa/manifest_json.tmpl
+++ /dev/null
@@ -1,31 +0,0 @@
-{
- "short_name": "Gitea",
- "name": "Gitea - Git with a cup of tea",
- "icons": [
- {
- "src": "{{StaticUrlPrefix}}/img/logo-lg.png",
- "type": "image/png",
- "sizes": "880x880"
- },
- {
- "src": "{{StaticUrlPrefix}}/img/logo-sm.png",
- "type": "image/png",
- "sizes": "120x120"
- },
- {
- "src": "{{StaticUrlPrefix}}/img/logo-512.png",
- "type": "image/png",
- "sizes": "512x512"
- },
- {
- "src": "{{StaticUrlPrefix}}/img/logo-192.png",
- "type": "image/png",
- "sizes": "192x192"
- }
- ],
- "start_url": "{{AppSubUrl}}/",
- "scope": "{{AppSubUrl}}/",
- "background_color": "#FAFAFA",
- "display": "standalone",
- "theme_color": "{{ThemeColorMetaTag}}"
-}