Skip to content

Commit 6e0037b

Browse files
feature (loading): optimise loading time
1 parent f359ed8 commit 6e0037b

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

public/assets/boot/ctrl_boot_frontoffice.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { loadJS } from "../helpers/loader.js";
33
import { init as setup_translation } from "../locales/index.js";
44
import { init as setup_config } from "../model/config.js";
55
import { init as setup_plugin } from "../model/plugin.js";
6-
import { init as setup_chromecast } from "../model/chromecast.js";
76
import { init as setup_cache } from "../pages/filespage/cache.js";
87
import { report } from "../helpers/log.js";
98
import { $error } from "./common.js";
@@ -12,7 +11,6 @@ export default async function main() {
1211
try {
1312
await Promise.all([
1413
setup_config().then((config) => Promise.all([
15-
setup_chromecast(config),
1614
setup_title(config),
1715
window.self === window.top ? verify_origin(config) : verify_iframe_origin(config),
1816
])),

public/assets/model/chromecast.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
export function init(config) {
2-
if (navigator.onLine === false) return Promise.resolve();
3-
if (!config["enable_chromecast"]) {
4-
return Promise.resolve();
5-
} else if (!("chrome" in window)) {
6-
return Promise.resolve();
7-
} else if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
8-
return Promise.resolve();
9-
}
10-
return Chromecast.init();
11-
}
1+
import { get as getConfig } from "./config.js";
122

133
export const Chromecast = new class ChromecastManager {
144
init() {
5+
if (navigator.onLine === false) return Promise.resolve();
6+
if (!getConfig("enable_chromecast", false)) {
7+
return Promise.resolve();
8+
} else if (!("chrome" in window)) {
9+
return Promise.resolve();
10+
} else if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
11+
return Promise.resolve();
12+
}
1513
return new Promise((resolve) => {
14+
if (document.head.querySelector("script#chromecast")) return resolve(null);
1615
const script = document.createElement("script");
16+
script.id = "chromecast";
1717
script.src = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1";
1818
script.onerror = () => resolve(null);
1919
window["__onGCastApiAvailable"] = function(isAvailable) {

public/assets/pages/filespage/model_files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { ls as middlewareLs } from "./model_virtual_layer.js";
2727

2828
const handleSuccess = (text) => rxjs.tap(() => notification.info(text));
2929
const handleError = rxjs.catchError((err) => {
30-
notification.error(err);
30+
notification.error(err.message);
3131
throw err;
3232
});
3333
const handleErrorRedirectLogin = rxjs.catchError((err) => {

public/assets/pages/filespage/model_virtual_layer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ export function mv(fromPath, toPath) {
402402
...file,
403403
loading: false,
404404
last: false,
405-
}
405+
};
406406
return file;
407407
},
408408
});

public/assets/pages/viewerpage/application_image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function init() {
114114
return Promise.all([
115115
loadCSS(import.meta.url, "./application_image.css"),
116116
loadCSS(import.meta.url, "./component_menubar.css"),
117-
initPagination(), initInformation(),
117+
initPagination(), initInformation(), Chromecast.init(),
118118
]);
119119
}
120120

public/index.frontoffice.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<meta http-equiv="X-UA-Compatible" content="IE=edge">
77
<meta name="application-name" content="Filestash">
88
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
9-
<link rel="icon" href="favicon.ico">
109
<title></title>
10+
<link rel="icon" href="{{ .favicon }}">
1111
<script>{{ if eq .license "agpl" }}{{ template "loader-cat" }}{{ else }}{{ template "loader-basic" }}{{ end }}</script>
1212
</head>
1313
<body>
@@ -60,7 +60,7 @@
6060
register.active.postMessage({
6161
"type": "preload",
6262
"payload": URLS,
63-
"version": "{{ .version }}".substring(0, 7) + "::{{ .hash }}",
63+
"version": "{{ slice .version 0 7 }}::{{ .hash }}",
6464
"clear": {{ .clear }},
6565
});
6666
await new Promise((resolve, reject) => navigator.serviceWorker.addEventListener("message", (event) => {

server/ctrl/static.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"compress/gzip"
66
_ "embed"
7+
"encoding/base64"
78
"encoding/json"
89
"fmt"
910
"io"
@@ -226,6 +227,24 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
226227

227228
return func(ctx *App, res http.ResponseWriter, req *http.Request) {
228229
head := res.Header()
230+
sign := signature()
231+
base := WithBase("/")
232+
clear := req.Header.Get("Cache-Control") == "no-cache"
233+
templateData := map[string]any{
234+
"base": base,
235+
"version": BUILD_REF,
236+
"license": LICENSE,
237+
"preload": preload(),
238+
"clear": clear,
239+
"hash": sign,
240+
"favicon": favicon(),
241+
}
242+
calculatedEtag := QuickHash(base+BUILD_REF+LICENSE+fmt.Sprintf("%t", clear)+sign, 10)
243+
head.Set("ETag", calculatedEtag)
244+
if etag := req.Header.Get("If-None-Match"); etag == calculatedEtag {
245+
res.WriteHeader(http.StatusNotModified)
246+
return
247+
}
229248
var out io.Writer = res
230249
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
231250
head.Set("Content-Encoding", "gzip")
@@ -234,14 +253,7 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
234253
out = gz
235254
}
236255
head.Set("Content-Type", "text/html")
237-
tmpl.Execute(out, map[string]any{
238-
"base": WithBase("/"),
239-
"version": BUILD_REF,
240-
"license": LICENSE,
241-
"preload": preload(),
242-
"clear": req.Header.Get("Cache-Control") == "no-cache",
243-
"hash": signature(),
244-
})
256+
tmpl.Execute(out, templateData)
245257
}
246258
}
247259

@@ -496,3 +508,15 @@ func signature() string {
496508
}
497509
return strings.ToLower(QuickHash(text, 3))
498510
}
511+
512+
func favicon() string {
513+
file, err := WWWPublic.Open("/assets/logo/favicon.svg")
514+
if err != nil {
515+
return "favicon.ico"
516+
}
517+
f, err := io.ReadAll(file)
518+
if err != nil {
519+
return "favicon.ico"
520+
}
521+
return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString(f)
522+
}

0 commit comments

Comments
 (0)