Skip to content

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

Fixed DataAppLink opening the iframe URL in new tabs by linking to the app's Metabase page, and the dev server not rebuilding when queries/ or actions/ change. (#84733)

Timofey Kachalov50178bce5214
diff --git a/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.tsx b/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.tsx
index bfa5013ea23..4e250dcde66 100644
--- a/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.tsx
@@ -1,7 +1,7 @@
import type { AnchorHTMLAttributes, MouseEvent, ReactNode } from "react";
import {
- getBasename,
+ getPageBasename,
navigate,
} from "embedding-sdk-package/lib/private/data-app-routing";
@@ -18,6 +18,10 @@ export interface DataAppLinkProps extends Omit<
*
* Renders a plain anchor rather than a router `<Link>`: a data app is mounted
* outside the app's route tree, so it has no router context to read.
+ *
+ * The `href` is the app's page in Metabase, so opening the link in a new tab or
+ * copying its address lands on the app rather than on its iframe, which refuses
+ * to load as a top-level page. A plain click navigates inside the app.
*/
export const DataAppLink = ({
to,
@@ -27,7 +31,7 @@ export const DataAppLink = ({
rel,
...rest
}: DataAppLinkProps) => {
- const href = getBasename() + to;
+ const href = getPageBasename() + to;
const isExternalTarget = target != null && target !== "_self";
const resolvedRel = rel ?? (isExternalTarget ? "noopener noreferrer" : rel);
diff --git a/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.unit.spec.tsx
new file mode 100644
index 00000000000..3fec85c5b1b
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-package/components/public/DataAppLink/DataAppLink.unit.spec.tsx
@@ -0,0 +1,44 @@
+import { render, screen } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+
+import { DataAppLink } from "./DataAppLink";
+
+interface SetupOpts {
+ pathname: string;
+}
+
+const setup = ({ pathname }: SetupOpts) => {
+ window.history.replaceState(null, "", pathname);
+
+ render(<DataAppLink to="/customers/42">Customer</DataAppLink>);
+
+ return { link: screen.getByRole("link", { name: "Customer" }) };
+};
+
+describe("DataAppLink", () => {
+ it("links to the app's page, so opening it in a new tab shows the app", () => {
+ const { link } = setup({ pathname: "/embed/apps/sales/orders" });
+
+ expect(link).toHaveAttribute("href", "/apps/sales/customers/42");
+ });
+
+ it("keeps a subpath install's root in the link", () => {
+ const { link } = setup({ pathname: "/mb/embed/apps/sales/orders" });
+
+ expect(link).toHaveAttribute("href", "/mb/apps/sales/customers/42");
+ });
+
+ it("links to the bare sub-path in the dev preview", () => {
+ const { link } = setup({ pathname: "/orders" });
+
+ expect(link).toHaveAttribute("href", "/customers/42");
+ });
+
+ it("navigates within the app's frame on a plain click", async () => {
+ const { link } = setup({ pathname: "/embed/apps/sales/orders" });
+
+ await userEvent.click(link);
+
+ expect(window.location.pathname).toBe("/embed/apps/sales/customers/42");
+ });
+});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts
index 5817ad0d429..822c2460bd4 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts
@@ -2,6 +2,10 @@ import path from "node:path";
import type { Plugin } from "vite";
+import {
+ ACTION_DEFINITIONS,
+ QUERY_DEFINITIONS,
+} from "../../data-app-resources/discover";
import { validateDataAppManifest } from "../config/validate-manifest";
import { DATA_APP_REBUILT_EVENT } from "../constants/bundle";
import { DATA_APP_DIAGNOSTICS_CHANGED_EVENT } from "../constants/diagnostics-channel";
@@ -17,6 +21,20 @@ import {
resolveDataAppVirtualId,
} from "./virtual-modules";
+// `src/` imports the definitions from the root-level `queries/` and `actions/`,
+// so the bundle is built from all three.
+const BUNDLED_DIRECTORIES: readonly string[] = [
+ "src",
+ QUERY_DEFINITIONS.directory,
+ ACTION_DEFINITIONS.directory,
+];
+
+const isBundledFile = (root: string, file: string) => {
+ const [topDirectory] = path.relative(root, file).split(path.sep);
+
+ return BUNDLED_DIRECTORIES.includes(topDirectory);
+};
+
export function dataAppSandboxDevPlugin(
appSlug: string,
allowedHosts: string[],
@@ -56,8 +74,6 @@ export function dataAppSandboxDevPlugin(
}),
);
- const srcDir = `${path.sep}src${path.sep}`;
-
server.watcher.on("all", async (_event, file) => {
if (path.basename(file) === DATA_APP_MANIFEST_FILE_NAME) {
server.ws.send({
@@ -68,7 +84,7 @@ export function dataAppSandboxDevPlugin(
return;
}
- if (file.includes(srcDir) && (await bundle.rebuild())) {
+ if (isBundledFile(root, file) && (await bundle.rebuild())) {
server.ws.send({ type: "custom", event: DATA_APP_REBUILT_EVENT });
}
});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts
index f4e36e9b1fa..e94351417a8 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts
@@ -328,7 +328,28 @@ describe("dataAppSandboxDevPlugin", () => {
expect(mockedBuild).toHaveBeenCalledTimes(3);
});
- it("ignores changes outside src/", async () => {
+ it("rebuilds when a query or action definition changes", async () => {
+ const { server } = await setup();
+
+ await server.watcher.emit(
+ "all",
+ "change",
+ path.join("/app", "queries", "orders.query.ts"),
+ );
+ await server.watcher.emit(
+ "all",
+ "change",
+ path.join("/app", "actions", "orders.action.ts"),
+ );
+
+ expect(mockedBuild).toHaveBeenCalledTimes(3);
+ expect(server.ws.send).toHaveBeenCalledWith({
+ type: "custom",
+ event: DATA_APP_REBUILT_EVENT,
+ });
+ });
+
+ it("ignores changes outside the app's src/, queries/, and actions/", async () => {
const { server } = await setup();
await server.watcher.emit(
@@ -336,6 +357,11 @@ describe("dataAppSandboxDevPlugin", () => {
"change",
path.join("/app", "README.md"),
);
+ await server.watcher.emit(
+ "all",
+ "change",
+ path.join("/app", "node_modules", "some-package", "src", "index.js"),
+ );
expect(mockedBuild).toHaveBeenCalledTimes(1);
expect(server.ws.send).not.toHaveBeenCalled();
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts
index 8f0e506b459..29ac922c755 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts
@@ -18,7 +18,7 @@ export const QUERY_DEFINITIONS: DefinitionKind = {
idKey: "savedQuestionEntityId",
};
-const ACTION_DEFINITIONS: DefinitionKind = {
+export const ACTION_DEFINITIONS: DefinitionKind = {
directory: "actions",
idKey: "copiedActionEntityId",
};
diff --git a/enterprise/frontend/src/embedding-sdk-package/lib/private/data-app-routing.ts b/enterprise/frontend/src/embedding-sdk-package/lib/private/data-app-routing.ts
index ca6d301041d..a3e044d68e8 100644
--- a/enterprise/frontend/src/embedding-sdk-package/lib/private/data-app-routing.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/lib/private/data-app-routing.ts
@@ -1,17 +1,20 @@
// eslint-disable-next-line metabase/no-external-references-for-sdk-package-code
-import { DATA_APP_EMBED_PREFIX } from "metabase/urls/data-apps";
+import * as DataAppUrls from "metabase/urls/data-apps";
const escapeRegExp = (str: string): string =>
str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
-// Captures everything from the start of `pathname` up to and including the
-// data-app `:name` segment. Tolerates a Metabase subpath install
-// (`/mb/embed/apps/sales/…`) by allowing arbitrary characters before the
-// `DATA_APP_EMBED_PREFIX` literal.
-const DATA_APP_BASENAME_RE = new RegExp(
- `^(.*?${escapeRegExp(DATA_APP_EMBED_PREFIX)}/[^/]+)`,
+// Captures the Metabase subpath install root (`/mb` in `/mb/embed/apps/sales/…`,
+// empty without one) and the data-app `:name` segment.
+const DATA_APP_IFRAME_PATH_RE = new RegExp(
+ `^(.*?)${escapeRegExp(DataAppUrls.DATA_APP_EMBED_PREFIX)}/([^/]+)`,
);
+const matchIframePath = () =>
+ typeof window === "undefined"
+ ? null
+ : window.location.pathname.match(DATA_APP_IFRAME_PATH_RE);
+
/**
* The iframe URL's data-app prefix, including any Metabase subpath install root.
* `""` outside the iframe (the dev preview), where the sub-path is the raw pathname.
@@ -20,10 +23,27 @@ const DATA_APP_BASENAME_RE = new RegExp(
* `/mb/embed/apps/sales/customers/42` -> `/mb/embed/apps/sales`
* `/customers/42` -> `""`
*/
-export const getBasename = (): string =>
- typeof window === "undefined"
- ? ""
- : (window.location.pathname.match(DATA_APP_BASENAME_RE)?.[1] ?? "");
+export const getBasename = (): string => {
+ const match = matchIframePath();
+
+ return match
+ ? `${match[1]}${DataAppUrls.DATA_APP_EMBED_PREFIX}/${match[2]}`
+ : "";
+};
+
+/**
+ * The prefix of the app's own page in Metabase, the URL a person can open or
+ * share, including any subpath install root. `""` in the dev preview.
+ *
+ * `/embed/apps/sales/customers/42` -> `/apps/sales`
+ * `/mb/embed/apps/sales/customers/42` -> `/mb/apps/sales`
+ * `/customers/42` -> `""`
+ */
+export const getPageBasename = (): string => {
+ const match = matchIframePath();
+
+ return match ? `${match[1]}${DataAppUrls.DATA_APP_ROOT_URL}/${match[2]}` : "";
+};
const subscribers = new Set<() => void>();
let detachPopstate: (() => void) | undefined;