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
Make custom-viz dev-mode hot reload reliable (#76739)
Oleh Kbfdce241fec8
diff --git a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.tsindex b75b370005e..e61bbf5512e 100644--- a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts+++ b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts@@ -1398,22 +1398,24 @@ describe("admin > custom visualizations", () => {const QUESTION_NAME = "Custom Viz Dev Mode Question Test";let devServerPid: number | null = null;+ before(() => {+ cy.exec(`mkdir -p ${tmpDir}`);+ cy.log("Build the SDK so we can use the repo-local CLI");+ cy.exec(+ `cd "${sdkDir}" && bun install --frozen-lockfile && bun run build`,+ {+ timeout: TIMEOUT,+ },+ );+ });+beforeEach(() => {H.restore("postgres-writable");cy.signInAsAdmin();H.activateToken("bleeding-edge");H.updateSetting("csp-img-enabled", true);H.updateSetting("custom-viz-enabled", true);- });- before(() => {- cy.exec(`mkdir -p ${tmpDir}`);- cy.log("Build the SDK so we can use the repo-local CLI");- cy.exec(`cd "${sdkDir}" && bun install && bun run build`, {- timeout: TIMEOUT,- });-- // Scaffold the boilerplate plugin using the init CLI command.cy.exec(`rm -rf "${projectDir}"`, { timeout: TIMEOUT });cy.exec(`cd "${tmpDir}" && node "${cliPath}" init "${CUSTOM_VIZ_DEV_PROJECT_NAME}"`,@@ -1463,7 +1465,7 @@ describe("admin > custom visualizations", () => {// Install dependencies in the tmp plugin folder.cy.exec(`cd "${projectDir}" && npm i`, { timeout: TIMEOUT });- // Start the plugin dev server and keep it running+cy.task<{ pid: number }>("startCustomVizDevServer", {cwd: projectDir,}).then(({ pid }) => {@@ -1471,12 +1473,11 @@ describe("admin > custom visualizations", () => {});});- after(() => {- if (devServerPid == null) {- return;+ afterEach(() => {+ if (devServerPid != null) {+ cy.task("stopCustomVizDevServer", devServerPid);+ devServerPid = null;}-- cy.task("stopCustomVizDevServer", devServerPid);});it("should load a dev-only plugin from a local dev server URL and use it in a question", () => {diff --git a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.cljindex ae38c1cbda7..a3d73854f7e 100644--- a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj+++ b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj@@ -320,12 +320,17 @@(defn fetch-dev-bundle"Fetch a JS bundle from a dev base URL.- Returns {:content str :hash str} or nil."+ Returns {:content str :hash str}, or nil when the dev server is transiently+ unavailable (e.g. mid-rebuild)."[^String base-url]- (let [content (:body (http/get (dev-url base-url bundle-rel-path)- (assoc http-opts :as :string)))]- {:content content- :hash (string-hash content)}))+ (try+ (let [content (:body (http/get (dev-url base-url bundle-rel-path)+ (assoc http-opts :as :string)))]+ {:content content+ :hash (string-hash content)})+ (catch Exception e+ (log/debugf "Failed to fetch dev bundle from %s: %s" base-url (ex-message e))+ nil)))(defn fetch-dev-manifest"Fetch and parse the manifest from a dev base URL.diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsx b/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsxindex a8c2092e823..65fb796ef24 100644--- a/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsx+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsx@@ -17,6 +17,7 @@ import { useEmbeddingEntityContext } from "metabase/embedding/context";import { PLUGIN_CUSTOM_VIZ } from "metabase/plugins";import { useColorScheme } from "metabase/ui";import { getSubpathSafeUrl } from "metabase/urls";+import { retry } from "metabase/utils/retry";import visualizations, { registerVisualization } from "metabase/visualizations";import {getCustomPluginIdentifier,@@ -53,6 +54,11 @@ const failedPluginHashes = new Map<CustomVizPluginRuntime["bundle_hash"]>();+// Monotonic per-plugin load counters for latest-wins ordering of overlapping+// dev reloads.+const loadStartedSeqByPluginId = new Map<CustomVizPluginId, number>();+const loadAppliedSeqByPluginId = new Map<CustomVizPluginId, number>();+/*** Remove a previously-loaded custom-viz display from the global* visualizations registry and drop its load/failure cache entries, so a@@ -68,6 +74,8 @@ export function unregisterCustomVizDisplay(display: VisualizationDisplay) {PLUGIN_CUSTOM_VIZ.releaseCustomVizAsset(id);loadedPlugins.delete(id);failedPluginHashes.delete(id);+ loadStartedSeqByPluginId.delete(id);+ loadAppliedSeqByPluginId.delete(id);}}}@@ -167,9 +175,18 @@ export function useAutoLoadCustomVizPlugin(const { sandboxMode = "hosted" } = options;const { plugins, disabled } = useCustomVizPlugins();const [sendToast] = useToast();- const [loading, setLoading] = useState(false);+ const [loading, setLoadingState] = useState(false);const loadingRef = useRef<string | null>(null);+ const loadingCountRef = useRef(0);+ const setLoading = useCallback((isLoading: boolean) => {+ loadingCountRef.current = Math.max(+ 0,+ loadingCountRef.current + (isLoading ? 1 : -1),+ );+ setLoadingState(loadingCountRef.current > 0);+ }, []);+const onInfo = useCallback((message: string) => {sendToast({ message });@@ -203,7 +220,7 @@ export function useAutoLoadCustomVizPlugin(setLoading(false);}},- [onInfo, sandboxMode],+ [onInfo, sandboxMode, setLoading],);useEffect(() => {@@ -279,6 +296,13 @@ export type LoadCustomVizPluginOptions = {sandboxMode?: SandboxMode;};+class BundleFetchError extends Error {+ constructor(readonly status: number) {+ super(`HTTP ${status}`);+ this.name = "BundleFetchError";+ }+}+/*** Dynamically load a custom viz plugin bundle, call its factory,* decompose the returned definition, and register it as a Metabase@@ -302,6 +326,9 @@ export async function loadCustomVizPlugin(ensureVizApi();+ const loadSeq = (loadStartedSeqByPluginId.get(plugin.id) ?? 0) + 1;+ loadStartedSeqByPluginId.set(plugin.id, loadSeq);+try {const params: Record<string, string> = {};if (cacheBustSuffix) {@@ -309,15 +336,27 @@ export async function loadCustomVizPlugin(} else if (currentHash) {params.v = currentHash;}- const res = await api.fetch({- method: "GET",- url: plugin.bundle_url,- params,- cache: "no-store",++ const fetchBundle = async () => {+ const res = await api.fetch({+ method: "GET",+ url: plugin.bundle_url,+ params,+ cache: "no-store",+ });+ if (!res.ok) {+ throw new BundleFetchError(res.status);+ }+ return res;+ };+ const isLatest = () => loadStartedSeqByPluginId.get(plugin.id) === loadSeq;++ const res = await retry(fetchBundle, {+ maxRetries: plugin.dev_bundle_url ? 4 : 0,+ delayMs: () => 300,+ shouldRetry: (error) =>+ isLatest() && error instanceof BundleFetchError && error.status >= 500,});- if (!res.ok) {- throw new Error(`HTTP ${res.status}`);- }const text = await res.text();@@ -384,6 +423,10 @@ export async function loadCustomVizPlugin(isDev: Boolean(plugin.dev_bundle_url),});+ if (!isLatest()) {+ return identifier;+ }+// Use registerVisualization for first load; overwrite directly for updates// (registerVisualization throws on duplicate identifiers).// Check visualizations map directly (not loadedPlugins) because HMR may@@ -397,10 +440,18 @@ export async function loadCustomVizPlugin(identifier,hash: currentHash,});+ loadAppliedSeqByPluginId.set(plugin.id, loadSeq);failedPluginHashes.delete(plugin.id);return identifier;} catch (error) {+ // Suppress the failure if a newer load has superseded this one+ const superseded =+ (loadAppliedSeqByPluginId.get(plugin.id) ?? 0) > loadSeq ||+ (loadStartedSeqByPluginId.get(plugin.id) ?? 0) > loadSeq;+ if (superseded) {+ return null;+ }console.error(t`Failed to load plugin "${plugin.display_name}":`, error);if (!failedPluginHashes.has(plugin.id)) {onInfo?.(