Skip to content

Commit 06a71e8

Browse files
committed
refactor: optimize theme initialization to prevent redundant animations and replace CDN-based MDB CSS with local assets
1 parent 9a85b5d commit 06a71e8

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

_includes/head.liquid

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717
<link rel="stylesheet" href="{{ '/assets/css/bootstrap.min.css' | relative_url | bust_file_cache }}">
1818
<link
1919
rel="stylesheet"
20-
href="{{ site.third_party_libraries.mdb.url.css }}"
21-
integrity="{{ site.third_party_libraries.mdb.integrity.css }}"
22-
crossorigin="anonymous"
20+
href="{{ '/assets/css/mdb.min.css' | relative_url | bust_file_cache }}"
2321
media="print"
2422
onload="this.media='all'"
2523
>
2624
<noscript>
2725
<link
2826
rel="stylesheet"
29-
href="{{ site.third_party_libraries.mdb.url.css }}"
30-
integrity="{{ site.third_party_libraries.mdb.integrity.css }}"
31-
crossorigin="anonymous"
27+
href="{{ '/assets/css/mdb.min.css' | relative_url | bust_file_cache }}"
3228
>
3329
</noscript>
3430

assets/js/theme.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@ let toggleThemeSetting = () => {
1313
};
1414

1515
// Change the theme setting and apply the theme.
16-
let setThemeSetting = (themeSetting) => {
17-
localStorage.setItem("theme", themeSetting);
16+
let setThemeSetting = (themeSetting, init = false) => {
17+
try {
18+
if (localStorage.getItem("theme") !== themeSetting) {
19+
localStorage.setItem("theme", themeSetting);
20+
}
21+
} catch (e) {
22+
console.debug("localStorage is not available:", e);
23+
}
1824

1925
document.documentElement.setAttribute("data-theme-setting", themeSetting);
2026

21-
applyTheme();
27+
applyTheme(init);
2228
};
2329

2430
// Apply the computed dark or light theme to the website.
25-
let applyTheme = () => {
31+
let applyTheme = (init = false) => {
2632
let theme = determineComputedTheme();
2733

28-
transTheme();
34+
if (!init) {
35+
transTheme();
36+
}
2937
setHighlight(theme);
3038
setGiscusTheme(theme);
3139
setSearchTheme(theme);
@@ -72,18 +80,24 @@ let applyTheme = () => {
7280
// Set jupyter notebooks themes.
7381
let jupyterNotebooks = document.getElementsByClassName("jupyter-notebook-iframe-container");
7482
for (let i = 0; i < jupyterNotebooks.length; i++) {
75-
let bodyElement = jupyterNotebooks[i].getElementsByTagName("iframe")[0].contentWindow.document.body;
76-
if (theme == "dark") {
77-
bodyElement.setAttribute("data-jp-theme-light", "false");
78-
bodyElement.setAttribute("data-jp-theme-name", "JupyterLab Dark");
79-
} else {
80-
bodyElement.setAttribute("data-jp-theme-light", "true");
81-
bodyElement.setAttribute("data-jp-theme-name", "JupyterLab Light");
83+
try {
84+
let bodyElement = jupyterNotebooks[i].getElementsByTagName("iframe")[0].contentWindow.document.body;
85+
if (bodyElement) {
86+
if (theme == "dark") {
87+
bodyElement.setAttribute("data-jp-theme-light", "false");
88+
bodyElement.setAttribute("data-jp-theme-name", "JupyterLab Dark");
89+
} else {
90+
bodyElement.setAttribute("data-jp-theme-light", "true");
91+
bodyElement.setAttribute("data-jp-theme-name", "JupyterLab Light");
92+
}
93+
}
94+
} catch (e) {
95+
console.warn("Could not access jupyter notebook iframe document:", e);
8296
}
8397
}
8498

8599
// Updates the background of medium-zoom overlay.
86-
if (typeof medium_zoom !== "undefined") {
100+
if (!init && typeof medium_zoom !== "undefined") {
87101
medium_zoom.update({
88102
background: getComputedStyle(document.documentElement).getPropertyValue("--global-bg-color") + "ee", // + 'ee' for trasparency.
89103
});
@@ -292,7 +306,7 @@ let determineComputedTheme = () => {
292306
let initTheme = () => {
293307
let themeSetting = determineThemeSetting();
294308

295-
setThemeSetting(themeSetting);
309+
setThemeSetting(themeSetting, true);
296310

297311
// Theme toggle button is removed - no event listener needed
298312
// Users cannot manually change the theme

0 commit comments

Comments
 (0)