diff --git a/assets/darkmode.js b/assets/darkmode.js
index 93bf6dacf..d95a281ac 100644
--- a/assets/darkmode.js
+++ b/assets/darkmode.js
@@ -1,14 +1,8 @@
-// Darkmode toggle
-const toggleSwitch = document.querySelector('#darkmode-toggle')
-
const userPref = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'
const currentTheme = localStorage.getItem('theme') ?? userPref
if (currentTheme) {
document.documentElement.setAttribute('saved-theme', currentTheme);
- if (currentTheme === 'dark') {
- toggleSwitch.checked = true
- }
}
const switchTheme = (e) => {
@@ -22,5 +16,14 @@ const switchTheme = (e) => {
}
}
-// listen for toggle
-toggleSwitch.addEventListener('change', switchTheme, false)
\ No newline at end of file
+window.addEventListener('DOMContentLoaded', () => {
+ // Darkmode toggle
+ const toggleSwitch = document.querySelector('#darkmode-toggle')
+
+ // listen for toggle
+ toggleSwitch.addEventListener('change', switchTheme, false)
+
+ if (currentTheme === 'dark') {
+ toggleSwitch.checked = true
+ }
+})
diff --git a/layouts/404.html b/layouts/404.html
index 849491011..deec3eb68 100644
--- a/layouts/404.html
+++ b/layouts/404.html
@@ -11,12 +11,6 @@
↳ Let's get you home.
-
-{{- with resources.Get "darkmode.js" | minify -}}
-
-{{- end -}}