Skip to content

Commit f9488a1

Browse files
authored
fix: theme search param crashing booking page (#21120)
* fix * fix
1 parent d4a3894 commit f9488a1

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

apps/web/lib/__tests__/getThemeProviderProps.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ describe("getThemeProviderProps", () => {
5252

5353
expect(result).toEqual({
5454
...appThemeExpectedProps,
55-
storageKey: undefined,
56-
key: undefined,
55+
storageKey: "forcedThemeKey",
56+
key: "forcedThemeKey",
5757
forcedTheme: "light",
5858
enableSystem: false,
5959
});

apps/web/lib/getThemeProviderProps.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ReadonlyURLSearchParams } from "next/navigation";
2+
import { z } from "zod";
23

34
import { EmbedTheme } from "@calcom/features/embed/lib/constants";
45

@@ -92,13 +93,28 @@ export function getThemeProviderProps({
9293
searchParams: ReadonlyURLSearchParams | null;
9394
}) {
9495
const isBookingPage = props.isBookingPage;
95-
9696
const themeSupport = isBookingPage
9797
? ThemeSupport.Booking
9898
: props.isThemeSupported === false
9999
? ThemeSupport.None
100100
: ThemeSupport.App;
101101

102+
const themeQueryParam = searchParams?.get("theme") ?? "";
103+
const themeParsed = z.enum(["light", "dark", "system", "auto"]).safeParse(themeQueryParam);
104+
const isWrongThemeValue = themeQueryParam.length > 0 && !themeParsed.success;
105+
const forcedTheme = themeSupport === ThemeSupport.None || isWrongThemeValue ? "light" : undefined;
106+
if (forcedTheme) {
107+
return {
108+
key: "forcedThemeKey",
109+
storageKey: "forcedThemeKey",
110+
forcedTheme,
111+
attribute: "class",
112+
nonce: props.nonce,
113+
enableColorScheme: false,
114+
enableSystem: themeSupport !== ThemeSupport.None,
115+
};
116+
}
117+
102118
const isBookingPageThemeSupportRequired = themeSupport === ThemeSupport.Booking;
103119
const themeBasis = pathname ? getUniqueIdentifierForBookingPage({ pathname }) : null;
104120
const isThemeBasisRequired = isBookingPageThemeSupportRequired || isEmbedMode;
@@ -109,8 +125,6 @@ export function getThemeProviderProps({
109125
}
110126

111127
const appearanceIdSuffix = themeBasis ? `:${themeBasis}` : "";
112-
const forcedTheme = themeSupport === ThemeSupport.None ? "light" : undefined;
113-
const themeQueryParam = searchParams?.get("theme");
114128
const embedExplicitlySetThemeSuffix =
115129
isEmbedMode && themeQueryParam && themeQueryParam !== EmbedTheme.auto ? `:${themeQueryParam}` : "";
116130

@@ -126,8 +140,6 @@ export function getThemeProviderProps({
126140

127141
const themeProviderProps = {
128142
storageKey,
129-
// Can force a theme through this, no matter what system preference is or storage value is
130-
forcedTheme,
131143
nonce: props.nonce,
132144
enableColorScheme: false,
133145
// Enables theme switching based on system preference if true

0 commit comments

Comments
 (0)