Skip to content
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

Closed
Talaxy009 opened this issue Feb 13, 2023 · 5 comments · Fixed by #146
Closed

Website crashes when the value of darkMode in LocalStorage is undefined #145

Talaxy009 opened this issue Feb 13, 2023 · 5 comments · Fixed by #146

Comments

@Talaxy009
Copy link

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:

  • Running localStorage.setItem('darkMode', 'undefined'); in the console to simulate unexpected situations.
  • Reloading the website then you should be able to see something like the image below:

image

@wKovacs64
Copy link
Owner

Thanks @Talaxy009!

@tholyong
Copy link

tholyong commented Mar 6, 2023

@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.

@Talaxy009
Copy link
Author

@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 prefers-color-scheme media but it doesn't), when the input value change, it will set the localStorage as undefined because the event object is different. To fix it you can remove all @media(prefers-color-scheme: dark) styles in your site or write your "use-dark-mode", I write mine here, the use method is different but the base logic is same so the gatsby-plugin-use-dark-mode still work.

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;
});

and you might see something like this:
ss02

@tholyong
Copy link

tholyong commented Mar 6, 2023

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.

@Talaxy009
Copy link
Author

You are welcome☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants