Skip to content

Commit 3a21f8a

Browse files
silverwindlunnyzeripath
authored
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 <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent cd607b5 commit 3a21f8a

File tree

6 files changed

+17
-46
lines changed

6 files changed

+17
-46
lines changed

modules/context/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ func Contexter() macaron.Handler {
345345
ctx.Data["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
346346
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
347347

348+
ctx.Data["ManifestData"] = setting.ManifestData
349+
348350
c.Map(ctx)
349351
}
350352
}

modules/setting/setting.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package setting
88
import (
99
"encoding/base64"
1010
"fmt"
11+
"html/template"
1112
"io"
1213
"io/ioutil"
1314
"math"
@@ -293,6 +294,8 @@ var (
293294
CSRFCookieName = "_csrf"
294295
CSRFCookieHTTPOnly = true
295296

297+
ManifestData template.URL
298+
296299
// Mirror settings
297300
Mirror struct {
298301
DefaultInterval time.Duration
@@ -642,6 +645,8 @@ func NewContext() {
642645
LandingPageURL = LandingPageHome
643646
}
644647

648+
ManifestData = makeManifestData()
649+
645650
if len(SSH.Domain) == 0 {
646651
SSH.Domain = Domain
647652
}
@@ -1040,6 +1045,14 @@ func loadOrGenerateInternalToken(sec *ini.Section) string {
10401045
return token
10411046
}
10421047

1048+
func makeManifestData() template.URL {
1049+
name := url.QueryEscape(AppName)
1050+
prefix := url.QueryEscape(StaticURLPrefix)
1051+
subURL := url.QueryEscape(AppSubURL) + "/"
1052+
1053+
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"}`)
1054+
}
1055+
10431056
// NewServices initializes the services
10441057
func NewServices() {
10451058
InitDBConfig()

routers/admin/admin.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ const (
3939
tplQueue base.TplName = "admin/queue"
4040
)
4141

42-
var (
43-
startTime = time.Now()
44-
)
45-
4642
var sysStatus struct {
4743
Uptime string
4844
NumGoroutine int
@@ -85,7 +81,7 @@ var sysStatus struct {
8581
}
8682

8783
func updateSystemStatus() {
88-
sysStatus.Uptime = timeutil.TimeSincePro(startTime, "en")
84+
sysStatus.Uptime = timeutil.TimeSincePro(setting.AppStartTime, "en")
8985

9086
m := new(runtime.MemStats)
9187
runtime.ReadMemStats(m)

routers/routes/macaron.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ package routes
66

77
import (
88
"encoding/gob"
9-
"net/http"
109

1110
"code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/modules/auth"
1312
"code.gitea.io/gitea/modules/context"
14-
"code.gitea.io/gitea/modules/httpcache"
1513
"code.gitea.io/gitea/modules/lfs"
1614
"code.gitea.io/gitea/modules/log"
1715
"code.gitea.io/gitea/modules/options"
@@ -977,13 +975,6 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
977975
private.RegisterRoutes(m)
978976
})
979977

980-
// Progressive Web App
981-
m.Get("/manifest.json", templates.JSONRenderer(), func(ctx *context.Context) {
982-
ctx.Resp.Header().Set("Cache-Control", httpcache.GetCacheControl())
983-
ctx.Resp.Header().Set("Last-Modified", setting.AppStartTime.Format(http.TimeFormat))
984-
ctx.HTML(200, "pwa/manifest_json")
985-
})
986-
987978
// Not found handler.
988979
m.NotFound(routers.NotFound)
989980
}

templates/base/head.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<meta http-equiv="x-ua-compatible" content="ie=edge">
77
<title>{{if .Title}}{{.Title | RenderEmojiPlain}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}} </title>
8-
<link rel="manifest" href="{{AppSubUrl}}/manifest.json" crossorigin="use-credentials">
8+
<link rel="manifest" href="{{.ManifestData}}"/>
99
<meta name="theme-color" content="{{ThemeColorMetaTag}}">
1010
<meta name="default-theme" content="{{DefaultTheme}}" />
1111
<meta name="author" content="{{if .Repository}}{{.Owner.Name}}{{else}}{{MetaAuthor}}{{end}}" />

templates/pwa/manifest_json.tmpl

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)