From 68d000033c7412b74d00b06182e373e9b27537f7 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 18 Jan 2024 12:40:32 +0000 Subject: [PATCH 1/4] Make it possible to disable service worker through config --- frontend/iframe/config/ConfigFactory.ts | 11 +++++++++++ frontend/iframe/config/IConfig.ts | 9 +++++++++ frontend/iframe/platform/Platform.ts | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/frontend/iframe/config/ConfigFactory.ts b/frontend/iframe/config/ConfigFactory.ts index 6ecad4c..e67f286 100644 --- a/frontend/iframe/config/ConfigFactory.ts +++ b/frontend/iframe/config/ConfigFactory.ts @@ -11,10 +11,21 @@ export class ConfigFactory { return param; }; + let enableServiceWorker = true; + const enableServiceWorkerParam = getQueryParam("enableServiceWorker"); + if (enableServiceWorkerParam) { + if (enableServiceWorkerParam === "true") { + enableServiceWorker = true; + } else if (enableServiceWorkerParam === "false") { + enableServiceWorker = false; + } + } + return { instanceId: getQueryParam("instanceId") ?? "", defaultHomeserver: getQueryParam("defaultHomeserver") ?? "", roomId: getQueryParam("roomId") ?? "", + enableServiceWorker, themeManifests: [ new URL("assets/theme-chatrix.json", import.meta.url).href, ], diff --git a/frontend/iframe/config/IConfig.ts b/frontend/iframe/config/IConfig.ts index e178211..31b0c75 100644 --- a/frontend/iframe/config/IConfig.ts +++ b/frontend/iframe/config/IConfig.ts @@ -38,4 +38,13 @@ export interface IConfig { * Example: !abc123:example.com */ roomId?: string; + + /** + * Set to false to disable service worker. + * Note that the service worker is required to make Hydrogen work correctly across multiple browser tabs. + * You should not disable the service worker in environments where multiple browser tabs are a possibility. + * + * Defaults to true. + */ + enableServiceWorker?: boolean } diff --git a/frontend/iframe/platform/Platform.ts b/frontend/iframe/platform/Platform.ts index ad074c9..e45184f 100644 --- a/frontend/iframe/platform/Platform.ts +++ b/frontend/iframe/platform/Platform.ts @@ -17,7 +17,7 @@ export class Platform extends BasePlatform { // Register our own service worker handler. let serviceWorkerHandler; - if (assetPaths.serviceWorker && "serviceWorker" in navigator) { + if (options.config.enableServiceWorker && assetPaths.serviceWorker && "serviceWorker" in navigator) { serviceWorkerHandler = new ServiceWorkerHandler(); serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker); } From 6aaf0c6222220f802b72f3ddf39b268644456c04 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 18 Jan 2024 14:11:11 +0000 Subject: [PATCH 2/4] Add enableServiceWorker attribute to block --- frontend/components/block/attributes.ts | 3 +++ frontend/components/block/block.tsx | 2 ++ 2 files changed, 5 insertions(+) diff --git a/frontend/components/block/attributes.ts b/frontend/components/block/attributes.ts index 6d9156c..f0e4d67 100644 --- a/frontend/components/block/attributes.ts +++ b/frontend/components/block/attributes.ts @@ -4,6 +4,7 @@ export interface BlockAttributes { instanceId: string, defaultHomeserver?: string, roomId?: string, + enableServiceWorker?: boolean, height?: Height, borderWidth?: BorderWidth, borderRadius?: BorderRadius, @@ -16,6 +17,7 @@ export function parseAttributes(attributes): BlockAttributes { instanceId, defaultHomeserver, roomId, + enableServiceWorker, height, borderWidth, borderRadius, @@ -27,6 +29,7 @@ export function parseAttributes(attributes): BlockAttributes { instanceId, defaultHomeserver: defaultHomeserver ?? '', roomId: roomId ?? '', + enableServiceWorker: enableServiceWorker ?? true, height: height ? new Height(height.value, height.unit) : undefined, borderWidth: borderWidth ? new BorderWidth(borderWidth.value, borderWidth.unit) : undefined, borderRadius: borderRadius ? new BorderRadius(borderRadius.value, borderRadius.unit) : undefined, diff --git a/frontend/components/block/block.tsx b/frontend/components/block/block.tsx index 8b743f2..58a8af2 100644 --- a/frontend/components/block/block.tsx +++ b/frontend/components/block/block.tsx @@ -13,6 +13,7 @@ export function Block(props: BlockProps) { instanceId, defaultHomeserver, roomId, + enableServiceWorker, height, borderWidth, borderRadius, @@ -34,6 +35,7 @@ export function Block(props: BlockProps) { instanceId, defaultHomeserver, roomId, + enableServiceWorker, }; return ( From f6057d0d9cb110515c3488d40ba507147d087db6 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 18 Jan 2024 14:11:49 +0000 Subject: [PATCH 3/4] Set enableServiceWorker value in iframe query params --- frontend/components/chat/chat.tsx | 5 ++++- frontend/components/chat/url.ts | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/components/chat/chat.tsx b/frontend/components/chat/chat.tsx index d7eeaf2..adc1661 100644 --- a/frontend/components/chat/chat.tsx +++ b/frontend/components/chat/chat.tsx @@ -7,6 +7,7 @@ export interface ChatProps { instanceId: string, defaultHomeserver?: string, roomId?: string, + enableServiceWorker?: boolean, } export function Chat(props: ChatProps) { @@ -15,7 +16,8 @@ export function Chat(props: ChatProps) { iframeUrl, instanceId, defaultHomeserver, - roomId + roomId, + enableServiceWorker, } = props; const ref = focusable ? useFocusableIframe() : undefined; @@ -23,6 +25,7 @@ export function Chat(props: ChatProps) { instanceId, defaultHomeserver, roomId, + enableServiceWorker, }); return ( diff --git a/frontend/components/chat/url.ts b/frontend/components/chat/url.ts index be01d1b..1c25ba4 100644 --- a/frontend/components/chat/url.ts +++ b/frontend/components/chat/url.ts @@ -4,6 +4,7 @@ type IframeParams = { instanceId: string, defaultHomeserver?: string roomId?: string, + enableServiceWorker?: boolean, } export class IframeUrl { @@ -13,9 +14,11 @@ export class IframeUrl { this.url = iframeUrl; for (let key in params) { - if (!!params[key]) { - this.url.searchParams.append(key, params[key]); + let value = params[key]; + if (typeof value === 'boolean') { + value = value ? "true" : "false"; } + this.url.searchParams.append(key, value); } this.applyLoginToken(); From 4a1038fe386173521cb8fb2d3d6f194f1181906c Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 18 Jan 2024 14:21:54 +0000 Subject: [PATCH 4/4] Filter out empty query params --- frontend/components/chat/url.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/frontend/components/chat/url.ts b/frontend/components/chat/url.ts index 1c25ba4..37142a0 100644 --- a/frontend/components/chat/url.ts +++ b/frontend/components/chat/url.ts @@ -15,9 +15,15 @@ export class IframeUrl { for (let key in params) { let value = params[key]; + if (typeof value === 'boolean') { value = value ? "true" : "false"; } + + if (!value) { + continue; + } + this.url.searchParams.append(key, value); }