feature (loadspeed): optimise load speed

This commit is contained in:
MickaelK 2025-09-23 12:28:51 +10:00
parent 99d539929e
commit 6299f8776f
2 changed files with 24 additions and 16 deletions

View file

@ -1,13 +1,15 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<base href="{{ .base }}">
<meta charset="UTF-8"> <meta charset="UTF-8">
<title></title>
<base href="{{ .base }}">
{{- range .bundle }}
<link rel="modulepreload" href="{{ . }}" />
{{- end }}
<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">
<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>
@ -34,16 +36,15 @@
<script type="module"> <script type="module">
async function ignitionSequence() { async function ignitionSequence() {
window.VERSION = "{{ slice .version 0 7 }}::{{ .hash }}";
try { try {
if (!HTMLScriptElement.supports?.("importmap")) throw new Error("fastboot is not supported on this platform"); if (!HTMLScriptElement.supports?.("importmap")) throw new Error("fastboot is not supported on this platform");
{{ load_asset "assets/boot/bundler_init.js" }} {{ load_asset "assets/boot/bundler_init.js" }}
await Promise.all(Array({{ .bundle_size }}).fill().map((_, i) => new Promise((resolve, reject) => document.head.appendChild(Object.assign(document.createElement("script"), { await Promise.all([
type: "module", {{- range .bundle }}
src: `./assets/bundle.js?version=${window.VERSION}&chunk=${i+1}`, "{{ . }}",
onload: resolve, {{- end }}
onerror: reject, ].map((src) => new Promise((onload, onerror) => document.head.appendChild(Object.assign(document.createElement("script"), {
type: "module", src, onload, onerror,
}))))); })))));
{{ load_asset "assets/boot/bundler_complete.js" }} {{ load_asset "assets/boot/bundler_complete.js" }}
} catch (err) { console.error(err); } } catch (err) { console.error(err); }

View file

@ -244,7 +244,14 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
"license": LICENSE, "license": LICENSE,
"hash": sign, "hash": sign,
"favicon": favicon(), "favicon": favicon(),
"bundle_size": len(preload), "bundle": func() []string {
b := make([]string, len(preload))
v := BUILD_REF[0:7] + "::" + sign
for i := 0; i < len(preload); i++ {
b[i] = fmt.Sprintf("./assets/bundle.js?version=%s&chunk=%d", v, i+1)
}
return b
}(),
} }
calculatedEtag := QuickHash(base+BUILD_REF+LICENSE+sign, 10) calculatedEtag := QuickHash(base+BUILD_REF+LICENSE+sign, 10)
head.Set("ETag", calculatedEtag) head.Set("ETag", calculatedEtag)