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!

added support for setting theme via query param

Blue Mouseccb8205d71c9
diff --git a/app/redux/reducers/ui/theme.ts b/app/redux/reducers/ui/theme.ts
index 47b8b1a8d..2a4c72c1a 100644
--- a/app/redux/reducers/ui/theme.ts
+++ b/app/redux/reducers/ui/theme.ts
@@ -32,6 +32,13 @@ const getInitialTheme = (): ThemeType => {
return 'light'
}
+ const queryTheme = new URLSearchParams(window.location.search).get('theme') as (ThemeType | null)
+
+ if (queryTheme && _includes(SUPPORTED_THEMES, queryTheme)) {
+ setThemeToDOM(queryTheme)
+ return queryTheme
+ }
+
const lsTheme: any = getCookie(LS_THEME_SETTING)
if (_includes(SUPPORTED_THEMES, lsTheme)) {
diff --git a/app/utils/server.ts b/app/utils/server.ts
index 4dff8c955..b1d54e0c1 100644
--- a/app/utils/server.ts
+++ b/app/utils/server.ts
@@ -17,7 +17,14 @@ export const hasAuthCookies = (request: Request) => {
* @returns [theme, storeToCookie]
*/
export function detectTheme(request: Request): [ThemeType, boolean] {
- // Stage 1: Check if user has set theme manually
+ // Stage 1: Check if theme is set via `theme` query param
+ const queryTheme = new URL(request.url).searchParams.get('theme') as (ThemeType | null)
+
+ if (queryTheme && _includes(SUPPORTED_THEMES, queryTheme)) {
+ return [queryTheme, false]
+ }
+
+ // Stage 2: Check if user has set theme manually
const cookie = request.headers.get('Cookie')
const theme = cookie?.match(/(?<=colour-theme=)[^;]*/)?.[0] as ThemeType
@@ -25,7 +32,7 @@ export function detectTheme(request: Request): [ThemeType, boolean] {
return [theme, false]
}
- // Stage 2: Try to detect theme based on Sec-CH browser hints
+ // Stage 3: Try to detect theme based on Sec-CH browser hints
// Currently only Chromium-based browsers support this feature
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-Prefers-Color-Scheme
const hintedTheme = request.headers.get('Sec-CH-Prefers-Color-Scheme') as ThemeType