Skip to content

Commit

Permalink
Merge pull request #243 from Automattic/disable-service-worker
Browse files Browse the repository at this point in the history
Make it possible to disable service worker at runtime
  • Loading branch information
psrpinto authored Jan 19, 2024
2 parents ca1adb8 + 4a1038f commit 99fb67b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions frontend/components/block/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface BlockAttributes {
instanceId: string,
defaultHomeserver?: string,
roomId?: string,
enableServiceWorker?: boolean,
height?: Height,
borderWidth?: BorderWidth,
borderRadius?: BorderRadius,
Expand All @@ -16,6 +17,7 @@ export function parseAttributes(attributes): BlockAttributes {
instanceId,
defaultHomeserver,
roomId,
enableServiceWorker,
height,
borderWidth,
borderRadius,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions frontend/components/block/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function Block(props: BlockProps) {
instanceId,
defaultHomeserver,
roomId,
enableServiceWorker,
height,
borderWidth,
borderRadius,
Expand All @@ -34,6 +35,7 @@ export function Block(props: BlockProps) {
instanceId,
defaultHomeserver,
roomId,
enableServiceWorker,
};

return (
Expand Down
5 changes: 4 additions & 1 deletion frontend/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ChatProps {
instanceId: string,
defaultHomeserver?: string,
roomId?: string,
enableServiceWorker?: boolean,
}

export function Chat(props: ChatProps) {
Expand All @@ -15,14 +16,16 @@ export function Chat(props: ChatProps) {
iframeUrl,
instanceId,
defaultHomeserver,
roomId
roomId,
enableServiceWorker,
} = props;

const ref = focusable ? useFocusableIframe() : undefined;
const url = new IframeUrl(iframeUrl, {
instanceId,
defaultHomeserver,
roomId,
enableServiceWorker,
});

return (
Expand Down
13 changes: 11 additions & 2 deletions frontend/components/chat/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type IframeParams = {
instanceId: string,
defaultHomeserver?: string
roomId?: string,
enableServiceWorker?: boolean,
}

export class IframeUrl {
Expand All @@ -13,9 +14,17 @@ 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";
}

if (!value) {
continue;
}

this.url.searchParams.append(key, value);
}

this.applyLoginToken();
Expand Down
11 changes: 11 additions & 0 deletions frontend/iframe/config/ConfigFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
9 changes: 9 additions & 0 deletions frontend/iframe/config/IConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion frontend/iframe/platform/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 99fb67b

Please sign in to comment.