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(dynamic-branding): reuse branding data and icons fetched by the preload script

Hristo Terezove2b3e2641543
diff --git a/react/features/dynamic-branding/actions.any.ts b/react/features/dynamic-branding/actions.any.ts
index fa8df3ce2b..396b050c33 100644
--- a/react/features/dynamic-branding/actions.any.ts
+++ b/react/features/dynamic-branding/actions.any.ts
@@ -1,5 +1,6 @@
import { IStore } from '../app/types';
import { doGetJSON } from '../base/util/httpUtils';
+import { takePreloadedBranding } from '../preload/functions';
import {
SET_DYNAMIC_BRANDING_DATA,
@@ -28,7 +29,7 @@ export function fetchCustomBrandingData() {
if (url) {
try {
- const res = await doGetJSON(url);
+ const res = await fetchBrandingData(url);
return dispatch(setDynamicBrandingData(res));
} catch (err) {
@@ -43,6 +44,27 @@ export function fetchCustomBrandingData() {
};
}
+/**
+ * Fetches the branding data from the given URL, reusing the response the preload script already
+ * started downloading for it when there is one.
+ *
+ * @param {string} url - The branding URL.
+ * @returns {Promise<Object>}
+ */
+async function fetchBrandingData(url: string): Promise<Object> {
+ const preloaded = takePreloadedBranding(url);
+
+ if (preloaded) {
+ try {
+ return await preloaded;
+ } catch (err) {
+ logger.warn('Preloaded branding data failed, fetching it again', err);
+ }
+ }
+
+ return doGetJSON(url);
+}
+
/**
* Action used to set the user customizations.
*
diff --git a/react/features/dynamic-branding/functions.any.ts b/react/features/dynamic-branding/functions.any.ts
index 8f8c0d6828..59d5348611 100644
--- a/react/features/dynamic-branding/functions.any.ts
+++ b/react/features/dynamic-branding/functions.any.ts
@@ -57,11 +57,16 @@ export function isDynamicBrandingDataLoaded(state: IReduxState) {
* the rest of the branding. Icons that fail to load are logged and left out of the result.
*
* @param {Record<string, string>} customIcons - Map of icon name to SVG URL or inline SVG markup.
+ * @param {Promise<Record<string, string>>} [preloadedMarkup] - Raw markup the preload script already
+ * downloaded for some of the URL icons, keyed by icon name.
* @returns {Promise<Record<string, string>>} Map of icon name to sanitized SVG XML.
*/
-export async function fetchCustomIcons(customIcons: Record<string, string>): Promise<Record<string, string>> {
+export async function fetchCustomIcons(
+ customIcons: Record<string, string>,
+ preloadedMarkup?: Promise<Record<string, string>>): Promise<Record<string, string>> {
+ const preloaded = await resolvePreloadedMarkup(preloadedMarkup);
const entries = Object.entries(customIcons);
- const results = await Promise.allSettled(entries.map(entry => loadCustomIcon(entry[1])));
+ const results = await Promise.allSettled(entries.map(([ key, value ]) => loadCustomIcon(preloaded[key] ?? value)));
const localCustomIcons: Record<string, string> = {};
results.forEach((result, index) => {
@@ -77,6 +82,28 @@ export async function fetchCustomIcons(customIcons: Record<string, string>): Pro
return localCustomIcons;
}
+/**
+ * Waits for the icon markup the preload script downloaded, if any. A failure there is not fatal,
+ * the icons are simply downloaded again.
+ *
+ * @param {Promise<Record<string, string>>} [preloadedMarkup] - The preloaded markup, keyed by icon name.
+ * @returns {Promise<Record<string, string>>}
+ */
+async function resolvePreloadedMarkup(
+ preloadedMarkup?: Promise<Record<string, string>>): Promise<Record<string, string>> {
+ if (!preloadedMarkup) {
+ return {};
+ }
+
+ try {
+ return await preloadedMarkup;
+ } catch (err) {
+ logger.warn('Preloaded custom icons failed, fetching them again', err);
+
+ return {};
+ }
+}
+
/**
* Resolves a single custom icon to sanitized SVG XML. Inline markup is used as is; anything else
* is treated as a URL and downloaded.
diff --git a/react/features/dynamic-branding/middleware.any.ts b/react/features/dynamic-branding/middleware.any.ts
index 01020cc216..18fe74f7e5 100644
--- a/react/features/dynamic-branding/middleware.any.ts
+++ b/react/features/dynamic-branding/middleware.any.ts
@@ -5,10 +5,11 @@ import { PARTICIPANT_ROLE } from '../base/participants/constants';
import { getLocalParticipant, isLocalParticipantModerator } from '../base/participants/functions';
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
import { toState } from '../base/redux/functions';
+import { takePreloadedIcons } from '../preload/functions';
import { SET_DYNAMIC_BRANDING_DATA, SET_DYNAMIC_BRANDING_READY } from './actionTypes';
import { setDynamicBrandingIcons } from './actions.any';
-import { fetchCustomIcons } from './functions.any';
+import { fetchCustomIcons, getDynamicBrandingUrl } from './functions.any';
import logger from './logger';
MiddlewareRegistry.register(store => next => action => {
@@ -20,7 +21,8 @@ MiddlewareRegistry.register(store => next => action => {
// The icons are loaded separately so the theme and the rest of the branding are
// applied right away instead of waiting for every SVG to arrive.
if (customIcons) {
- fetchCustomIcons(customIcons)
+ getDynamicBrandingUrl(store.getState())
+ .then(url => fetchCustomIcons(customIcons, url ? takePreloadedIcons(url) : undefined))
.then(icons => store.dispatch(setDynamicBrandingIcons(icons)))
.catch((error: any) => {
logger.error('Error fetching branded custom icons:', error);