Set color-scheme in an early stylesheet and put the theme class on the html element before first paint. With a strict CSP you can use a hashed external script.
Not fully for a user preference stored in localStorage. But prefers-color-scheme in CSS handles the OS setting with zero flash, then JS only overrides explicit user choice.
3 replies