1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-06-18 07:27:49 +02:00

Fix server push and use Link header to load CSS

Nginx with http2_push_preload on will convert the Link header to server
pushes, so we use it to load css.
This commit is contained in:
Harvey Tindall 2021-01-15 18:57:12 +00:00
parent ca0889aaab
commit ffc62574ec
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 33 additions and 10 deletions

View File

@ -49,6 +49,7 @@ For [docker](https://hub.docker.com/repository/docker/hrfee/jfa-go), run:
docker create \
--name "jfa-go" \ # Whatever you want to name it
-p 8056:8056 \
# -p 8057:8057 if using tls
-v /path/to/.config/jfa-go:/data \ # Path to wherever you want to store the config file and other data
-v /path/to/jellyfin:/jf \ # Path to jellyfin config directory
-v /etc/localtime:/etc/localtime:ro \ # Makes sure time is correct

View File

@ -232,7 +232,7 @@
"requires_restart": true,
"type": "bool",
"value": false,
"description": "Enable TLS, and by extension HTTP2. This enables server push, where required files are pushed to the web browser before they request them, allowing quicker page loads."
"description": "Enable TLS."
},
"tls_port": {
"name": "TLS Port",

View File

@ -2,7 +2,6 @@
<html lang="en" class="{{ .cssClass }}">
<head>
<link rel="stylesheet" type="text/css" href="css/base.css">
<script>
window.URLBase = "{{ .urlBase }}";
window.notificationsEnabled = {{ .notifications }};

View File

@ -7,12 +7,41 @@ import (
"github.com/gin-gonic/gin"
)
var css = []string{"base.css", "a17t.css", "remixicon.css", "modal.css", "dark.css", "tooltip.css", "loader.css"}
var cssHeader = func() string {
l := len(css)
h := ""
for i, f := range css {
h += "</css/" + f + ">; rel=preload; as=style"
if l > 1 && i != (l-1) {
h += ", "
}
}
return h
}()
func gcHTML(gc *gin.Context, code int, file string, templ gin.H) {
gc.Header("Cache-Control", "no-cache")
gc.HTML(code, file, templ)
}
func (app *appContext) pushResources(gc *gin.Context, admin bool) {
if pusher := gc.Writer.Pusher(); pusher != nil {
app.debug.Println("Using HTTP2 Server push")
if admin {
toPush := []string{"/js/admin.js", "/js/theme.js", "/js/lang.js", "/js/modal.js", "/js/tabs.js", "/js/invites.js", "/js/accounts.js", "/js/settings.js", "/js/profiles.js", "/js/common.js"}
for _, f := range toPush {
if err := pusher.Push(f, nil); err != nil {
app.debug.Printf("Failed HTTP2 ServerPush of \"%s\": %+v", f, err)
}
}
}
}
gc.Header("Link", cssHeader)
}
func (app *appContext) AdminPage(gc *gin.Context) {
app.pushResources(gc, true)
lang := gc.Query("lang")
if lang == "" {
lang = app.storage.lang.chosenAdminLang
@ -22,14 +51,6 @@ func (app *appContext) AdminPage(gc *gin.Context) {
emailEnabled, _ := app.config.Section("invite_emails").Key("enabled").Bool()
notificationsEnabled, _ := app.config.Section("notifications").Key("enabled").Bool()
ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false)
if pusher := gc.Writer.Pusher(); pusher != nil {
toPush := []string{"/js/admin.js", "/js/theme.js", "/js/lang.js", "/js/modal.js", "/js/tabs.js", "/js/invites.js", "/js/accounts.js", "/js/settings.js", "/js/profiles.js", "/js/common.js"}
for _, f := range toPush {
if err := pusher.Push(f, nil); err != nil {
app.debug.Printf("Failed HTTP2 ServerPush of \"%s\": %+v", f, err)
}
}
}
gcHTML(gc, http.StatusOK, "admin.html", gin.H{
"urlBase": app.URLBase,
"cssClass": app.cssClass,
@ -47,6 +68,7 @@ func (app *appContext) AdminPage(gc *gin.Context) {
}
func (app *appContext) InviteProxy(gc *gin.Context) {
app.pushResources(gc, false)
code := gc.Param("invCode")
lang := gc.Query("lang")
if lang == "" {
@ -84,6 +106,7 @@ func (app *appContext) InviteProxy(gc *gin.Context) {
}
func (app *appContext) NoRouteHandler(gc *gin.Context) {
app.pushResources(gc, false)
gcHTML(gc, 404, "404.html", gin.H{
"cssClass": app.cssClass,
"contactMessage": app.config.Section("ui").Key("contact_message").String(),