Move towards loading the important scripts asynchronously.

Removed the client's reliance on the server's Site.URL setting.
This commit is contained in:
Azareal 2018-08-13 11:53:16 +10:00
parent a9a1d667c0
commit 317ab8856b
3 changed files with 18 additions and 16 deletions

View File

@ -276,7 +276,7 @@ function len(item) {
}
function loadScript(name, callback) {
let url = "//" +me.Site.URL+"/static/"+name
let url = "/static/"+name
$.getScript(url)
.done(callback)
.fail((e,xhr,settings,ex) => {
@ -294,7 +294,7 @@ function DoNothingButPassBack(item) {
}
function fetchPhrases() {
fetch("//" +me.Site.URL+"/api/phrases/?query=status,topic_list")
fetch("/api/phrases/?query=status,topic_list")
.then((resp) => resp.json())
.then((data) => {
console.log("loaded phrase endpoint data");
@ -327,7 +327,9 @@ function fetchPhrases() {
}
(() => {
runHook("pre_me");
runHook("pre_iife");
let loggedIn = document.head.querySelector("[property='x-loggedin']").content;
fetch("/api/me/")
.then((resp) => resp.json())
.then((data) => {
@ -336,15 +338,6 @@ function fetchPhrases() {
me = data;
runHook("pre_init");
if(me.User.ID > 0) {
let toLoad = 1;
loadScript("template_topics_topic.js", () => {
console.log("Loaded template_topics_topic.js");
toLoad--;
if(toLoad===0) fetchPhrases();
});
}
// We can only get away with this because template_alert has no phrases, otherwise it too would have to be part of the "dance", I miss Go concurrency :(
loadScript("template_alert.js", () => {
console.log("Loaded template_alert.js");
@ -360,6 +353,15 @@ function fetchPhrases() {
$(document).ready(mainInit);
});
if(loggedIn) {
let toLoad = 1;
loadScript("template_topics_topic.js", () => {
console.log("Loaded template_topics_topic.js");
toLoad--;
if(toLoad===0) fetchPhrases();
});
}
})();
function mainInit(){
@ -667,8 +669,8 @@ function mainInit(){
console.log("content.value", content.value);
let attachItem;
if(content.value == "") attachItem = "//" + me.Site.URL + "/attachs/" + hash + "." + ext;
else attachItem = "\r\n//" + me.Site.URL + "/attachs/" + hash + "." + ext;
if(content.value == "") attachItem = "//" + window.location.host + "/attachs/" + hash + "." + ext;
else attachItem = "\r\n//" + window.location.host + "/attachs/" + hash + "." + ext;
content.value = content.value + attachItem;
console.log("content.value", content.value);

View File

@ -229,7 +229,6 @@ type JsonMe struct {
// We don't want to expose too much information about the site, so we'll make this a small subset of common.site
type MeSite struct {
URL string
MaxRequestSize int
}
@ -243,7 +242,7 @@ func APIMe(w http.ResponseWriter, r *http.Request, user common.User) common.Rout
// TODO: Use this header anywhere with a user check?
w.Header().Set("Cache-Control", "private")
me := JsonMe{(&user).Me(), MeSite{common.Site.URL, common.Site.MaxRequestSize}}
me := JsonMe{(&user).Me(), MeSite{common.Site.MaxRequestSize}}
jsonBytes, err := json.Marshal(me)
if err != nil {

View File

@ -11,6 +11,7 @@
{{range .Header.Scripts}}
<script type="text/javascript" src="/static/{{.}}"></script>
{{end}}
<meta property="x-loggedin" content="{{.CurrentUser.Loggedin}}" />
<script type="text/javascript" src="/static/global.js"></script>
<meta name="viewport" content="width=device-width,initial-scale = 1.0, maximum-scale=1.0,user-scalable=no" />
{{if .Header.MetaDesc}}<meta name="description" content="{{.Header.MetaDesc}}" />{{end}}