diff --git a/packages/embeds/embed-core/playground.ts b/packages/embeds/embed-core/playground.ts
index 08b7e45b55..8dd4fa1460 100644
--- a/packages/embeds/embed-core/playground.ts
+++ b/packages/embeds/embed-core/playground.ts
@@ -1,6 +1,9 @@
import type { GlobalCal, EmbedEvent } from "./src/embed";
const Cal = window.Cal as GlobalCal;
+Cal.config = Cal.config || {};
+Cal.config.forwardQueryParams = true;
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const callback = function (e: any) {
const detail = e.detail;
@@ -547,6 +550,25 @@ if (only === "all" || only == "ns:autoScrollTest") {
},
});
}
+
+if (only === "all" || only == "ns:pageParamsForwarding") {
+ Cal("init", "pageParamsForwarding", {
+ debug: true,
+ origin: origin,
+ });
+
+ Cal.ns.pageParamsForwarding("inline", {
+ elementOrSelector: "#cal-booking-place-pageParamsForwarding .place",
+ calLink: "pro/paid?embedType=inline&month=2024-08&date=2024-08-26&slot=2024-08-26T14%3A00%3A00.000Z",
+ config: {
+ iframeAttrs: {
+ id: "cal-booking-place-pageParamsForwarding-iframe",
+ },
+ "flag.coep": "true",
+ },
+ });
+}
+
// Verifies that the type of e.detail.data is valid. type-check will fail if we accidentally break it.
const bookingSuccessfulV2Callback = (e: EmbedEvent<"bookingSuccessfulV2">) => {
const data = e.detail.data;
diff --git a/packages/embeds/embed-core/src/embed.test.ts b/packages/embeds/embed-core/src/embed.test.ts
new file mode 100644
index 0000000000..3507f9f893
--- /dev/null
+++ b/packages/embeds/embed-core/src/embed.test.ts
@@ -0,0 +1,116 @@
+import { describe, it, expect, beforeEach, vi, beforeAll } from "vitest";
+
+vi.mock("./tailwindCss", () => ({
+ default: "mockedTailwindCss",
+}));
+
+describe("Cal", () => {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ let CalClass: any;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ let cal: any;
+
+ function mockSearchParams(search: string) {
+ Object.defineProperty(window, "location", {
+ value: { search },
+ writable: true,
+ });
+ }
+
+ beforeAll(async () => {
+ // Mock window.Cal
+ const mockCal = {
+ q: [],
+ ns: {},
+ };
+ Object.defineProperty(window, "Cal", {
+ value: mockCal,
+ writable: true,
+ });
+
+ CalClass = (await import("./embed")).Cal;
+ });
+
+ beforeEach(() => {
+ cal = new CalClass("test-namespace", []);
+ window.Cal.config = { forwardQueryParams: true };
+ // Mock the getConfig method
+ cal.getConfig = vi.fn().mockReturnValue({ calOrigin: "https://app.cal.com" });
+ });
+
+ describe("createIframe", () => {
+ describe("params handling", () => {
+ it("should merge query parameters from URL and explicit params", () => {
+ mockSearchParams("?existingParam=value");
+
+ const iframe = cal.createIframe({
+ calLink: "john-doe/meeting",
+ config: { newParam: "newValue" },
+ calOrigin: null,
+ });
+
+ expect(iframe.src).toContain("existingParam=value");
+ expect(iframe.src).toContain("newParam=newValue");
+ });
+
+ it("should not lose duplicate params in page query", () => {
+ mockSearchParams("?param1=value1¶m1=value2");
+
+ const iframe = cal.createIframe({
+ calLink: "john-doe/meeting",
+ config: { param2: "value3" },
+ calOrigin: null,
+ });
+
+ const urlParams = new URLSearchParams(new URL(iframe.src).search);
+ const paramValues = urlParams.getAll("param1");
+ expect(paramValues).toEqual(["value1", "value2"]);
+ });
+
+ it("should override the duplicate params in page if the same param exists in explicit config", () => {
+ mockSearchParams("?param=value1¶m=value2");
+
+ const iframe = cal.createIframe({
+ calLink: "john-doe/meeting",
+ config: { param: "value3" },
+ calOrigin: null,
+ });
+
+ const urlParams = new URLSearchParams(new URL(iframe.src).search);
+ const paramValues = urlParams.getAll("param");
+ expect(paramValues).toEqual(["value3"]);
+ });
+
+ it("should exclude reserved params", () => {
+ const iframe = cal.createIframe({
+ calLink: "john-doe/meeting",
+ config: {
+ date: "2023-05-01",
+ duration: "30",
+ email: "test@example.com",
+ },
+ calOrigin: null,
+ });
+
+ expect(iframe.src).not.toContain("date=");
+ expect(iframe.src).not.toContain("duration=");
+ expect(iframe.src).toContain("email=test%40example.com");
+ });
+
+ it("should respect forwardQueryParams setting to disable sending page query params but still send the ones in the config", () => {
+ mockSearchParams("?param1=value");
+
+ window.Cal.config = { forwardQueryParams: false };
+
+ const iframe = cal.createIframe({
+ calLink: "john-doe/meeting",
+ config: { param2: "value" },
+ calOrigin: null,
+ });
+
+ expect(iframe.src).not.toContain("param1=value");
+ expect(iframe.src).toContain("param2=value");
+ });
+ });
+ });
+});
diff --git a/packages/embeds/embed-core/src/embed.ts b/packages/embeds/embed-core/src/embed.ts
index 8f6e1d10a8..dfeb15f60d 100644
--- a/packages/embeds/embed-core/src/embed.ts
+++ b/packages/embeds/embed-core/src/embed.ts
@@ -6,8 +6,9 @@ import type { InterfaceWithParent, interfaceWithParent, PrefillAndIframeAttrsCon
import css from "./embed.css";
import { SdkActionManager } from "./sdk-action-manager";
import type { EventData, EventDataMap } from "./sdk-action-manager";
-import allCss from "./tailwind.generated.css?inline";
+import tailwindCss from "./tailwindCss";
import type { UiConfig } from "./types";
+import { fromEntriesWithDuplicateKeys } from "./utils";
export type { PrefillAndIframeAttrsConfig } from "./embed-iframe";
@@ -15,7 +16,7 @@ export type { PrefillAndIframeAttrsConfig } from "./embed-iframe";
export type { EmbedEvent } from "./sdk-action-manager";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
-type Rest
= T extends [any, ...infer U] ? U : never;
+type Rest = T extends [any, ...infer U] ? U : never;
export type Message = {
originator: string;
method: keyof InterfaceWithParent;
@@ -31,12 +32,13 @@ customElements.define("cal-inline", Inline);
declare module "*.css";
type Namespace = string;
-type Config = {
+type InitConfig = {
calOrigin: string;
debug?: boolean;
uiDebug?: boolean;
};
-type InitArgConfig = Partial & {
+
+type InitArgConfig = Partial & {
origin?: string;
};
@@ -52,17 +54,9 @@ if (!globalCal || !globalCal.q) {
throw new Error("Cal is not defined. This shouldn't happen");
}
-// Store Commit Hash to know exactly what version of the code is running
-// TODO: Ideally it should be the version as per package.json and then it can be renamed to version.
-// But because it is built on local machine right now, it is much more reliable to have the commit hash.
-globalCal.fingerprint = process.env.EMBED_PUBLIC_EMBED_FINGER_PRINT as string;
-globalCal.version = process.env.EMBED_PUBLIC_EMBED_VERSION as string;
-globalCal.__css = allCss;
-document.head.appendChild(document.createElement("style")).innerHTML = css;
+initializeGlobalCalProps();
-function log(...args: unknown[]) {
- console.log(...args);
-}
+document.head.appendChild(document.createElement("style")).innerHTML = css;
// eslint-disable-next-line @typescript-eslint/ban-types
type ValidationSchemaPropType = string | Function;
@@ -129,20 +123,20 @@ function getColorScheme(el: Element) {
}
function withColorScheme(
- queryObject: PrefillAndIframeAttrsConfig & { guest?: string | string[] },
+ config: PrefillAndIframeAttrsConfigWithGuest,
containerEl: Element
-) {
+): PrefillAndIframeAttrsConfigWithGuestAndColorScheme {
// If color-scheme not explicitly configured, keep it same as the webpage that has the iframe
// This is done to avoid having an opaque background of iframe that arises when they aren't same. We really need to have a transparent background to make embed part of the page
// https://fvsch.com/transparent-iframes#:~:text=the%20resolution%20was%3A-,If%20the%20color%20scheme%20of%20an%20iframe%20differs%20from%20embedding%20document%2C%20iframe%20gets%20an%20opaque%20canvas%20background%20appropriate%20to%20its%20color%20scheme.,-So%20the%20dark
- if (!queryObject["ui.color-scheme"]) {
+ if (!config["ui.color-scheme"]) {
const colorScheme = getColorScheme(containerEl);
// Only handle two color-schemes for now. We don't want to have unintented affect by always explicitly adding color-scheme
if (colorScheme) {
- queryObject["ui.color-scheme"] = colorScheme;
+ config["ui.color-scheme"] = colorScheme;
}
}
- return queryObject;
+ return config;
}
type allPossibleCallbacksAndActions = {
@@ -166,10 +160,36 @@ type SingleInstruction = SingleInstructionMap[keyof SingleInstructionMap];
export type Instruction = SingleInstruction | SingleInstruction[];
export type InstructionQueue = Instruction[];
+const excludeParam = (key: string, _value: unknown) => {
+ const paramsReservedByBookingForm = [
+ "month",
+ "date",
+ "slot",
+ "rescheduleUid",
+ "bookingUid",
+ "duration",
+ "overlayCalendar",
+ ];
+
+ const EXCLUDED_PARAMS = [...paramsReservedByBookingForm];
+
+ if (EXCLUDED_PARAMS.includes(key)) return true;
+
+ return false;
+};
+
+type PrefillAndIframeAttrsConfigWithGuest = PrefillAndIframeAttrsConfig & {
+ guest?: string | string[];
+};
+
+type PrefillAndIframeAttrsConfigWithGuestAndColorScheme = PrefillAndIframeAttrsConfigWithGuest & {
+ "ui.color-scheme"?: string | null;
+};
+
export class Cal {
iframe?: HTMLIFrameElement;
- __config: Config;
+ __config: InitConfig;
modalBox?: Element;
@@ -189,13 +209,13 @@ export class Cal {
static actionsManagers: Record;
- static getQueryObject(config: PrefillAndIframeAttrsConfig) {
+ static ensureGuestKey(config: PrefillAndIframeAttrsConfig) {
config = config || {};
return {
...config,
// guests is better for API but Booking Page accepts guest. So do the mapping
guest: config.guests ?? undefined,
- } as PrefillAndIframeAttrsConfig & { guest?: string | string[] };
+ } as PrefillAndIframeAttrsConfigWithGuest;
}
processInstruction(instructionAsArgs: IArguments | Instruction) {
@@ -245,39 +265,28 @@ export class Cal {
*/
createIframe({
calLink,
- queryObject = {},
+ config = {},
calOrigin,
}: {
calLink: string;
- queryObject?: PrefillAndIframeAttrsConfig & { guest?: string | string[] };
+ config?: PrefillAndIframeAttrsConfigWithGuestAndColorScheme;
calOrigin: string | null;
}) {
const iframe = (this.iframe = document.createElement("iframe"));
iframe.className = "cal-embed";
iframe.name = `cal-embed=${this.namespace}`;
iframe.title = `Book a call`;
- const config = this.getConfig();
- const { iframeAttrs, ...restQueryObject } = queryObject;
+ const embedConfig = this.getInitConfig();
+ const { iframeAttrs, ...queryParamsFromConfig } = config;
if (iframeAttrs && iframeAttrs.id) {
iframe.setAttribute("id", iframeAttrs.id);
}
- // Prepare searchParams from config
- const searchParams = new URLSearchParams();
- for (const [key, value] of Object.entries(restQueryObject)) {
- if (value === undefined) {
- continue;
- }
- if (value instanceof Array) {
- value.forEach((val) => searchParams.append(key, val));
- } else {
- searchParams.set(key, value as string);
- }
- }
+ const searchParams = this.buildFilteredQueryParams(queryParamsFromConfig);
// cal.com has rewrite issues on Safari that sometimes cause 404 for assets.
- const originToUse = (calOrigin || config.calOrigin || "").replace(
+ const originToUse = (calOrigin || embedConfig.calOrigin || "").replace(
"https://cal.com",
"https://app.cal.com"
);
@@ -295,14 +304,14 @@ export class Cal {
urlInstance.searchParams.set("embed", this.namespace);
- if (config.debug) {
- urlInstance.searchParams.set("debug", `${config.debug}`);
+ if (embedConfig.debug) {
+ urlInstance.searchParams.set("debug", `${embedConfig.debug}`);
}
// Keep iframe invisible, till the embedded calLink sets its color-scheme. This is so that there is no flash of non-transparent(white/black) background
iframe.style.visibility = "hidden";
- if (config.uiDebug) {
+ if (embedConfig.uiDebug) {
iframe.style.border = "1px solid green";
}
@@ -316,7 +325,7 @@ export class Cal {
return iframe;
}
- getConfig() {
+ getInitConfig() {
return this.__config;
}
@@ -426,6 +435,33 @@ export class Cal {
this.modalBox?.setAttribute("state", "failed");
});
}
+
+ private filterParams(params: Record): Record {
+ return Object.fromEntries(Object.entries(params).filter(([key, value]) => !excludeParam(key, value)));
+ }
+
+ private buildFilteredQueryParams(queryParamsFromConfig: PrefillAndIframeAttrsConfig): URLSearchParams {
+ const queryParamsFromPageUrl = globalCal.config?.forwardQueryParams ? getQueryParamsFromPage() : {};
+
+ // Query Params via config have higher precedence
+ const mergedQueryParams = { ...queryParamsFromPageUrl, ...queryParamsFromConfig };
+
+ const filteredQueryParams = this.filterParams(mergedQueryParams);
+
+ const searchParams = new URLSearchParams();
+ for (const [key, value] of Object.entries(filteredQueryParams)) {
+ if (value === undefined) {
+ continue;
+ }
+ if (value instanceof Array) {
+ value.forEach((val) => searchParams.append(key, val));
+ } else {
+ searchParams.set(key, value as string);
+ }
+ }
+
+ return searchParams;
+ }
}
class CalApi {
@@ -444,7 +480,7 @@ class CalApi {
init(namespaceOrConfig?: string | InitArgConfig, config = {} as InitArgConfig) {
let initForNamespace = "";
if (typeof namespaceOrConfig !== "string") {
- config = (namespaceOrConfig || {}) as Config;
+ config = (namespaceOrConfig || {}) as InitConfig;
} else {
initForNamespace = namespaceOrConfig;
}
@@ -525,11 +561,11 @@ class CalApi {
}
config.embedType = "inline";
- const calConfig = this.cal.getConfig();
+ const calConfig = this.cal.getInitConfig();
const iframe = this.cal.createIframe({
calLink,
- queryObject: withColorScheme(Cal.getQueryObject(config), containerEl),
+ config: withColorScheme(Cal.ensureGuestKey(config), containerEl),
calOrigin: calConfig.calOrigin,
});
@@ -629,14 +665,14 @@ class CalApi {
config.prerender = "true";
}
- const queryObject = withColorScheme(Cal.getQueryObject(config), containerEl);
+ const configWithGuestKeyAndColorScheme = withColorScheme(Cal.ensureGuestKey(config), containerEl);
const existingModalEl = document.querySelector(`cal-modal-box[uid="${uid}"]`);
if (existingModalEl) {
if (isConnectingToPreloadedModal) {
this.cal.doInIframe({
method: "connect",
- arg: queryObject,
+ arg: configWithGuestKeyAndColorScheme,
});
this.modalUid = uid;
existingModalEl.setAttribute("state", "loading");
@@ -661,7 +697,7 @@ class CalApi {
if (!iframe) {
iframe = this.cal.createIframe({
calLink,
- queryObject,
+ config: configWithGuestKeyAndColorScheme,
calOrigin: calOrigin || null,
});
}
@@ -766,7 +802,7 @@ class CalApi {
throw new Error(`Namespace ${namespace} isn't defined`);
}
- const config = this.cal.getConfig();
+ const config = this.cal.getInitConfig();
let prerenderIframe = options.prerenderIframe;
if (type && prerenderIframe === undefined) {
prerenderIframe = true;
@@ -819,12 +855,21 @@ class CalApi {
}
}
+function getQueryParamsFromPage() {
+ const params = new URLSearchParams(window.location.search);
+ return fromEntriesWithDuplicateKeys(params.entries());
+}
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Queue = any[];
+type GlobalConfig = {
+ forwardQueryParams?: boolean;
+};
+type KeyOfSingleInstructionMap = keyof SingleInstructionMap;
// This is a full fledged Cal instance but doesn't have ns property because it would be nested inside an ns instance already
export interface GlobalCalWithoutNs {
- (methodName: T, ...arg: Rest): void;
+ (methodName: T, ...arg: Rest): void;
/** Marks that the embed.js is loaded. Avoids re-downloading it. */
loaded?: boolean;
/** Maintains a queue till the time embed.js isn't loaded */
@@ -835,6 +880,7 @@ export interface GlobalCalWithoutNs {
fingerprint?: string;
version?: string;
__logQueue?: unknown[];
+ config?: GlobalConfig;
}
// Well Omit removes the Function Signature from a type if used. So, instead construct the types like this.
@@ -971,7 +1017,7 @@ function getEmbedApiFn(ns: string) {
return api;
}
-function preloadAssetsForCalLink({ config, calLink }: { config: Config; calLink: string }) {
+function preloadAssetsForCalLink({ config, calLink }: { config: InitConfig; calLink: string }) {
const iframe = document.body.appendChild(document.createElement("iframe"));
const urlInstance = new URL(`${config.calOrigin}/${calLink}`);
@@ -981,3 +1027,26 @@ function preloadAssetsForCalLink({ config, calLink }: { config: Config; calLink:
iframe.style.height = "0";
iframe.style.display = "none";
}
+
+function initializeGlobalCalProps() {
+ // Store Commit Hash to know exactly what version of the code is running
+ // TODO: Ideally it should be the version as per package.json and then it can be renamed to version.
+ // But because it is built on local machine right now, it is much more reliable to have the commit hash.
+ globalCal.fingerprint = process.env.EMBED_PUBLIC_EMBED_FINGER_PRINT as string;
+ globalCal.version = process.env.EMBED_PUBLIC_EMBED_VERSION as string;
+ globalCal.__css = tailwindCss;
+
+ if (!globalCal.config) {
+ globalCal.config = {};
+ }
+
+ // This is disabled by default because if we miss any param in reserved list, we might end up breaking embed Booking Form for a lot of users.
+ // Better to be conservative and let the user decide if he wants to forward the params or not.
+ // TODO: Going forward, Booking Form should maintain a list of params used by it and then we can enable this by default after using that list itself as reserved list.
+ // Use if configured by user otherwise set default
+ globalCal.config.forwardQueryParams = globalCal.config.forwardQueryParams ?? false;
+}
+
+function log(...args: unknown[]) {
+ console.log(...args);
+}
diff --git a/packages/embeds/embed-core/src/tailwindCss.ts b/packages/embeds/embed-core/src/tailwindCss.ts
new file mode 100644
index 0000000000..bff4461287
--- /dev/null
+++ b/packages/embeds/embed-core/src/tailwindCss.ts
@@ -0,0 +1,3 @@
+import tailwind from "./tailwind.generated.css?inline";
+
+export default tailwind;
diff --git a/packages/embeds/embed-core/src/utils.ts b/packages/embeds/embed-core/src/utils.ts
index b933a83add..0e4821244a 100644
--- a/packages/embeds/embed-core/src/utils.ts
+++ b/packages/embeds/embed-core/src/utils.ts
@@ -5,3 +5,33 @@ export const getErrorString = (errorCode: string | undefined) => {
return `Error Code: ${errorCode}. Something went wrong.`;
}
};
+
+/**
+ * An alternative to Object.fromEntries that allows duplicate keys and converts the values corresponding to them in an array
+ *
+ * NOTE: This is a duplicate of the function in @calcom/lib/hooks/useRouterQuery.ts. It has to be here because embed is published to npm and shouldn't refer to any private package
+ */
+export function fromEntriesWithDuplicateKeys(entries: IterableIterator<[string, string]> | null) {
+ const result: Record = {};
+
+ if (entries === null) {
+ return result;
+ }
+
+ // Consider setting atleast ES2015 as target
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ for (const [key, value] of entries) {
+ if (result.hasOwnProperty(key)) {
+ let currentValue = result[key];
+ if (!Array.isArray(currentValue)) {
+ currentValue = [currentValue];
+ }
+ currentValue.push(value);
+ result[key] = currentValue;
+ } else {
+ result[key] = value;
+ }
+ }
+ return result;
+}
diff --git a/packages/lib/hooks/useRouterQuery.ts b/packages/lib/hooks/useRouterQuery.ts
index d1c1ebfd77..938c7d236f 100644
--- a/packages/lib/hooks/useRouterQuery.ts
+++ b/packages/lib/hooks/useRouterQuery.ts
@@ -2,6 +2,7 @@ import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
/**
* An alternative to Object.fromEntries that allows duplicate keys.
+ * There is a duplicate of the function in @calcom/embeds/embed-core/src/utils.ts. Keep them in sync.
*/
function fromEntriesWithDuplicateKeys(entries: IterableIterator<[string, string]> | null) {
const result: Record = {};
diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma
index 0fa9000395..d07ddcfa56 100644
--- a/packages/prisma/schema.prisma
+++ b/packages/prisma/schema.prisma
@@ -1,4 +1,4 @@
-// This is your Prisma Schema file
+ // This is your Prisma Schema file
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
diff --git a/vitest.workspace.ts b/vitest.workspace.ts
index befcacc264..4c12aa59e9 100644
--- a/vitest.workspace.ts
+++ b/vitest.workspace.ts
@@ -14,7 +14,8 @@ const workspaces = packagedEmbedTestsOnly
? [
{
test: {
- include: ["packages/embeds/**/*.{test,spec}.{ts,js}"],
+ name: "PackagedEmbedTests",
+ include: ["packages/embeds/**/packaged/**/*.{test,spec}.{ts,js}"],
environment: "jsdom",
},
},
@@ -203,6 +204,16 @@ const workspaces = packagedEmbedTestsOnly
setupFiles: ["apps/web/modules/test-setup.ts"],
},
},
+
+ {
+ test: {
+ globals: true,
+ environment: "jsdom",
+ name: "@calcom/embeds",
+ include: ["packages/embeds/**/*.{test,spec}.{ts,js}"],
+ exclude: ["packages/embeds/**/packaged/**/*.{test,spec}.{ts,js}"],
+ },
+ },
];
export default defineWorkspace(workspaces);