mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-10-30 01:26:43 +08:00
feature (loading): optimise loading time
This commit is contained in:
@ -3,7 +3,6 @@ import { loadJS } from "../helpers/loader.js";
|
|||||||
import { init as setup_translation } from "../locales/index.js";
|
import { init as setup_translation } from "../locales/index.js";
|
||||||
import { init as setup_config } from "../model/config.js";
|
import { init as setup_config } from "../model/config.js";
|
||||||
import { init as setup_plugin } from "../model/plugin.js";
|
import { init as setup_plugin } from "../model/plugin.js";
|
||||||
import { init as setup_chromecast } from "../model/chromecast.js";
|
|
||||||
import { init as setup_cache } from "../pages/filespage/cache.js";
|
import { init as setup_cache } from "../pages/filespage/cache.js";
|
||||||
import { report } from "../helpers/log.js";
|
import { report } from "../helpers/log.js";
|
||||||
import { $error } from "./common.js";
|
import { $error } from "./common.js";
|
||||||
@ -12,7 +11,6 @@ export default async function main() {
|
|||||||
try {
|
try {
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
setup_config().then((config) => Promise.all([
|
setup_config().then((config) => Promise.all([
|
||||||
setup_chromecast(config),
|
|
||||||
setup_title(config),
|
setup_title(config),
|
||||||
window.self === window.top ? verify_origin(config) : verify_iframe_origin(config),
|
window.self === window.top ? verify_origin(config) : verify_iframe_origin(config),
|
||||||
])),
|
])),
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
export function init(config) {
|
import { get as getConfig } from "./config.js";
|
||||||
if (navigator.onLine === false) return Promise.resolve();
|
|
||||||
if (!config["enable_chromecast"]) {
|
|
||||||
return Promise.resolve();
|
|
||||||
} else if (!("chrome" in window)) {
|
|
||||||
return Promise.resolve();
|
|
||||||
} else if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
return Chromecast.init();
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Chromecast = new class ChromecastManager {
|
export const Chromecast = new class ChromecastManager {
|
||||||
init() {
|
init() {
|
||||||
|
if (navigator.onLine === false) return Promise.resolve();
|
||||||
|
if (!getConfig("enable_chromecast", false)) {
|
||||||
|
return Promise.resolve();
|
||||||
|
} else if (!("chrome" in window)) {
|
||||||
|
return Promise.resolve();
|
||||||
|
} else if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
if (document.head.querySelector("script#chromecast")) return resolve(null);
|
||||||
const script = document.createElement("script");
|
const script = document.createElement("script");
|
||||||
|
script.id = "chromecast";
|
||||||
script.src = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1";
|
script.src = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1";
|
||||||
script.onerror = () => resolve(null);
|
script.onerror = () => resolve(null);
|
||||||
window["__onGCastApiAvailable"] = function(isAvailable) {
|
window["__onGCastApiAvailable"] = function(isAvailable) {
|
||||||
|
|||||||
@ -27,7 +27,7 @@ import { ls as middlewareLs } from "./model_virtual_layer.js";
|
|||||||
|
|
||||||
const handleSuccess = (text) => rxjs.tap(() => notification.info(text));
|
const handleSuccess = (text) => rxjs.tap(() => notification.info(text));
|
||||||
const handleError = rxjs.catchError((err) => {
|
const handleError = rxjs.catchError((err) => {
|
||||||
notification.error(err);
|
notification.error(err.message);
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
const handleErrorRedirectLogin = rxjs.catchError((err) => {
|
const handleErrorRedirectLogin = rxjs.catchError((err) => {
|
||||||
|
|||||||
@ -402,7 +402,7 @@ export function mv(fromPath, toPath) {
|
|||||||
...file,
|
...file,
|
||||||
loading: false,
|
loading: false,
|
||||||
last: false,
|
last: false,
|
||||||
}
|
};
|
||||||
return file;
|
return file;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -114,7 +114,7 @@ export function init() {
|
|||||||
return Promise.all([
|
return Promise.all([
|
||||||
loadCSS(import.meta.url, "./application_image.css"),
|
loadCSS(import.meta.url, "./application_image.css"),
|
||||||
loadCSS(import.meta.url, "./component_menubar.css"),
|
loadCSS(import.meta.url, "./component_menubar.css"),
|
||||||
initPagination(), initInformation(),
|
initPagination(), initInformation(), Chromecast.init(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,8 +6,8 @@
|
|||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<meta name="application-name" content="Filestash">
|
<meta name="application-name" content="Filestash">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
|
||||||
<link rel="icon" href="favicon.ico">
|
|
||||||
<title></title>
|
<title></title>
|
||||||
|
<link rel="icon" href="{{ .favicon }}">
|
||||||
<script>{{ if eq .license "agpl" }}{{ template "loader-cat" }}{{ else }}{{ template "loader-basic" }}{{ end }}</script>
|
<script>{{ if eq .license "agpl" }}{{ template "loader-cat" }}{{ else }}{{ template "loader-basic" }}{{ end }}</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -60,7 +60,7 @@
|
|||||||
register.active.postMessage({
|
register.active.postMessage({
|
||||||
"type": "preload",
|
"type": "preload",
|
||||||
"payload": URLS,
|
"payload": URLS,
|
||||||
"version": "{{ .version }}".substring(0, 7) + "::{{ .hash }}",
|
"version": "{{ slice .version 0 7 }}::{{ .hash }}",
|
||||||
"clear": {{ .clear }},
|
"clear": {{ .clear }},
|
||||||
});
|
});
|
||||||
await new Promise((resolve, reject) => navigator.serviceWorker.addEventListener("message", (event) => {
|
await new Promise((resolve, reject) => navigator.serviceWorker.addEventListener("message", (event) => {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -226,6 +227,24 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
|
|||||||
|
|
||||||
return func(ctx *App, res http.ResponseWriter, req *http.Request) {
|
return func(ctx *App, res http.ResponseWriter, req *http.Request) {
|
||||||
head := res.Header()
|
head := res.Header()
|
||||||
|
sign := signature()
|
||||||
|
base := WithBase("/")
|
||||||
|
clear := req.Header.Get("Cache-Control") == "no-cache"
|
||||||
|
templateData := map[string]any{
|
||||||
|
"base": base,
|
||||||
|
"version": BUILD_REF,
|
||||||
|
"license": LICENSE,
|
||||||
|
"preload": preload(),
|
||||||
|
"clear": clear,
|
||||||
|
"hash": sign,
|
||||||
|
"favicon": favicon(),
|
||||||
|
}
|
||||||
|
calculatedEtag := QuickHash(base+BUILD_REF+LICENSE+fmt.Sprintf("%t", clear)+sign, 10)
|
||||||
|
head.Set("ETag", calculatedEtag)
|
||||||
|
if etag := req.Header.Get("If-None-Match"); etag == calculatedEtag {
|
||||||
|
res.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
var out io.Writer = res
|
var out io.Writer = res
|
||||||
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
|
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
head.Set("Content-Encoding", "gzip")
|
head.Set("Content-Encoding", "gzip")
|
||||||
@ -234,14 +253,7 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
|
|||||||
out = gz
|
out = gz
|
||||||
}
|
}
|
||||||
head.Set("Content-Type", "text/html")
|
head.Set("Content-Type", "text/html")
|
||||||
tmpl.Execute(out, map[string]any{
|
tmpl.Execute(out, templateData)
|
||||||
"base": WithBase("/"),
|
|
||||||
"version": BUILD_REF,
|
|
||||||
"license": LICENSE,
|
|
||||||
"preload": preload(),
|
|
||||||
"clear": req.Header.Get("Cache-Control") == "no-cache",
|
|
||||||
"hash": signature(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,3 +508,15 @@ func signature() string {
|
|||||||
}
|
}
|
||||||
return strings.ToLower(QuickHash(text, 3))
|
return strings.ToLower(QuickHash(text, 3))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func favicon() string {
|
||||||
|
file, err := WWWPublic.Open("/assets/logo/favicon.svg")
|
||||||
|
if err != nil {
|
||||||
|
return "favicon.ico"
|
||||||
|
}
|
||||||
|
f, err := io.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return "favicon.ico"
|
||||||
|
}
|
||||||
|
return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString(f)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user