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

Optimization for page load theme-change delay #28

Open
farukcan opened this issue Dec 19, 2022 · 3 comments
Open

Optimization for page load theme-change delay #28

farukcan opened this issue Dec 19, 2022 · 3 comments

Comments

@farukcan
Copy link

farukcan commented Dec 19, 2022

We must change theme before pageload event to avoid theme changing delay.

  1. Step
if (typeof exports != "undefined") {
    module.exports = {
        themeChange: themeChange
    }
} else {
    var selectedTheme = localStorage.getItem("theme"); // ADD line 1
    if(selectedTheme){ // ADD line 2
        document.documentElement.setAttribute("data-theme",selectedTheme); // // ADD line 3
    } // // ADD line 4
    themeChange(true);
}
  1. Step
    Add async attribute. And add this element to <head> . It must be first loaded script that no need wait for page load.
    <script async crossorigin="anonymous" src="./theme-change.js"></script>

Thanks for your library!

@saadeghi
Copy link
Owner

Thank you my friend.
I will try this soon.

It's a challenge because on one hand, we don't want to block the page loading by the script, on the other hand, we want to apply the theme before page loading...

Also, many people instead of using a <script> tag, want to install it as a NPM package and import it with React, Vue, etc.

@JieyueCoding
Copy link

Thank you all. Following Mr. Farukcan's comment, I temporarily added the following code to the <head> of my index.html. This also avoids delays.

<script async crossorigin="anonymous">
var selectedTheme = localStorage.getItem("theme");
if(selectedTheme) {
    document.documentElement.setAttribute("data-theme", selectedTheme);
}
</script>

@jamaluddinrumi
Copy link

jamaluddinrumi commented Jul 12, 2023

hi @JieyueCoding it does not work for me

i mean, it need to be checked from blank tab each, not from already visited website

the glitch is still there

can i see your demo, please?

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

No branches or pull requests

4 participants