-
Notifications
You must be signed in to change notification settings - Fork 3.4k
PR comments: add try catch while accessing theme from localStorage #12682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -171,16 +171,20 @@ ga('send', 'pageview'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // avoid a flash of the initial theme on load. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raw(''' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (storedTheme === 'auto-mode') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.body.classList.add( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'auto-mode', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.body.classList.add(storedTheme); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (storedTheme === 'auto-mode') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.body.classList.add( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'auto-mode', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.body.classList.add(storedTheme); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+175
to
+185
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a minor performance improvement and to scope variables more tightly, you can move the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // localStorage is not available, do nothing and fallback to default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @parlough I added the changes in the rest of the files. For now I have added some print statements in debug mode. Please feel free to direct me if we need to perform other actions there.
Also, it'd be great if you can give tips on testing this.