Skip to content

Commit

Permalink
Use SSR env variable for easier logic separation
Browse files Browse the repository at this point in the history
Since it gets replaced during build, dead code also gets removed.
  • Loading branch information
silvenon committed Apr 8, 2024
1 parent 733df68 commit d876105
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
3 changes: 1 addition & 2 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ export function Layout({ children }: { children: React.ReactNode }) {
const data = useRouteLoaderData<typeof loader>('root')
const error = useRouteError()
const origin = data?.origin ?? window.location.origin
const hasJs = typeof document !== 'undefined'

return (
<AnalyticsProvider>
<DarkMode.Provider sessionValue={error !== null || data?.darkMode}>
<DarkMode.Html
lang="en"
className={clsx('h-full', hasJs ? 'js' : 'no-js')}
className={clsx('h-full', import.meta.env.SSR ? 'no-js' : 'js')}
>
<head>
{data?.appName === 'silvenon-staging' && (
Expand Down
2 changes: 1 addition & 1 deletion app/services/dark-mode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function DarkModeProvider({ sessionValue, children }: ProviderProps) {
const sessionFetcher = useFetcher({ key: FETCHER_KEY })
const optimisticSessionValueRef = useRef<boolean | undefined>(undefined)
const [matchesValue, setMatchesValue] = useState<boolean | null>(() => {
if (typeof document === 'undefined') {
if (import.meta.env.SSR) {
// there's no way for us to know what the theme should be in this context
// the client will have to figure it out before hydration.
return null
Expand Down

0 comments on commit d876105

Please sign in to comment.