From 3c5484a7c273aaffdf025fc293383ce5f960d3d0 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sat, 24 Aug 2024 01:36:49 +0200 Subject: [PATCH] main.js: load styles with dyanamically provided resource version. This is done because: - If the version is updated before the rebuild, someone might cache the old version in the meanwhile. - One could not rebuild javascript before updating the version. Now it's possible. --- templates/main.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/templates/main.js b/templates/main.js index 13c80c33..82c87094 100644 --- a/templates/main.js +++ b/templates/main.js @@ -141,7 +141,33 @@ function changeStyle(styleName, link) { x.appendChild(s); } - document.getElementById('stylesheet').href = styles[styleName]; + let mainStylesheetElement = document.getElementById('stylesheet'); + let userStylesheetElement = document.getElementById('stylesheet-user'); + + // Override main stylesheet with the user selected one. + if (!userStylesheetElement) { + userStylesheetElement = document.createElement('link'); + userStylesheetElement.rel = 'stylesheet'; + userStylesheetElement.media = 'none'; + userStylesheetElement.type = 'text/css'; + userStylesheetElement.id = 'stylesheet'; + let x = document.getElementsByTagName('head')[0]; + x.appendChild(userStylesheetElement); + } + + // When the new one is loaded, disable the old one + userStylesheetElement.onload = function() { + this.media = 'all'; + mainStylesheetElement.media = 'none'; + } + + let style = styles[styleName]; + if (style !== '') { + // Add the version of the resource if the style is not the embedded one. + style += `?v=${resourceVersion}`; + } + + document.getElementById('stylesheet').href = style; selectedstyle = styleName; if (document.getElementsByClassName('styles').length != 0) { @@ -162,6 +188,7 @@ function changeStyle(styleName, link) { {% endverbatim %} +var resourceVersion = document.currentScript.getAttribute('data-resource-version'); {% if config.stylesheets_board %} {% verbatim %}