Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

move theme setting to cookies to prevent flickering

Blue Mouse4a4b79f67842
diff --git a/app/redux/reducers/ui/theme.ts b/app/redux/reducers/ui/theme.ts
index 9e50bed87..1fbd0f600 100644
--- a/app/redux/reducers/ui/theme.ts
+++ b/app/redux/reducers/ui/theme.ts
@@ -1,9 +1,11 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import _includes from 'lodash/includes'
+import { setCookie, getCookie } from 'utils/cookie'
+import { isBrowser } from 'redux/constants'
import { LS_THEME_SETTING, SUPPORTED_THEMES, THEME_TYPE } from 'redux/constants'
const setThemeToDOM = (theme: string) => {
- if (typeof window === 'undefined') {
+ if (!isBrowser) {
return
}
@@ -16,28 +18,29 @@ const setThemeToDOM = (theme: string) => {
const setTheme = (theme: string): string => {
setThemeToDOM(theme)
- if (typeof window !== 'undefined' && window.localStorage) {
- window.localStorage.setItem(LS_THEME_SETTING, theme)
- }
+ setCookie(LS_THEME_SETTING, theme)
return theme
}
const getInitialTheme = (): 'light' | 'dark' => {
- if (typeof window !== 'undefined' && window.localStorage) {
- const lsTheme: any = window.localStorage.getItem(LS_THEME_SETTING)
- if (_includes(SUPPORTED_THEMES, lsTheme)) {
- setThemeToDOM(lsTheme)
- return lsTheme
- }
+ if (!isBrowser) {
+ return 'light'
+ }
+
+ const lsTheme: any = getCookie(LS_THEME_SETTING)
+
+ if (_includes(SUPPORTED_THEMES, lsTheme)) {
+ setThemeToDOM(lsTheme)
+ return lsTheme
+ }
- const userMedia = window.matchMedia('(prefers-color-scheme: dark)')
- if (userMedia.matches) {
- setThemeToDOM('dark')
- return 'dark'
- }
+ const userMedia = window.matchMedia('(prefers-color-scheme: dark)')
+ if (userMedia.matches) {
+ setTheme('dark')
+ return 'dark'
}
- setThemeToDOM('light')
+ setTheme('light')
return 'light' // light theme as the default
}
diff --git a/app/root.tsx b/app/root.tsx
index 2e3107ca1..c61183a00 100644
--- a/app/root.tsx
+++ b/app/root.tsx
@@ -54,22 +54,34 @@ export const links: LinksFunction = () => [
{ rel: 'stylesheet', href: FONTS_URL },
]
-const removeObsoleteAuthTokens = () => {
- const accessToken = getAccessToken()
- const refreshToken = getRefreshToken()
+// const removeObsoleteAuthTokens = () => {
+// const accessToken = getAccessToken()
+// const refreshToken = getRefreshToken()
- if (accessToken && !refreshToken) {
- removeAccessToken()
+// if (accessToken && !refreshToken) {
+// removeAccessToken()
+// }
+// }
+
+// removeObsoleteAuthTokens()
+
+export function detectTheme(request: Request): string {
+ const cookie = request.headers.get('Cookie')
+ const theme = cookie?.match(/(?<=colour-theme=)[^;]*/)?.[0]
+
+ if (theme === 'dark') {
+ return 'dark'
}
-}
-removeObsoleteAuthTokens()
+ return 'light'
+}
export async function loader({ request }: LoaderArgs) {
const { url } = request
const locale = detectLanguage(request)
+ const theme = detectTheme(request)
- return json({ locale, url })
+ return json({ locale, url, theme })
}
export const handle = {
@@ -77,7 +89,9 @@ export const handle = {
}
export default function App() {
- const { locale, url } = useLoaderData<typeof loader>()
+ const {
+ locale, url, theme,
+ } = useLoaderData<typeof loader>()
const { i18n } = useTranslation()
const alternateLinks = _map(whitelist, (locale) => ({
@@ -89,7 +103,7 @@ export default function App() {
useChangeLanguage(locale)
return (
- <html lang={locale} dir={i18n.dir()}>
+ <html className={theme} lang={locale} dir={i18n.dir()}>
<head>
<meta charSet='utf-8' />
<meta name='viewport' content='width=device-width,initial-scale=1' />