Skip to content

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

import embed.ts instead of adding the script (#62154)

Nicolò Pretto1321b75fffa3
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx
index 7557b4aa974..295005d66d5 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/components/SdkIframeEmbedPreview.tsx
@@ -1,5 +1,5 @@
//
-import { createElement, useCallback, useEffect, useMemo, useRef } from "react";
+import { createElement, useCallback, useEffect, useMemo } from "react";
import { useSearchParam } from "react-use";
import { match } from "ts-pattern";
@@ -14,6 +14,10 @@ import { getConfigurableThemeColors } from "../utils/theme-colors";
import S from "./SdkIframeEmbedPreview.module.css";
+// we import the equivalent of embed.js so that we don't add extra loading time
+// by appending the script
+import "metabase-enterprise/embedding_iframe_sdk/embed";
+
declare global {
interface Window {
metabaseConfig: Partial<SdkIframeEmbedBaseSettings>;
@@ -21,11 +25,9 @@ declare global {
}
export const SdkIframeEmbedPreview = () => {
- const { settings, isEmbedSettingsLoaded, experience } =
- useSdkIframeEmbedSetupContext();
+ const { settings } = useSdkIframeEmbedSetupContext();
const localeOverride = useSearchParam("locale");
- const scriptRef = useRef<HTMLScriptElement | null>(null);
const instanceUrl = useSetting("site-url");
const applicationColors = useSetting("application-colors");
@@ -37,24 +39,6 @@ export const SdkIframeEmbedPreview = () => {
[],
);
- useEffect(
- () => {
- if (isEmbedSettingsLoaded) {
- const script = document.createElement("script");
- script.src = `${instanceUrl}/app/embed.js`;
- document.body.appendChild(script);
- scriptRef.current = script;
- }
-
- return () => {
- scriptRef.current?.remove();
- };
- },
-
- // eslint-disable-next-line react-hooks/exhaustive-deps -- settings are synced via useEffect below
- [isEmbedSettingsLoaded, experience],
- );
-
// TODO(EMB-696): There is a bug in the SDK where if we set the theme back to undefined,
// some color will not be reset to the default (e.g. text color, CSS variables).
// We can remove this block once EMB-696 is fixed.
@@ -69,20 +53,24 @@ export const SdkIframeEmbedPreview = () => {
return { colors };
}, [applicationColors]);
- useEffect(() => {
- defineMetabaseConfig({
+ const metabaseConfig = useMemo(
+ () => ({
instanceUrl,
useExistingUserSession: true,
theme: settings.theme ?? defaultTheme,
...(localeOverride ? { locale: localeOverride } : {}),
- });
- }, [
- instanceUrl,
- localeOverride,
- settings.theme,
- defineMetabaseConfig,
- defaultTheme,
- ]);
+ }),
+ [instanceUrl, localeOverride, settings.theme, defaultTheme],
+ );
+
+ // initial configuration, needed so that the element finds the config on first render
+ if (!window.metabaseConfig.instanceUrl) {
+ defineMetabaseConfig(metabaseConfig);
+ }
+
+ useEffect(() => {
+ defineMetabaseConfig(metabaseConfig);
+ }, [metabaseConfig, defineMetabaseConfig]);
return (
<Card