feat(next): server-side theme detection #6452
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
We need to detect the user’s theme server-side in effort to achieve better loading states. This way we can immediately show meaningful content to the user while the components stream in.
Currently, we set
opacity: 0
on the entire admin panel until the theme has been detected. We do this by searching in local storage and subscribing towindow.watchMedia(‘(prefers-color-scheme: dark)’).matches
. We then set thedata-theme
data-attribute on the root<html>
element through a DOM query selector (not great). If the user changes their theme, we affect this change in local storage.Now, we no longer save the preferred theme in local storage and instead use a first-party cookie, which can be read server-side in the top-level
layout.tsx
. There, we can set thedata-theme
data-attribute on the root element directly. If no cookie is detected, we now accept and read the experimentalSec-CH-Prefers-Color-Scheme
request header to detect the user's preferred color scheme. For browsers that do not support this header yet, they will simply fallback to light mode. Client-side media query detection still occurs in case the user changes their OS theme in the background (pretty cool).