There is an experimental theme component that does the automatic switch based on media queries:
Because window.matchMedia
is not available server-side, as you note, the component evaluates in JS whether prefers-color-scheme
matches the current theme, and if not, sets the correct theme and reloads the site with the updated theme.