Skip to content

Commit 8943350

Browse files
committed
fix theme persistence.
Closes #163
1 parent 94fd68e commit 8943350

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

app/root.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { cssBundleHref } from '@remix-run/css-bundle'
33
import {
44
json,
55
type DataFunctionArgs,
6+
type HeadersFunction,
67
type LinksFunction,
78
type V2_MetaFunction,
8-
type HeadersFunction,
99
} from '@remix-run/node'
1010
import {
1111
Form,
@@ -20,9 +20,10 @@ import {
2020
useSubmit,
2121
} from '@remix-run/react'
2222
import { withSentry } from '@sentry/remix'
23-
import { ThemeSwitch, useTheme } from './routes/resources+/theme.tsx'
24-
import tailwindStylesheetUrl from './styles/tailwind.css'
23+
import { ThemeSwitch, useTheme } from './routes/resources+/theme/index.tsx'
24+
import { getTheme } from './routes/resources+/theme/theme-session.server.ts'
2525
import fontStylestylesheetUrl from './styles/font.css'
26+
import tailwindStylesheetUrl from './styles/tailwind.css'
2627
import { authenticator, getUserId } from './utils/auth.server.ts'
2728
import { ClientHintCheck, getHints } from './utils/client-hints.tsx'
2829
import { prisma } from './utils/db.server.ts'
@@ -31,9 +32,9 @@ import { ButtonLink } from './utils/forms.tsx'
3132
import { getDomainUrl } from './utils/misc.server.ts'
3233
import { getUserImgSrc } from './utils/misc.ts'
3334
import { useNonce } from './utils/nonce-provider.ts'
34-
import { getSession, getTheme } from './utils/session.server.ts'
35-
import { useOptionalUser, useUser } from './utils/user.ts'
35+
import { getSession } from './utils/session.server.ts'
3636
import { makeTimings, time } from './utils/timing.server.ts'
37+
import { useOptionalUser, useUser } from './utils/user.ts'
3738

3839
export const links: LinksFunction = () => {
3940
return [
@@ -107,7 +108,7 @@ export async function loader({ request }: DataFunctionArgs) {
107108
origin: getDomainUrl(request),
108109
path: new URL(request.url).pathname,
109110
session: {
110-
theme: getTheme(cookieSession),
111+
theme: await getTheme(request),
111112
},
112113
},
113114
ENV: getEnv(),

app/routes/resources+/theme.tsx renamed to app/routes/resources+/theme/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
deleteTheme,
1414
getSession,
1515
setTheme,
16-
} from '~/utils/session.server.ts'
16+
} from './theme-session.server.ts'
1717

1818
const ROUTE_PATH = '/resources/theme'
1919

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createCookieSessionStorage } from '@remix-run/node'
2+
3+
export const sessionStorage = createCookieSessionStorage({
4+
cookie: {
5+
name: 'theme',
6+
sameSite: 'lax',
7+
path: '/',
8+
httpOnly: true,
9+
secure: process.env.NODE_ENV === 'production',
10+
},
11+
})
12+
13+
export const { getSession, commitSession, destroySession } = sessionStorage
14+
15+
type Session = Awaited<ReturnType<typeof getSession>>
16+
17+
const themeKey = 'theme'
18+
19+
export async function getTheme(
20+
request: Request,
21+
): Promise<'dark' | 'light' | null> {
22+
const session = await getSession(request.headers.get('Cookie'))
23+
const theme = session.get(themeKey)
24+
if (theme === 'dark' || theme === 'light') return theme
25+
return null
26+
}
27+
28+
export function setTheme(session: Session, theme: 'dark' | 'light') {
29+
session.set(themeKey, theme)
30+
}
31+
32+
export function deleteTheme(session: Session) {
33+
session.unset(themeKey)
34+
}

app/utils/session.server.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,3 @@ export const sessionStorage = createCookieSessionStorage({
1212
})
1313

1414
export const { getSession, commitSession, destroySession } = sessionStorage
15-
16-
type Session = Awaited<ReturnType<typeof getSession>>
17-
18-
const themeKey = 'theme'
19-
20-
export function getTheme(session: Session): 'dark' | 'light' | null {
21-
const theme = session.get(themeKey)
22-
if (theme === 'dark' || theme === 'light') return theme
23-
return null
24-
}
25-
26-
export function setTheme(session: Session, theme: 'dark' | 'light') {
27-
session.set(themeKey, theme)
28-
}
29-
30-
export function deleteTheme(session: Session) {
31-
session.unset(themeKey)
32-
}

0 commit comments

Comments
 (0)