Remember chosen theme in localStorage

This way, the selected theme is kept when changing pages, and for subsequent visits. To prevent flickering, we set the selected theme in a blocking script directly on load. To speed things up further, we now use a `data-theme` attribute instead of classes on the body tag, this way we don't need to wait until the body element is loaded.
This commit is contained in:
Philipp Oppermann
2021-10-17 14:51:00 +02:00
parent 3281df3a41
commit 69917e234c
3 changed files with 44 additions and 39 deletions

View File

@@ -17,6 +17,13 @@
<link rel="alternate" type="application/rss+xml" title="RSS feed for os.phil-opp.com" href="{{ config.base_url | safe }}/rss.xml" />
<script>
let theme = localStorage.getItem("theme");
if (theme != null) {
document.documentElement.setAttribute("data-theme", theme);
}
</script>
<script async src="/js/edition-2/main.js"></script>
<title>{% block title %}{% endblock title %}</title>