Skip to content

Conversation

jacobsfletch
Copy link
Member

@jacobsfletch jacobsfletch commented May 22, 2024

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 to window.watchMedia(‘(prefers-color-scheme: dark)’).matches. We then set the data-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 the data-theme data-attribute on the root element directly. If no cookie is detected, we now accept and read the experimental Sec-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).

  • I have read and understand the CONTRIBUTING.md document in this repository.

@jacobsfletch jacobsfletch requested a review from denolfe as a code owner May 22, 2024 12:31
@jacobsfletch jacobsfletch changed the title feat(next): ssr theme detection feat(next): server-side theme detection May 22, 2024
@jacobsfletch jacobsfletch merged commit 1fe9790 into beta May 22, 2024
@jacobsfletch jacobsfletch deleted the feat/ssr-theme branch May 22, 2024 14:19
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

Successfully merging this pull request may close these issues.

1 participant