Blocking cookies breaks withDarkMode component
See original GitHub issueDescription
The withDarkMode
component uses the localStorage API directly, which causes errors when the browser blocks access to those APIs. This causes most of the site to not display correctly.
withDarkMode
provides information about the current mode of the theme to various components around the site. It does this through checking localStorage for the darkMode
key and using the use-dark-mode
hook. When cookies are blocked by the browser, it blocks our access to certain browser APIs such as localStorage and causes this component to throw an uncaught error. This error comes from our attempt to access localStorage https://github.com/newrelic/opensource-website/blob/develop/src/components/withDarkMode.js#L10 and the use-dark-mode
hook.
Solution
- Wrap the call to localStorage in a try/catch.
- Tell
use-dark-mode
to use an in-memory store when localStorage isn’t available, similar to how the theme does it.
Steps to Reproduce
Run the opensource site and block all cookies on your browser.
Expected Behavior
The core content loads without needing access to the localStorage.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
@rudouglas I’m taking myself off this one. I put a
closes
keyword on your PR, but if you get a chance you may want to relink it. No worries at all - you didn’t step on my toes, and I was glad to see your solution. Thanks for coordinating!Sorry @elaguerta didn’t see your comment, ignore my PR 😬