From 5c3c038a41849e23cbd60fe8d222dbb394eedcdc Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Sat, 2 Apr 2022 10:54:45 -0700 Subject: [PATCH] docs(theme): wrap localStorage in try catch So that the site still works in Safari with all cookies blocked. --- docs/src/components/Theme.svelte | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/src/components/Theme.svelte b/docs/src/components/Theme.svelte index c58be048..c10ca7ee 100644 --- a/docs/src/components/Theme.svelte +++ b/docs/src/components/Theme.svelte @@ -9,14 +9,20 @@ const isValidTheme = (value) => themes.includes(value); onMount(() => { - const persisted_theme = localStorage.getItem(persistKey); - if (isValidTheme(persisted_theme)) theme.set(persisted_theme); + try { + const persisted_theme = localStorage.getItem(persistKey); + if (isValidTheme(persisted_theme)) theme.set(persisted_theme); + } catch (e) {} }); afterUpdate(() => { if (isValidTheme($theme)) { document.documentElement.setAttribute("theme", $theme); - if (persist) localStorage.setItem(persistKey, $theme); + if (persist) { + try { + localStorage.setItem(persistKey, $theme); + } catch (e) {} + } } });