N°7264 - Improve async load of CSS files to avoid duplicates in the webpage.

Based on the same mechanism that was made for JS files.
This commit is contained in:
Molkobain
2024-04-03 16:15:49 +02:00
parent a4aa494e5d
commit f7230de6d6
2 changed files with 43 additions and 13 deletions

View File

@@ -70,7 +70,7 @@
* ```
*/
// If these constants aren't defined by the main page, define them (global) ourselves
// If these constants aren't defined by the main page, define them (global) ourselves
if (typeof aLoadedJsFilesRegister === "undefined") {
Object.defineProperty(window, "aLoadedJsFilesRegister", {
value: new Map(),
@@ -186,11 +186,25 @@
{% endif %}
{% block iboPageCssFiles %}
{% for aCssFileData in aPage.aCssFiles %}
<script type="text/javascript">
if (!$('link[href="{{ aCssFileData.link }}"]').length) $('<link href="{{ aCssFileData.link }}" rel="stylesheet">').appendTo('head');
</script>
{% endfor %}
<script type="text/javascript">
// If this constant isn't defined by the main page, define it (global) ourselves
if (typeof aLoadedCssFilesRegister === "undefined") {
Object.defineProperty(window, "aLoadedCssFilesRegister", {
value: new Map(),
writable: false,
configurable: false,
enumerable: true
});
}
{% for aCssFileData in aPage.aCssFiles %}
// Only if file is NOT already present in the register (see it declaration in WebPage TWIG template), add it to the page and register
if (aLoadedCssFilesRegister.has("{{ aCssFileData['link']|raw }}") === false) {
$('<link href="{{ aCssFileData['link'] }}" rel="stylesheet">').appendTo('head');
aLoadedCssFilesRegister.set("{{ aCssFileData['link']|raw }}", true);
}
{% endfor %}
</script>
{% endblock %}
{{ aPage.sCapturedOutput|raw }}