From a3d6f1c844c1fbed27714a5d5def33ce479bb8ad Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 23 Mar 2020 11:18:10 +1000 Subject: [PATCH] minify interpreted templates save bytes in init.js --- common/template_init.go | 29 ++++++++++++++++++++++++++--- common/theme.go | 1 + public/init.js | 40 ++++++++++++++++++++-------------------- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/common/template_init.go b/common/template_init.go index 0c80f004..3d5c3076 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -4,6 +4,7 @@ import ( "fmt" "html/template" "io" + "io/ioutil" "log" "path/filepath" "runtime" @@ -903,9 +904,31 @@ func loadTemplates(t *template.Template, themeName string) error { } } - template.Must(t.ParseFiles(tFiles...)) - template.Must(t.ParseGlob("pages/*")) - return nil + // TODO: Minify these + /*err = t.ParseFiles(tFiles...) + if err != nil { + return err + }*/ + for _, fname := range tFiles { + b, err := ioutil.ReadFile(fname) + if err != nil { + return err + } + s := tmpl.Minify(string(b)) + name := filepath.Base(fname) + var tmpl *template.Template + if name == t.Name() { + tmpl = t + } else { + tmpl = t.New(name) + } + _, err = tmpl.Parse(s) + if err != nil { + return err + } + } + _, err = t.ParseGlob("pages/*") + return err } func InitTemplates() error { diff --git a/common/theme.go b/common/theme.go index 5bb78cbd..504b513e 100644 --- a/common/theme.go +++ b/common/theme.go @@ -118,6 +118,7 @@ func (t *Theme) LoadStaticFiles() error { return out } t.ResourceTemplates.Funcs(fmap) + // TODO: Minify these template.Must(t.ResourceTemplates.ParseGlob("./themes/" + t.Name + "/public/*.css")) // It should be safe for us to load the files for all the themes in memory, as-long as the admin hasn't setup a ridiculous number of themes diff --git a/public/init.js b/public/init.js index 53f470ff..0385273f 100644 --- a/public/init.js +++ b/public/init.js @@ -107,7 +107,7 @@ function notifyOnScriptW(name,complete,success) { complete(); if(success!==undefined) success(); }).catch(e => { - console.log("Unable to get script name '"+name+"'",e); + console.log("Unable to get '"+name+"'",e); console.trace(); complete(e); }); @@ -125,12 +125,12 @@ function loadScript(name,callback,fail) { asyncGetScript(url) .then(callback) .catch(e => { - console.log("Unable to get script '"+url+"'"); + console.log("Unable to get '"+url+"'"); if(fname!=name) { asyncGetScript(iurl) .then(callback) .catch(e => { - console.log("Unable to get script '"+iurl+"'",e); + console.log("Unable to get '"+iurl+"'",e); console.trace(); }); } @@ -140,20 +140,20 @@ function loadScript(name,callback,fail) { }); } -function RelativeTime(date) {return date;} +function RelativeTime(date) {return date} -function initPhrases(loggedIn, panel=false) { - console.log("in initPhrases") +function initPhrases(member, acp=false) { + console.log("initPhrases") console.log("tmlInits",tmplInits) let e = ""; - if(loggedIn && !panel) e = ",status,topic_list,topic"; - else if(panel) e = ",analytics,panel"; // TODO: Request phrases for just one section of the control panel? + if(member && !acp) e = ",status,topic_list,topic"; + else if(acp) e = ",analytics,panel"; // TODO: Request phrases for just one section of the cp? else e = ",status,topic_list"; fetchPhrases("alerts,paginator"+e) // TODO: Break this up? } function fetchPhrases(plist) { - fetch("/api/phrases/?q="+plist, {cache:"no-cache"}) + fetch("/api/phrases/?q="+plist,{cache:"no-cache"}) .then(resp => resp.json()) .then(data => { console.log("loaded phrase endpoint data"); @@ -185,20 +185,20 @@ function fetchPhrases(plist) { (() => { runInitHook("pre_iife"); - let loggedIn = document.head.querySelector("[property='x-mem']")!=null; - let panel = window.location.pathname.startsWith("/panel/"); + let member = document.head.querySelector("[property='x-mem']")!=null; + let acp = window.location.pathname.startsWith("/panel/"); let toLoad = 1; - // TODO: Shunt this into loggedIn if there aren't any search and filter widgets? + // TODO: Shunt this into member if there aren't any search and filter widgets? let q = (f) => { toLoad--; - if(toLoad===0) initPhrases(loggedIn,panel); - if(f) throw("template function not found"); + if(toLoad===0) initPhrases(member,acp); + if(f) throw("tmpl func not found"); }; - if(!panel) { + if(!acp) { toLoad += 2; - if(loggedIn) { + if(member) { toLoad += 3; notifyOnScriptW("tmpl_topic_c_edit_post", () => q(!Tmpl_topic_c_edit_post)); notifyOnScriptW("tmpl_topic_c_attach_item", () => q(!Tmpl_topic_c_attach_item)); @@ -209,16 +209,16 @@ function fetchPhrases(plist) { } notifyOnScriptW("tmpl_notice", () => q(!Tmpl_notice)); - if(loggedIn) { + if(member) { fetch("/api/me/") .then(resp => resp.json()) .then(data => { - console.log("loaded me endpoint data",data); - me = data; + console.log("me data",data); + me=data; runInitHook("pre_init"); }); } else { - me = {User:{ID:0,S:""},Site:{"MaxRequestSize":0}}; + me={User:{ID:0,S:""},Site:{"MaxRequestSize":0}}; runInitHook("pre_init"); } })() \ No newline at end of file