Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

feat(embed-js): Add a counter-like query parameter for EmbedJS iframe src to allow multiple iframes loading in parallel (#68420)

Timofey Kachalov1149c3e30cda
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts b/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts
index 614fbe52fec..fe205c28a49 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts
@@ -29,6 +29,9 @@ const EMBEDDING_ROUTE = "embed/sdk/v1";
/** list of active embeds, used to know which embeds to update when the global config changes */
const _activeEmbeds: Set<MetabaseEmbedElement> = new Set();
+/** counter used as a parameter in the iframe src to force parallel loading */
+let _iframeCounter = 0;
+
// Setup a proxy to watch for changes to window.metabaseConfig and update all
// active embeds when the config changes. It also setups a setter for
// window.metabaseConfig to re-create the proxy if the whole object is replaced,
@@ -339,7 +342,10 @@ export abstract class MetabaseEmbedElement
: null;
this._iframe = document.createElement("iframe");
- this._iframe.src = `${this.globalSettings.instanceUrl}/${EMBEDDING_ROUTE}`;
+ // Random query param is needed to allow parallel EmbedJS iframes loading.
+ // Without it multiple EmbedJS iframes on a page loaded sequentially.
+ // We don't cache the iframe content, so random query parameter does not break caching.
+ this._iframe.src = `${this.globalSettings.instanceUrl}/${EMBEDDING_ROUTE}?v=${_iframeCounter++}`;
this._iframe.style.width = "100%";
this._iframe.style.height = "100%";
this._iframe.style.border = "none";
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.unit.spec.ts b/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.unit.spec.ts
index effce5ac346..dfec33a4c04 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.unit.spec.ts
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.unit.spec.ts
@@ -188,4 +188,20 @@ describe("embed.js script tag for sdk iframe embedding", () => {
defineMetabaseConfig({});
}).not.toThrow();
});
+
+ it("should include a counter as a query parameter in the iframe src for parallel loading", () => {
+ defineMetabaseConfig({
+ instanceUrl: "https://example.com",
+ });
+
+ const embed = document.createElement("metabase-dashboard");
+ embed.setAttribute("dashboard-id", "1");
+ document.body.appendChild(embed);
+
+ const iframe = embed.querySelector("iframe");
+ expect(iframe).not.toBeNull();
+ expect(iframe?.src).toMatch(
+ /^https:\/\/example\.com\/embed\/sdk\/v1\?v=\d+$/,
+ );
+ });
});