We currently have a light/dark theme enabled on our WordPress site using cookies and a .dark class in CSS. It works, but makes using WP Total Cache almost not worth it as any post/page using the cookie cannot be page cached. I tried using localstorage instead, but was unable to get past an annoying "flash" when changing pages: each new navigation loads the default light css, then applies the dark css, resulting in a glimpse of the light theme before switching, very annoying. I was able to minimize the flash, but it's still there. I'm not finding any plugins that solve the light/dark theme question, and so far remain stuck using cookies and accepting that caching just isn't possible Does anyone have a working example of light/dark theme switching that works with caching, and actually works?
We currently have a light/dark theme enabled on our WordPress site using cookies and a .dark class in CSS. It works, but makes using WP Total Cache almost not worth it as any post/page using the cookie cannot be page cached. I tried using localstorage instead, but was unable to get past an annoying "flash" when changing pages: each new navigation loads the default light css, then applies the dark css, resulting in a glimpse of the light theme before switching, very annoying. I was able to minimize the flash, but it's still there. I'm not finding any plugins that solve the light/dark theme question, and so far remain stuck using cookies and accepting that caching just isn't possible Does anyone have a working example of light/dark theme switching that works with caching, and actually works?
You can try fragment caching (https://www.justinsilver/technology/wordpress/w3-total-cache-fragment-caching-wordpress/)
If this is your own theme (that you made) I have some suggestions. If the theme is third-party, I suggest making a child theme (lots of helpful advice on Google about this).
As you are using a cookie to pick light or dark, you should be able to hook the enqueued styles and dequeue the unwanted one with the style_loader_tag filter.
add_filter( 'style_loader_tag', 'fix_style_choice', 10, 4 );
function fix_style_choice( $html, $handle, $href, $media ){
// check cookies and only enqueue the right style (dequeue the wrong one).
}
My guess is this could sort out the problem.
As Tom Broucke points out, fragment caching could also help.
Another approach may be to give the two style sheets a very short (or a very long) cache time.
Finally, there is also a Javascript solution to the flash of unstyled content issue.