Skip to content

Commit

Permalink
Inline manifest.json (#14038)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Dec 22, 2020
1 parent cd607b5 commit 3a21f8a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 46 deletions.
2 changes: 2 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
13 changes: 13 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package setting
import (
"encoding/base64"
"fmt"
"html/template"
"io"
"io/ioutil"
"math"
Expand Down Expand Up @@ -293,6 +294,8 @@ var (
CSRFCookieName = "_csrf"
CSRFCookieHTTPOnly = true

ManifestData template.URL

// Mirror settings
Mirror struct {
DefaultInterval time.Duration
Expand Down Expand Up @@ -642,6 +645,8 @@ func NewContext() {
LandingPageURL = LandingPageHome
}

ManifestData = makeManifestData()

if len(SSH.Domain) == 0 {
SSH.Domain = Domain
}
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions routers/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ const (
tplQueue base.TplName = "admin/queue"
)

var (
startTime = time.Now()
)

var sysStatus struct {
Uptime string
NumGoroutine int
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 0 additions & 9 deletions routers/routes/macaron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}" />
Expand Down
31 changes: 0 additions & 31 deletions templates/pwa/manifest_json.tmpl

This file was deleted.

0 comments on commit 3a21f8a

Please sign in to comment.