docs(theme): wrap localStorage in try catch

So that the site still works in Safari with all cookies blocked.
This commit is contained in:
Eric Liu 2022-04-02 10:54:45 -07:00
commit 5c3c038a41

View file

@ -9,14 +9,20 @@
const isValidTheme = (value) => themes.includes(value); const isValidTheme = (value) => themes.includes(value);
onMount(() => { onMount(() => {
const persisted_theme = localStorage.getItem(persistKey); try {
if (isValidTheme(persisted_theme)) theme.set(persisted_theme); const persisted_theme = localStorage.getItem(persistKey);
if (isValidTheme(persisted_theme)) theme.set(persisted_theme);
} catch (e) {}
}); });
afterUpdate(() => { afterUpdate(() => {
if (isValidTheme($theme)) { if (isValidTheme($theme)) {
document.documentElement.setAttribute("theme", $theme); document.documentElement.setAttribute("theme", $theme);
if (persist) localStorage.setItem(persistKey, $theme); if (persist) {
try {
localStorage.setItem(persistKey, $theme);
} catch (e) {}
}
} }
}); });
</script> </script>