Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Main and Inquire pages: load scripts after every stylesheet, and paint the last known theme background before the stylesheets load

Murtaza Nasir3d4a88890cac
diff --git a/templates/includes/theme_bootstrap.html b/templates/includes/theme_bootstrap.html
index 404e85a5..9285638f 100644
--- a/templates/includes/theme_bootstrap.html
+++ b/templates/includes/theme_bootstrap.html
@@ -11,5 +11,20 @@
: !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
root.classList.toggle('dark', dark);
if (scheme && scheme !== 'blue') root.classList.add((dark ? 'theme-dark-' : 'theme-light-') + scheme);
+ // Until the stylesheets are loaded, the page background is the browser's
+ // default (white). The background last seen with this mode and scheme is
+ // used for that moment, and saved again once the page has loaded.
+ var key = 'themeBg:' + (dark ? 'dark' : 'light') + ':' + (scheme || 'blue');
+ try {
+ var bg = localStorage.getItem(key);
+ if (bg) {
+ var style = document.createElement('style');
+ style.textContent = 'html{background-color:' + bg.replace(/[^#(),.%\w\s]/g, '') + '}';
+ document.head.appendChild(style);
+ }
+ } catch (e) {}
+ window.addEventListener('load', function () {
+ try { localStorage.setItem(key, getComputedStyle(document.body).backgroundColor); } catch (e) {}
+ });
})();
</script>
diff --git a/templates/index.html b/templates/index.html
index 43c88463..bf2e4c9f 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -60,14 +60,8 @@
<meta name="app-version" content="{{ app_version }}">
<meta name="robots" content="noindex, nofollow, noarchive, nosnippet, noimageindex">
<title>Speakr - Audio Transcription & Summarization</title>
- {% include 'includes/loading_overlay.html' %}
<!-- All dependencies bundled locally for offline support -->
- <script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>
- <script src="{{ url_for('static', filename='vendor/js/marked.min.js') }}"></script>
- <script src="{{ url_for('static', filename='vendor/js/purify.min.js') }}"></script>
- <script src="{{ url_for('static', filename='js/markdown-safe.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/easymde.min.css') }}">
- <script src="{{ url_for('static', filename='vendor/js/easymde.min.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/fontawesome.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/primitives.css') }}">
@@ -81,6 +75,14 @@
<head> so that, as with the former runtime build, they follow the page's
own stylesheets in the cascade. -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/tailwind.css') }}">
+ <!-- Scripts after every stylesheet, so that no page is painted while a
+ script blocks the parser before the stylesheets are known. -->
+ {% include 'includes/loading_overlay.html' %}
+ <script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>
+ <script src="{{ url_for('static', filename='vendor/js/marked.min.js') }}"></script>
+ <script src="{{ url_for('static', filename='vendor/js/purify.min.js') }}"></script>
+ <script src="{{ url_for('static', filename='js/markdown-safe.js') }}"></script>
+ <script src="{{ url_for('static', filename='vendor/js/easymde.min.js') }}"></script>
</head>
<body class="h-full bg-[var(--bg-primary)] text-[var(--text-primary)] transition-colors duration-300">
<div id="loader" class="fixed inset-0 bg-gray-800 flex items-center justify-center z-50">
diff --git a/templates/inquire.html b/templates/inquire.html
index ff47c2af..05dd663b 100644
--- a/templates/inquire.html
+++ b/templates/inquire.html
@@ -22,22 +22,15 @@
<title>Inquire Mode - Speakr</title>
<!-- All dependencies bundled locally for offline support -->
<!-- All dependencies bundled locally for offline support -->
-<script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>
<!-- All dependencies bundled locally for offline support -->
-<script src="{{ url_for('static', filename='vendor/js/marked.min.js') }}"></script>
-<script src="{{ url_for('static', filename='vendor/js/purify.min.js') }}"></script>
-<script src="{{ url_for('static', filename='js/markdown-safe.js') }}"></script>
<!-- All dependencies bundled locally for offline support -->
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/fontawesome.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/primitives.css') }}">
- {% include 'includes/i18n_bootstrap.html' %}
- <script src="{{ url_for('static', filename='js/i18n.js') }}"></script>
<link rel="icon" href="{{ url_for('static', filename='img/favicon.ico') }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ url_for('static', filename='img/icon-32x32.png') }}">
<!-- Loading overlay to prevent FOUC -->
- {% include 'includes/loading_overlay.html' %}
<style>
/* Custom checkbox styling for better visual feedback */
@@ -204,6 +197,15 @@
<head> so that, as with the former runtime build, they follow the page's
own stylesheets in the cascade. -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/tailwind.css') }}">
+ <!-- Scripts after every stylesheet, so that no page is painted while a
+ script blocks the parser before the stylesheets are known. -->
+ <script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>
+ <script src="{{ url_for('static', filename='vendor/js/marked.min.js') }}"></script>
+ <script src="{{ url_for('static', filename='vendor/js/purify.min.js') }}"></script>
+ <script src="{{ url_for('static', filename='js/markdown-safe.js') }}"></script>
+ {% include 'includes/i18n_bootstrap.html' %}
+ <script src="{{ url_for('static', filename='js/i18n.js') }}"></script>
+ {% include 'includes/loading_overlay.html' %}
</head>
<body class="h-full bg-[var(--bg-primary)] text-[var(--text-primary)] transition-colors duration-300">
<div id="inquire-app" v-cloak class="h-full flex flex-col"
diff --git a/tests/test_page_assets.py b/tests/test_page_assets.py
index 28d4bfa4..fa7b5c17 100644
--- a/tests/test_page_assets.py
+++ b/tests/test_page_assets.py
@@ -182,3 +182,17 @@ def test_theme_is_applied_before_first_paint(page):
if THEME_INCLUDE in head:
first_css = head.find('rel="stylesheet"')
assert first_css == -1 or head.find(THEME_INCLUDE) < first_css, f"{page}: theme include after a stylesheet"
+
+
[email protected]("page", ["index.html", "inquire.html"])
+def test_scripts_follow_the_stylesheets(page):
+ """No external script blocks the parser before the stylesheets are known.
+
+ While such a script was loading, some browsers painted the default white
+ canvas, which showed as a white flash before a dark page.
+ """
+ head = _read(page).split("</head>")[0]
+ last_css = max(m.start() for m in re.finditer(r'rel="stylesheet"', head))
+ early = [m.group(0) for m in re.finditer(r"<script src=[^>]*>|\{% include '[^']*' %\}", head)
+ if m.start() < last_css and "theme_bootstrap" not in m.group(0)]
+ assert not early, f"{page}: before the last stylesheet: {early}"