Skip to content

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

fix(sdk-bundle): only render SDK components when EE plugins are loaded (#70737)

Phoomparin Mano75582f798300
diff --git a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.tsx b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.tsx
index 28d9e8aa6b9..71d738de720 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.tsx
@@ -3,6 +3,7 @@ import { type ReactNode, forwardRef } from "react";
import { PublicComponentStylesWrapper } from "embedding-sdk-bundle/components/private/PublicComponentStylesWrapper";
import { SdkError } from "embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError";
import { SdkLoader } from "embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkLoader";
+import { useArePluginsReady } from "embedding-sdk-bundle/hooks/private/use-are-plugins-ready";
import { useSdkSelector } from "embedding-sdk-bundle/store";
import {
getInitStatus,
@@ -20,6 +21,7 @@ export const PublicComponentWrapper = forwardRef<
>(function PublicComponentWrapper({ children, className, style }, ref) {
const initStatus = useSdkSelector(getInitStatus);
const usageProblem = useSdkSelector(getUsageProblem);
+ const pluginsReady = useArePluginsReady();
let content = children;
@@ -41,6 +43,11 @@ export const PublicComponentWrapper = forwardRef<
content = null;
}
+ // Wait for EE plugins to be initialized before rendering children.
+ if (!pluginsReady && content === children) {
+ content = <SdkLoader />;
+ }
+
return (
<PublicComponentStylesWrapper className={className} style={style} ref={ref}>
{content}
diff --git a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.unit.spec.tsx
index 2eed45c445d..56d1b476c2d 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.unit.spec.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/PublicComponentWrapper.unit.spec.tsx
@@ -1,5 +1,8 @@
+import { act } from "@testing-library/react";
+
import { renderWithProviders, screen } from "__support__/ui";
import { sdkReducers } from "embedding-sdk-bundle/store";
+import { setPluginsReady } from "embedding-sdk-bundle/store/reducer";
import {
createMockLoginStatusState,
createMockSdkState,
@@ -9,10 +12,14 @@ import { createMockState } from "metabase-types/store/mocks";
import { PublicComponentWrapper } from "./PublicComponentWrapper";
-const setup = (status: LoginStatus = { status: "uninitialized" }) => {
+const setup = (
+ status: LoginStatus = { status: "uninitialized" },
+ pluginsReady = true,
+) => {
const state = createMockState({
sdk: createMockSdkState({
initStatus: createMockLoginStatusState(status),
+ pluginsReady,
}),
});
@@ -55,4 +62,32 @@ describe("PublicComponentWrapper", () => {
const component = screen.getByText("My component");
expect(component).toBeInTheDocument();
});
+
+ describe("plugin initialization race condition (EMB-1426)", () => {
+ it("shows loader when auth is done but plugins are not yet initialized", () => {
+ // Simulate the race: initStatus is "success" (auth completed) but
+ // pluginsReady is false (EE plugins haven't been initialized yet).
+ // This happens when ComponentProvider mounts after the SDK has already
+ // loaded, e.g. when a collapsible containing an iframe is first opened.
+ setup({ status: "success" }, false);
+
+ expect(screen.getByTestId("loading-indicator")).toBeInTheDocument();
+ expect(screen.queryByText("My component")).not.toBeInTheDocument();
+ });
+
+ it("renders children once plugins are initialized", async () => {
+ const { store } = setup({ status: "success" }, false);
+
+ expect(screen.getByTestId("loading-indicator")).toBeInTheDocument();
+
+ // Simulate useInitPlugins completing — dispatch is what
+ // ComponentProvider does after initializePlugins() returns.
+ act(() => {
+ store.dispatch(setPluginsReady(true));
+ });
+
+ expect(await screen.findByText("My component")).toBeInTheDocument();
+ expect(screen.queryByTestId("loading-indicator")).not.toBeInTheDocument();
+ });
+ });
});
diff --git a/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx b/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx
index 5778fb4502d..83feae6ae46 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/ComponentProvider/ComponentProvider.tsx
@@ -13,6 +13,7 @@ import {
setEventHandlers,
setIsGuestEmbed,
setPlugins,
+ setPluginsReady,
} from "embedding-sdk-bundle/store/reducer";
import type { SdkStore } from "embedding-sdk-bundle/store/types";
import type { MetabaseProviderProps } from "embedding-sdk-bundle/types/metabase-provider";
@@ -39,23 +40,36 @@ export type ComponentProviderInternalProps = ComponentProviderProps & {
let hasInitializedPlugins = false;
-function useInitPlugins() {
+/**
+ * Initializes EE plugins synchronously during render
+ * to avoid an extra frame where children render without plugins.
+ *
+ * Uses reduxStore.dispatch directly instead of the useDispatch hook,
+ * since the hook-based dispatch may not be available during render.
+ * This follows the same pattern as use-init-data-internal.ts.
+ */
+function useInitPlugins(reduxStore: SdkStore) {
const tokenFeatures = useSelector(
(state) => state.settings.values["token-features"],
);
- useEffect(() => {
- // EAJS already initializes the plugins in its entrypoint, and this
- // component is used by EAJS we have to make sure we don't re-initialize the
- // sdk plugins as they could override some of plugins needed by EAJS
- if (isEmbeddingEajs() || hasInitializedPlugins || !tokenFeatures) {
- return;
- }
+ // Modular Embedding already initializes the plugins in its entrypoint.
+ // We have to avoid re-initializing SDK plugins as they could override
+ // some of plugins needed by EAJS
+ if (isEmbeddingEajs() || !tokenFeatures) {
+ return;
+ }
+ if (!hasInitializedPlugins) {
hasInitializedPlugins = true;
initializePlugins();
- }, [tokenFeatures]);
+ }
+
+ // Mark ready for this store instance on first render.
+ if (!reduxStore.getState().sdk?.pluginsReady) {
+ reduxStore.dispatch(setPluginsReady(true));
+ }
}
export const ComponentProviderInternal = (
@@ -88,7 +102,7 @@ export const ComponentProviderInternal = (
isLocalHost,
});
- useInitPlugins();
+ useInitPlugins(reduxStore);
useSdkCustomLoader();
diff --git a/frontend/src/embedding-sdk-bundle/hooks/private/use-are-plugins-ready.ts b/frontend/src/embedding-sdk-bundle/hooks/private/use-are-plugins-ready.ts
new file mode 100644
index 00000000000..4dfe669536f
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/hooks/private/use-are-plugins-ready.ts
@@ -0,0 +1,24 @@
+import { useSdkSelector } from "embedding-sdk-bundle/store";
+import { getPluginsReady } from "embedding-sdk-bundle/store/selectors";
+import { isEmbeddingEajs } from "metabase/embedding-sdk/config";
+
+/**
+ * Returns whether the EE plugins have been initialized.
+ *
+ * For EAJS, plugins are always initialized in the entrypoint,
+ * so this always returns true.
+ *
+ * Can be used inside PublicComponentWrapper or public hooks
+ * to gate rendering until plugins are ready.
+ */
+export function useArePluginsReady(): boolean {
+ const pluginsReady = useSdkSelector(getPluginsReady);
+
+ // EAJS initializes plugins in its own entrypoint,
+ // so they are always ready.
+ if (isEmbeddingEajs()) {
+ return true;
+ }
+
+ return pluginsReady;
+}
diff --git a/frontend/src/embedding-sdk-bundle/store/reducer.ts b/frontend/src/embedding-sdk-bundle/store/reducer.ts
index 6d8e08c651c..c8025defc0f 100644
--- a/frontend/src/embedding-sdk-bundle/store/reducer.ts
+++ b/frontend/src/embedding-sdk-bundle/store/reducer.ts
@@ -53,6 +53,9 @@ export const setUsageProblem = createAction<SdkUsageProblem | null>(
SET_USAGE_PROBLEM,
);
+const SET_PLUGINS_READY = "sdk/SET_PLUGINS_READY";
+export const setPluginsReady = createAction<boolean>(SET_PLUGINS_READY);
+
const initialState: SdkState = {
isGuestEmbed: null,
metabaseInstanceUrl: "",
@@ -70,6 +73,7 @@ const initialState: SdkState = {
usageProblem: null,
errorComponent: null,
fetchRefreshTokenFn: null,
+ pluginsReady: false,
};
export const sdk = createReducer(initialState, (builder) => {
@@ -194,4 +198,8 @@ export const sdk = createReducer(initialState, (builder) => {
error,
};
});
+
+ builder.addCase(setPluginsReady, (state, action) => {
+ state.pluginsReady = action.payload;
+ });
});
diff --git a/frontend/src/embedding-sdk-bundle/store/selectors.ts b/frontend/src/embedding-sdk-bundle/store/selectors.ts
index 4cb58a43195..70a6f5220cc 100644
--- a/frontend/src/embedding-sdk-bundle/store/selectors.ts
+++ b/frontend/src/embedding-sdk-bundle/store/selectors.ts
@@ -40,6 +40,8 @@ export const getMetabaseInstanceVersion = (state: SdkStoreState) =>
export const getFetchRefreshTokenFn = (state: SdkStoreState) =>
state.sdk.fetchRefreshTokenFn;
+export const getPluginsReady = (state: SdkStoreState) => state.sdk.pluginsReady;
+
export const getAvailableFonts = (state: SdkStoreState) =>
getSetting(state, "available-fonts");
diff --git a/frontend/src/embedding-sdk-bundle/store/types.ts b/frontend/src/embedding-sdk-bundle/store/types.ts
index faac2c9da10..9c67d786b78 100644
--- a/frontend/src/embedding-sdk-bundle/store/types.ts
+++ b/frontend/src/embedding-sdk-bundle/store/types.ts
@@ -43,6 +43,7 @@ export type SdkState = {
usageProblem: null | SdkUsageProblem;
errorComponent: null | SdkErrorComponent;
fetchRefreshTokenFn: null | MetabaseAuthConfig["fetchRequestToken"];
+ pluginsReady: boolean;
};
export interface SdkStoreState extends State {
diff --git a/frontend/src/embedding-sdk-bundle/test/mocks/state.ts b/frontend/src/embedding-sdk-bundle/test/mocks/state.ts
index 52cb334e65f..1b5cc8123e3 100644
--- a/frontend/src/embedding-sdk-bundle/test/mocks/state.ts
+++ b/frontend/src/embedding-sdk-bundle/test/mocks/state.ts
@@ -38,6 +38,7 @@ export const createMockSdkState = ({
errorComponent: null,
error: null,
fetchRefreshTokenFn: null,
+ pluginsReady: true,
...opts,
};
};