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!

fixed a few hydration errors

Blue Mousef7de873ced60
diff --git a/app/App.tsx b/app/App.tsx
index 82d4ea561..c05519440 100644
--- a/app/App.tsx
+++ b/app/App.tsx
@@ -5,7 +5,6 @@ import { useLocation, Outlet } from '@remix-run/react'
import { useSelector } from 'react-redux'
// @ts-ignore
import { useAlert } from '@blaumaus/react-alert'
-// import Snowfall from 'react-snowfall'
import cx from 'clsx'
import _some from 'lodash/some'
import _includes from 'lodash/includes'
@@ -19,7 +18,7 @@ import Loader from 'ui/Loader'
import ScrollToTop from 'hoc/ScrollToTop'
import Selfhosted from 'hoc/Selfhosted'
import {
- isSelfhosted, PADDLE_JS_URL, PADDLE_VENDOR_ID, // THEME_TYPE,
+ isSelfhosted, PADDLE_JS_URL, PADDLE_VENDOR_ID,
} from 'redux/constants'
import { getAccessToken } from 'utils/accessToken'
import { loadScript } from 'utils/generic'
@@ -68,7 +67,11 @@ const Fallback = ({ isMinimalFooter }: FallbackProps): JSX.Element => {
)
}
-const App = () => {
+interface IApp {
+ ssrTheme: 'dark' | 'light'
+}
+
+const App: React.FC<IApp> = ({ ssrTheme }) => {
const dispatch = useAppDispatch()
const { pathname } = useLocation()
const alert = useAlert()
@@ -78,7 +81,6 @@ const App = () => {
const paddleLoaded = useSelector((state: StateType) => state.ui.misc.paddleLoaded)
const { error } = useSelector((state: StateType) => state.errors)
const { message, type } = useSelector((state: StateType) => state.alerts)
- // const themeType = useSelector(state => state.ui.theme.type)
const accessToken = getAccessToken()
const refreshToken = getRefreshToken()
@@ -131,7 +133,6 @@ const App = () => {
if ((accessToken && refreshToken) && !authenticated) {
try {
const me = await authMe()
- // dispatch(UIActions.setThemeType(me.theme))
dispatch(authActions.loginSuccessful(me))
dispatch(authActions.finishLoading())
} catch (e) {
@@ -171,14 +172,8 @@ const App = () => {
// eslint-disable-next-line react/jsx-no-useless-fragment
<Suspense fallback={<></>}>
{pathname !== routesPath.main && (
- <Header />
+ <Header ssrTheme={ssrTheme} />
)}
- {/* {location.pathname === routes.main && (
- <Snowfall />
- )} */}
- {/* {location.pathname !== routes.main && themeType === THEME_TYPE.christmas && (
- <Snowfall snowflakeCount={10} />
- )} */}
{/* @ts-ignore */}
<ScrollToTop>
<Selfhosted>
@@ -190,7 +185,6 @@ const App = () => {
<Footer minimal={isMinimalFooter} authenticated={authenticated} />
</Suspense>
)}
- <div className='hidden' />
</>
)
}
diff --git a/app/components/Header/index.tsx b/app/components/Header/index.tsx
index 9530d68d9..3ab235389 100644
--- a/app/components/Header/index.tsx
+++ b/app/components/Header/index.tsx
@@ -28,7 +28,7 @@ import sagaActions from 'redux/sagas/actions'
import UIActions from 'redux/reducers/ui'
import {
whitelist, languages, languageFlag, isSelfhosted, BLOG_URL,
- DOCS_URL, SUPPORTED_THEMES,
+ DOCS_URL, SUPPORTED_THEMES, isBrowser,
} from 'redux/constants'
import Dropdown from 'ui/Dropdown'
import { IUser } from 'redux/models/IUser'
@@ -571,7 +571,11 @@ const NotAuthedHeader = ({
</header>
)
-const Header = (): JSX.Element => {
+interface IHeader {
+ ssrTheme: 'dark' | 'light'
+}
+
+const Header: React.FC<IHeader> = ({ ssrTheme }): JSX.Element => {
const { t, i18n: { language } }: {
t: (key: string, options?: {
[key: string]: string | number | null
@@ -581,7 +585,7 @@ const Header = (): JSX.Element => {
const dispatch = useAppDispatch()
const _dispatch = useDispatch()
const { authenticated, user } = useSelector((state: StateType) => state.auth)
- const { theme } = useSelector((state: StateType) => state.ui.theme)
+ const theme = isBrowser ? useSelector((state: StateType) => state.ui.theme.theme) : ssrTheme
const { pathname } = useLocation()
// @ts-ignore
const buttonRef: MutableRefObject<HTMLButtonElement> = useRef<HTMLButtonElement>()
diff --git a/app/pages/MainPage/index.tsx b/app/pages/MainPage/index.tsx
index 523fd0fae..0dbe5fc96 100644
--- a/app/pages/MainPage/index.tsx
+++ b/app/pages/MainPage/index.tsx
@@ -16,7 +16,7 @@ import _isEmpty from 'lodash/isEmpty'
import routesPath from 'routesPath'
import { nFormatterSeparated } from 'utils/generic'
-import { GITHUB_URL, MARKETPLACE_URL, LIVE_DEMO_URL } from 'redux/constants'
+import { GITHUB_URL, MARKETPLACE_URL, LIVE_DEMO_URL, isBrowser } from 'redux/constants'
import { StateType } from 'redux/store/index'
import BackgroundSvg from 'ui/icons/BackgroundSvg'
import Webflow from 'ui/icons/Webflow'
@@ -34,6 +34,7 @@ import Gatsby from 'ui/icons/Gatsby'
import Wix from 'ui/icons/Wix'
import Header from 'components/Header'
+import { detectTheme } from 'utils/server'
import SignUp from '../Auth/Signup/BasicSignup'
import Pricing from './Pricing'
@@ -135,7 +136,11 @@ const M_FEATURES_ICONS = [
<ShareIcon className='w-5 h-5' key='ShareIcon' />,
]
-const Main = (): JSX.Element => {
+interface IMain {
+ ssrTheme: 'dark' | 'light'
+}
+
+const Main: React.FC<IMain> = ({ ssrTheme }): JSX.Element => {
const { t, i18n: { language } }: {
t: (key: string, options?: {
[key: string]: any
@@ -144,7 +149,7 @@ const Main = (): JSX.Element => {
language: string
},
} = useTranslation('common')
- const { theme } = useSelector((state: StateType) => state.ui.theme)
+ const theme = isBrowser ? useSelector((state: StateType) => state.ui.theme.theme) : ssrTheme
const { authenticated } = useSelector((state: StateType) => state.auth)
const { stats, lastBlogPost } = useSelector((state: StateType) => state.ui.misc)
@@ -193,7 +198,7 @@ const Main = (): JSX.Element => {
}}
/>
</div>
- <Header />
+ <Header ssrTheme={ssrTheme} />
<div className='flex justify-center items-center py-2 px-2'>
<a
href='https://bank.gov.ua/en/news/all/natsionalniy-bank-vidkriv-spetsrahunok-dlya-zboru-koshtiv-na-potrebi-armiyi'
diff --git a/app/root.tsx b/app/root.tsx
index 39aad1714..8ba711492 100644
--- a/app/root.tsx
+++ b/app/root.tsx
@@ -23,6 +23,7 @@ import { useChangeLanguage } from 'remix-i18next'
import { useTranslation } from 'react-i18next'
import AppWrapper from 'App'
import { detectLanguage } from 'i18n'
+import { detectTheme } from 'utils/server'
import mainCss from 'styles/index.css'
import tailwindCss from 'styles/tailwind.css'
@@ -73,17 +74,6 @@ export const links: LinksFunction = () => [
// 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'
- }
-
- return 'light'
-}
-
export async function loader({ request }: LoaderArgs) {
const { url } = request
const locale = detectLanguage(request)
@@ -134,7 +124,7 @@ export default function App() {
</div>
<Provider store={store}>
<AlertProvider template={AlertTemplate} {...options}>
- <AppWrapper />
+ <AppWrapper ssrTheme={theme} />
<ScrollRestoration />
<Scripts />
{isDevelopment && <LiveReload />}
diff --git a/app/routes/index.tsx b/app/routes/index.tsx
index 2a4a5895d..e4f372cb8 100644
--- a/app/routes/index.tsx
+++ b/app/routes/index.tsx
@@ -1,7 +1,11 @@
import MainPage from 'pages/MainPage'
-import type { V2_MetaFunction } from '@remix-run/node'
+import type { V2_MetaFunction, LoaderArgs } from '@remix-run/node'
+import { useLoaderData } from '@remix-run/react'
+import { json } from '@remix-run/node'
import type { SitemapFunction } from 'remix-sitemap'
+import { detectTheme } from 'utils/server'
+
export const sitemap: SitemapFunction = () => ({
priority: 1,
})
@@ -20,6 +24,16 @@ export const meta: V2_MetaFunction = () => {
]
}
+export async function loader({ request }: LoaderArgs) {
+ const theme = detectTheme(request)
+
+ return json({ theme })
+}
+
export default function Index() {
- return <MainPage />
+ const {
+ theme,
+ } = useLoaderData<typeof loader>()
+
+ return <MainPage ssrTheme={theme} />
}
diff --git a/app/utils/server.ts b/app/utils/server.ts
index 5ee89155b..45835e173 100644
--- a/app/utils/server.ts
+++ b/app/utils/server.ts
@@ -5,3 +5,14 @@ export const hasAuthCookies = (request: Request) => {
return accessToken && refreshToken
}
+
+export function detectTheme(request: Request): 'dark' | 'light' {
+ const cookie = request.headers.get('Cookie')
+ const theme = cookie?.match(/(?<=colour-theme=)[^;]*/)?.[0]
+
+ if (theme === 'dark') {
+ return 'dark'
+ }
+
+ return 'light'
+}