-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Website crashes when the value of darkMode in LocalStorage is undefined #145
Comments
Thanks @Talaxy009! |
@Talaxy009 May I know what plugin causes the issue? I recently found that similar issue but couldn't figure out why the value is replaced with "undefined"? I just updated Gatsby to version 5.7.0 and the issue come out. |
@tholyong I have written an article about this issue, but it is Chinese. Here's what I found, the problem happens because use-dark-mode listens to an input element (I think it's a problem with use-dark-mode because it should listen to If you're curious which element is causing it, you can locate the element with these codes: // save the orignal setItem method
const orignalSetItem = localStorage.setItem;
// rewrite setItem, it will dispatch a event when called
localStorage.setItem = function (key, newValue) {
let setItemEvent = new Event('setItemEvent');
setItemEvent.newValue = newValue;
setItemEvent.key = key;
window.dispatchEvent(setItemEvent);
orignalSetItem.apply(this, arguments);
};
// throw an error when the event dispatched
window.addEventListener('setItemEvent', function (e) {
throw e;
}); |
Thank you for the information and also the solution! This is exactly the issue I'm facing. I think I'm gonna write my own useDarkMode as you suggested. |
You are welcome |
I encountered a crash problem today and found that other plugins accidentally changed the value of darkMode to
undefined
. Although I've fixed the issue, the user has to clear the website cache to reset the drakMode value and get the website to work. So I thought it would be perfect if gatsby-plugin-use-dark-mode could handle this unexpected situation.The steps to reproduce:
localStorage.setItem('darkMode', 'undefined');
in the console to simulate unexpected situations.The text was updated successfully, but these errors were encountered: