The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix SDK Storybook chunk loading broken after #75985 (#78664)
Sébastien0e9c0eab6209
diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.tsindex 54f67dbbcc7..3786221ec46 100644--- a/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.ts+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.ts@@ -31,7 +31,7 @@// so the assignment below is rewritten to set the runtime's `publicPath`.declare let __webpack_public_path__: string;-const resolveAssetBaseUrl = (): string | undefined => {+export const resolveAssetBaseUrl = (): string | undefined => {const fromGlobal =typeof window !== "undefined"? window.METABASE_EMBEDDING_SDK_ASSET_BASE_URL@@ -48,7 +48,12 @@ const resolveAssetBaseUrl = (): string | undefined => {// Unjustified type cast. FIXMEconst scriptUrl = (document.currentScript as HTMLScriptElement | null)?.src;- if (!scriptUrl) {+ // Only derive the base from `document.currentScript` when it is the SDK entry+ // itself. In Storybook (and other hosts) the current script is a+ // bundle-specific file — e.g. `.../embedding-sdk/<story>.iframe.bundle.js` —+ // and deriving the base from it would point on-demand chunks at the wrong+ // directory and fail to load them.+ if (!scriptUrl || !/embedding-sdk\.js(\?|$)/.test(scriptUrl)) {return undefined;}@@ -61,5 +66,3 @@ const assetBaseUrl = resolveAssetBaseUrl();if (assetBaseUrl) {__webpack_public_path__ = assetBaseUrl;}--export {};diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.unit.spec.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.unit.spec.tsnew file mode 100644index 00000000000..a79ea74c229--- /dev/null+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-public-path.unit.spec.ts@@ -0,0 +1,61 @@+import { resolveAssetBaseUrl } from "./sdk-public-path";++const setCurrentScriptSrc = (src: string | undefined) => {+ // The code under test only reads `.src`, so a minimal stub stands in for a+ // real HTMLScriptElement here.+ const currentScript = src === undefined ? null : { src };++ Object.defineProperty(document, "currentScript", {+ configurable: true,+ value: currentScript,+ });+};++describe("resolveAssetBaseUrl", () => {+ afterEach(() => {+ delete window.METABASE_EMBEDDING_SDK_ASSET_BASE_URL;+ setCurrentScriptSrc(undefined);+ });++ it("prefers the base URL exposed on window by the bootstrap", () => {+ window.METABASE_EMBEDDING_SDK_ASSET_BASE_URL =+ "https://cdn.example.com/app/embedding-sdk/";+ setCurrentScriptSrc("https://cdn.example.com/app/embedding-sdk.js");++ expect(resolveAssetBaseUrl()).toBe(+ "https://cdn.example.com/app/embedding-sdk/",+ );+ });++ it("derives the base from the SDK entry script when no window value is set", () => {+ setCurrentScriptSrc("https://cdn.example.com/app/embedding-sdk.js");++ expect(resolveAssetBaseUrl()).toBe(+ "https://cdn.example.com/app/embedding-sdk/",+ );+ });++ it("derives the base from the SDK entry script with a query string", () => {+ setCurrentScriptSrc("https://cdn.example.com/app/embedding-sdk.js?v=123");++ expect(resolveAssetBaseUrl()).toBe(+ "https://cdn.example.com/app/embedding-sdk/",+ );+ });++ it("returns undefined when there is no current script and no window value", () => {+ setCurrentScriptSrc(undefined);++ expect(resolveAssetBaseUrl()).toBeUndefined();+ });++ it("returns undefined when the current script is a Storybook bundle, not the SDK entry", () => {+ // In Storybook `document.currentScript` points at a per-story bundle rather+ // than `embedding-sdk.js`; deriving a base from it would break chunk loading.+ setCurrentScriptSrc(+ "http://localhost:6006/embedding-sdk/embedding-sdk-bundle-components-public-InteractiveQuestion-InteractiveQuestion-stories.iframe.bundle.js",+ );++ expect(resolveAssetBaseUrl()).toBeUndefined();+ });+});