Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

feat(preload): add the contract for resources fetched before the app bundle

Hristo Terezoveeb39d56ad0a
diff --git a/globals.d.ts b/globals.d.ts
index 95f4a504fb..115a299720 100644
--- a/globals.d.ts
+++ b/globals.d.ts
@@ -1,5 +1,6 @@
import { IStore } from "./react/features/app/types";
import { IConfig } from "./react/features/base/config/configType";
+import { IJitsiMeetPreload } from "./react/features/preload/types";
import {
DocumentPiPMediaSessionActionHandler,
ExtendedMediaSessionAction,
@@ -37,6 +38,7 @@ declare global {
JITSI_MEET_LITE_SDK?: boolean;
interfaceConfig?: any;
JitsiMeetJS?: any;
+ JitsiMeetPreload?: IJitsiMeetPreload;
MediaStreamTrackGenerator: {
new(options: { kind: string }): MediaStreamTrack & {
writable: WritableStream<VideoFrame>;
diff --git a/react/features/preload/functions.native.ts b/react/features/preload/functions.native.ts
new file mode 100644
index 0000000000..36f3f75c5d
--- /dev/null
+++ b/react/features/preload/functions.native.ts
@@ -0,0 +1,31 @@
+import { IConfig } from '../base/config/configType';
+
+/**
+ * There is no preload script on mobile, so nothing is ever preloaded.
+ *
+ * @param {string} _url - The config.js URL the app is about to request.
+ * @returns {undefined}
+ */
+export function takePreloadedConfig(_url: string): Promise<IConfig> | undefined {
+ return undefined;
+}
+
+/**
+ * There is no preload script on mobile, so nothing is ever preloaded.
+ *
+ * @param {string} _url - The branding URL the app is about to request.
+ * @returns {undefined}
+ */
+export function takePreloadedBranding(_url: string): Promise<Object> | undefined {
+ return undefined;
+}
+
+/**
+ * There is no preload script on mobile, so nothing is ever preloaded.
+ *
+ * @param {string} _brandingUrl - The URL of the branding data the icons belong to.
+ * @returns {undefined}
+ */
+export function takePreloadedIcons(_brandingUrl: string): Promise<Record<string, string>> | undefined {
+ return undefined;
+}
diff --git a/react/features/preload/functions.web.ts b/react/features/preload/functions.web.ts
new file mode 100644
index 0000000000..8cb176f16c
--- /dev/null
+++ b/react/features/preload/functions.web.ts
@@ -0,0 +1,57 @@
+import { IConfig } from '../base/config/configType';
+
+import { IJitsiMeetPreload } from './types';
+
+/**
+ * Takes a resource the preload script started downloading for the given URL and removes it, so
+ * it is used only once. Returns undefined when the preload script did not run, when it has no
+ * such resource or when the resource was requested from a different URL.
+ *
+ * @param {string} key - The resource to take.
+ * @param {string} url - The URL the app would request the resource from.
+ * @returns {Promise | undefined}
+ */
+function takePreloadedResource<K extends keyof IJitsiMeetPreload>(
+ key: K, url: string): NonNullable<IJitsiMeetPreload[K]>['promise'] | undefined {
+ const preload = window.JitsiMeetPreload;
+ const resource = preload?.[key];
+
+ if (!preload || !resource) {
+ return undefined;
+ }
+
+ delete preload[key];
+
+ return resource.url === url ? resource.promise : undefined;
+}
+
+/**
+ * Takes the config.js the preload script started downloading from the given URL, if any.
+ *
+ * @param {string} url - The config.js URL the app is about to request.
+ * @returns {Promise<IConfig> | undefined}
+ */
+export function takePreloadedConfig(url: string): Promise<IConfig> | undefined {
+ return takePreloadedResource('config', url);
+}
+
+/**
+ * Takes the dynamic branding data the preload script started downloading from the given URL, if any.
+ *
+ * @param {string} url - The branding URL the app is about to request.
+ * @returns {Promise<Object> | undefined}
+ */
+export function takePreloadedBranding(url: string): Promise<Object> | undefined {
+ return takePreloadedResource('branding', url);
+}
+
+/**
+ * Takes the raw markup of the custom icons the preload script started downloading for the
+ * branding data of the given URL, if any.
+ *
+ * @param {string} brandingUrl - The URL of the branding data the icons belong to.
+ * @returns {Promise<Record<string, string>> | undefined}
+ */
+export function takePreloadedIcons(brandingUrl: string): Promise<Record<string, string>> | undefined {
+ return takePreloadedResource('icons', brandingUrl);
+}
diff --git a/react/features/preload/types.ts b/react/features/preload/types.ts
new file mode 100644
index 0000000000..3a1f9313e8
--- /dev/null
+++ b/react/features/preload/types.ts
@@ -0,0 +1,43 @@
+import { IConfig } from '../base/config/configType';
+
+/**
+ * A resource that the preload script started downloading before the app bundle was loaded.
+ */
+export interface IPreloadedResource<T> {
+
+ /**
+ * Resolves with the downloaded resource, or rejects if the download failed. After a rejection
+ * the app downloads the resource itself.
+ */
+ promise: Promise<T>;
+
+ /**
+ * The URL the resource was requested from. The app reuses the resource only when this is
+ * exactly the URL it would have requested itself.
+ */
+ url: string;
+}
+
+/**
+ * The resources published by the preload script on {@code window.JitsiMeetPreload}. Every entry
+ * is optional: it is missing when the preload script was not loaded at all, when the resource was
+ * not needed (e.g. config.js was inlined in the page) or once the app has consumed it.
+ */
+export interface IJitsiMeetPreload {
+
+ /**
+ * The dynamic branding data.
+ */
+ branding?: IPreloadedResource<Object>;
+
+ /**
+ * The parsed config.js. Only present when config.js had to be downloaded.
+ */
+ config?: IPreloadedResource<IConfig>;
+
+ /**
+ * Raw SVG markup of the custom icons that were given as URLs in the branding data, keyed by
+ * icon name. The URL is the one of the branding data the icons belong to.
+ */
+ icons?: IPreloadedResource<Record<string, string>>;
+}