Skip to content

Commit 9d4b12b

Browse files
fix: lazy-load companion iframe to avoid requests on every page visit (calcom#27946)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent c21281a commit 9d4b12b

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

companion/extension/entrypoints/content.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default defineContentScript({
3838

3939
let isVisible = false;
4040
let isClosed = true;
41+
let iframeLoaded = false;
4142

4243
// Create sidebar container
4344
const sidebarContainer = document.createElement("div");
@@ -69,7 +70,6 @@ export default defineContentScript({
6970
// - ext:build-prod → uses https://companion.cal.com
7071
const COMPANION_URL =
7172
(import.meta.env.EXPO_PUBLIC_COMPANION_DEV_URL as string) || "https://companion.cal.com";
72-
iframe.src = COMPANION_URL;
7373
// Enable clipboard access for the cross-origin iframe
7474
// This allows the companion app to use navigator.clipboard.writeText()
7575
iframe.allow = "clipboard-write; clipboard-read";
@@ -90,6 +90,8 @@ export default defineContentScript({
9090

9191
// Listen for messages from iframe to control width and handle OAuth
9292
window.addEventListener("message", (event) => {
93+
if (!iframeLoaded) return;
94+
9395
// Security: Only accept messages from our iframe's origin
9496
// This prevents malicious scripts on the host page from manipulating the companion
9597
const iframeOrigin = new URL(iframe.src).origin;
@@ -505,6 +507,10 @@ export default defineContentScript({
505507

506508
// Function to open the sidebar
507509
function openSidebar() {
510+
if (!iframeLoaded) {
511+
iframe.src = COMPANION_URL;
512+
iframeLoaded = true;
513+
}
508514
if (isClosed) {
509515
isClosed = false;
510516
isVisible = true;

0 commit comments

Comments
 (0)