Skip to content

Commit

Permalink
fix: ignore DOMException by accessing localStorage (#123)
Browse files Browse the repository at this point in the history
* fix: ignore DOMException by accessing localStorage

* chore: use as Theme instead of ts-expect-error
  • Loading branch information
koba04 committed Mar 23, 2023
1 parent fccbd09 commit bf93b8c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/swr-devtools-panel/src/components/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ const THEME_KEY = "$swr-devtools:theme";

const initialTheme = (): Theme => {
if (typeof window === "undefined") return "system";
// @ts-expect-error
return localStorage.getItem(THEME_KEY) || "system";
let theme: Theme = "system";
try {
theme = localStorage.getItem(THEME_KEY) as Theme;
} catch {
// noop
}
return theme;
};

const getThemeByPreference = () =>
Expand Down Expand Up @@ -42,7 +47,11 @@ export const ThemeProvider = ({ children }: { children: React.ReactNode }) => {
setTheme(initialTheme());
}, []);
useEffect(() => {
localStorage.setItem(THEME_KEY, theme);
try {
localStorage.setItem(THEME_KEY, theme);
} catch {
// noop
}
}, [theme]);
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
Expand Down

1 comment on commit bf93b8c

@vercel
Copy link

@vercel vercel bot commented on bf93b8c Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

swr-devtools – ./

swr-devtools-git-main-koba04.vercel.app
swr-devtools-koba04.vercel.app
swr-devtools.vercel.app

Please sign in to comment.