fix (caching): retrocompatibility over previous release

This commit is contained in:
MickaelK 2024-07-22 17:27:06 +10:00
parent 2f8b615e6e
commit 4f26bb265a
6 changed files with 24 additions and 38 deletions

View file

@ -1,4 +1,3 @@
import { init as initCSS } from "../helpers/loader.js";
import { report } from "../helpers/log.js";
import { $error } from "./common.js";
@ -8,7 +7,6 @@ export default async function main() {
setup_device(),
setup_blue_death_screen(),
setup_history(),
setup_css(),
]);
window.dispatchEvent(new window.Event("pagechange"));
} catch (err) {
@ -35,7 +33,3 @@ async function setup_blue_death_screen() {
async function setup_history() {
window.history.replaceState({}, "");
}
async function setup_css() {
return initCSS();
}

View file

@ -1,5 +1,5 @@
import { toHref } from "../lib/skeleton/router.js";
import { init as setup_loader, loadJS } from "../helpers/loader.js";
import { loadJS } from "../helpers/loader.js";
import { init as setup_translation } from "../locales/index.js";
import { init as setup_config } from "../model/config.js";
import { init as setup_chromecast } from "../model/chromecast.js";
@ -14,7 +14,6 @@ export default async function main() {
setup_device(),
// setup_sw(), // TODO
setup_blue_death_screen(),
setup_loader(),
setup_history(),
]);
@ -69,7 +68,7 @@ async function setup_device() {
});
}
async function setup_sw() {
async function setup_sw() { // eslint-disable-line no-unused-vars
if (!("serviceWorker" in window.navigator)) return;
if (window.navigator.userAgent.indexOf("Mozilla/") !== -1 &&
@ -93,7 +92,7 @@ async function setup_blue_death_screen() {
if ("serviceWorker" in navigator) navigator.serviceWorker
.getRegistrations()
.then((registrations) => {
for (let registration of registrations) {
for (const registration of registrations) {
registration.unregister();
}
});

View file

@ -1,10 +1,6 @@
import { get as getRelease } from "../pages/adminpage/model_release.js";
let version = null;
export async function loadJS(baseURL, path, opts = {}) {
const $script = document.createElement("script");
const link = new URL(path, baseURL) + (version ? "?version=" + version : "");
const link = new URL(path, baseURL);
$script.setAttribute("src", link.toString());
for (const key in opts) {
$script.setAttribute(key, opts[key]);
@ -19,7 +15,7 @@ export async function loadJS(baseURL, path, opts = {}) {
export async function loadCSS(baseURL, path) {
const $style = document.createElement("link");
const link = new URL(path, baseURL) + "?version=" + version;
const link = new URL(path, baseURL);
$style.setAttribute("href", link.toString());
$style.setAttribute("rel", "stylesheet");
if (document.head.querySelector(`[href="${link.toString()}"]`)) return Promise.resolve();
@ -31,7 +27,7 @@ export async function loadCSS(baseURL, path) {
}
export async function loadCSSInline(baseURL, filename) {
const res = await fetch(new URL(filename, baseURL).pathname + `?version=${version}`, {
const res = await fetch(new URL(filename, baseURL).pathname, {
cache: "force-cache",
});
if (res.status !== 200) return `/* ERROR: ${res.status} */`;
@ -43,8 +39,3 @@ export async function CSS(baseURL, ...arrayOfFilenames) {
const sheets = await Promise.all(arrayOfFilenames.map((filename) => loadCSSInline(baseURL, filename)));
return sheets.join("\n\n");
}
export async function init() {
const info = await getRelease().toPromise();
version = info.version;
}

View file

@ -5,12 +5,12 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="admin/assets/css/designsystem.css">
<link rel="stylesheet" href="./admin/assets/css/designsystem.css">
<link rel="icon" href="favicon.ico">
<title>Admin Console</title>
</head>
<body>
<script type="module" src="admin/assets/{{ .version }}/components/loader.js"></script>
<script type="module" src="./admin/assets/{{ .version }}/components/loader.js"></script>
<div role="main" id="app">
<component-loader delay="500"></component-loader>
</div>
@ -20,13 +20,13 @@
import routes from "./admin/assets/{{ .version }}/boot/router_backoffice.js";
main(document.getElementById("app"), routes, {
spinner: `<component-loader></component-loader>`,
beforeStart: import("./admin/assets/{{ .version }}/boot/ctrl_boot_backoffice.js"),
beforeStart: import("{{ .base }}admin/assets/{{ .version }}/boot/ctrl_boot_backoffice.js"),
});
</script>
<script type="module" src="admin/assets/{{ .version }}/components/modal.js"></script>
<script type="module" src="./admin/assets/{{ .version }}/components/modal.js"></script>
<component-modal></component-modal>
<script type="module" src="admin/assets/{{ .version }}/components/notification.js"></script>
<script type="module" src="./admin/assets/{{ .version }}/components/notification.js"></script>
<component-notification></component-notification>
<noscript>

View file

@ -5,13 +5,13 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<link rel="stylesheet" href="assets/{{ .version }}/css/designsystem.css">
<link rel="stylesheet" href="./assets/{{ .version }}/css/designsystem.css">
<link rel="stylesheet" href="custom.css">
<link rel="icon" href="assets/{{ .version}}/logo/favicon.svg">
{{ if eq .license "agpl" }}<link rel="icon" href="./assets/{{ .version}}/logo/favicon.svg">{{ else }}<link rel="icon" href="favicon.ico">{{ end }}
<title></title>
</head>
<body>
<script type="module" src="assets/{{ .version }}/components/loader.js"></script>
<script type="module" src="./assets/{{ .version }}/components/loader.js"></script>
<div role="main" id="app">
<component-loader delay="500"></component-loader>
</div>
@ -20,7 +20,7 @@
import routes from "./assets/{{ .version }}/boot/router_frontoffice.js";
main(document.getElementById("app"), routes, {
spinner: `<component-loader></component-loader>`,
beforeStart: import("./assets/{{ .version }}/boot/ctrl_boot_frontoffice.js"),
beforeStart: import("{{ .base }}assets/{{ .version }}/boot/ctrl_boot_frontoffice.js"),
});
</script>

View file

@ -194,7 +194,7 @@ func ServeBackofficeHandler(ctx *App, res http.ResponseWriter, req *http.Request
head.Add(
"Link",
fmt.Sprintf(`<%s>; rel="preload"; as="script"; crossorigin="anonymous";`, WithBase(
strings.Replace(href, "/assets/", "/assets/"+APP_VERSION+"/", 1),
strings.Replace(href, "/assets/", "/assets/"+version()+"/", 1),
)),
)
}
@ -289,7 +289,7 @@ func ServeFrontofficeHandler(ctx *App, res http.ResponseWriter, req *http.Reques
head.Add(
"Link",
fmt.Sprintf(`<%s>; rel="preload"; as="script"; crossorigin="anonymous";`, WithBase(
strings.Replace(href, "/assets/", "/assets/"+APP_VERSION+"/", 1),
strings.Replace(href, "/assets/", "/assets/"+version()+"/", 1),
)),
)
}
@ -431,7 +431,7 @@ func ServeFile(chroot string) func(*App, http.ResponseWriter, *http.Request) {
chroot,
strings.Replace(
TrimBase(req.URL.Path),
"assets/"+APP_VERSION+"/",
"assets/"+version()+"/",
"assets/",
1,
),
@ -515,9 +515,6 @@ func ServeFile(chroot string) func(*App, http.ResponseWriter, *http.Request) {
if cfg.ContentType != "" {
head.Set("Content-Encoding", cfg.ContentType)
}
head.Set("Cache-Control", "no-cache")
head.Set("Pragma", "no-cache")
head.Set("Expires", "0")
res.WriteHeader(http.StatusOK)
io.Copy(res, file)
file.Close()
@ -549,7 +546,8 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
res.WriteHeader(http.StatusOK)
template.Must(template.New(indexPath).Parse(string(b))).Execute(res, map[string]any{
"base": WithBase("/"),
"version": APP_VERSION,
"version": version(),
"license": LICENSE,
})
}
}
@ -584,3 +582,7 @@ func InitPluginList(code []byte) {
}
}
}
func version() string {
return BUILD_REF[:7]
}