Skip to content

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

Fix history.back 404 when Metabase is under a sub-path in interactive embedding (#75700)

Sébastienb55bed1cc1fa
diff --git a/frontend/src/metabase/embedding/interactive-embedding/initialize.ts b/frontend/src/metabase/embedding/interactive-embedding/initialize.ts
index 8ed2a7bbfb5..bfaa5694309 100644
--- a/frontend/src/metabase/embedding/interactive-embedding/initialize.ts
+++ b/frontend/src/metabase/embedding/interactive-embedding/initialize.ts
@@ -37,7 +37,7 @@ export function initializeInteractiveEmbedding(dispatch: Dispatch) {
window.addEventListener("message", (e) => {
if (e.source === window.parent && e.data.metabase) {
if (e.data.metabase.type === "location") {
- dispatch(push(e.data.metabase.location));
+ dispatch(push(getLocationWithoutBasename(e.data.metabase.location)));
}
}
});
@@ -45,6 +45,31 @@ export function initializeInteractiveEmbedding(dispatch: Dispatch) {
}
}
+/**
+ * The location synced from the parent frame is the iframe's `window.location`,
+ * whose `pathname` already includes the sub-path Metabase is mounted under
+ * (the react-router basename, e.g. `/metabase`). Because `push` re-prepends the
+ * basename, pushing the raw pathname would double the sub-path
+ * (`/metabase/metabase/...`) and 404 — once per back/forward navigation. Strip
+ * the basename so `push` re-adds it exactly once. (metabase EMB-106)
+ */
+export function getLocationWithoutBasename(
+ location: Partial<Location>,
+): Partial<Location> {
+ const basename = (window.MetabaseRoot ?? "/").replace(/\/+$/, "");
+ const { pathname } = location;
+
+ if (!basename || pathname == null) {
+ return location;
+ }
+
+ if (pathname === basename || pathname.startsWith(`${basename}/`)) {
+ return { ...location, pathname: pathname.slice(basename.length) || "/" };
+ }
+
+ return location;
+}
+
type LocationMessage = {
type: "location";
location: Partial<Location>;
diff --git a/frontend/src/metabase/embedding/interactive-embedding/initialize.unit.spec.ts b/frontend/src/metabase/embedding/interactive-embedding/initialize.unit.spec.ts
new file mode 100644
index 00000000000..3bc92dfc4e1
--- /dev/null
+++ b/frontend/src/metabase/embedding/interactive-embedding/initialize.unit.spec.ts
@@ -0,0 +1,74 @@
+import { getLocationWithoutBasename } from "./initialize";
+
+describe("getLocationWithoutBasename", () => {
+ const originalMetabaseRoot = window.MetabaseRoot;
+
+ afterEach(() => {
+ window.MetabaseRoot = originalMetabaseRoot;
+ });
+
+ it("strips the sub-path basename so push doesn't double it (metabase EMB-106)", () => {
+ window.MetabaseRoot = "/metabase/";
+
+ expect(
+ getLocationWithoutBasename({ pathname: "/metabase/auto/dashboard/1" }),
+ ).toEqual({ pathname: "/auto/dashboard/1" });
+ });
+
+ it("tolerates a basename without a trailing slash", () => {
+ window.MetabaseRoot = "/metabase";
+
+ expect(
+ getLocationWithoutBasename({ pathname: "/metabase/auto/dashboard/1" }),
+ ).toEqual({ pathname: "/auto/dashboard/1" });
+ });
+
+ it("maps the basename root to '/'", () => {
+ window.MetabaseRoot = "/metabase/";
+
+ expect(getLocationWithoutBasename({ pathname: "/metabase" })).toEqual({
+ pathname: "/",
+ });
+ });
+
+ it("preserves search and hash while stripping the basename", () => {
+ window.MetabaseRoot = "/metabase/";
+
+ expect(
+ getLocationWithoutBasename({
+ pathname: "/metabase/auto/dashboard/1",
+ search: "?foo=bar",
+ hash: "#baz",
+ }),
+ ).toEqual({
+ pathname: "/auto/dashboard/1",
+ search: "?foo=bar",
+ hash: "#baz",
+ });
+ });
+
+ it("does not strip when there is no sub-path", () => {
+ window.MetabaseRoot = "/";
+
+ expect(
+ getLocationWithoutBasename({ pathname: "/auto/dashboard/1" }),
+ ).toEqual({ pathname: "/auto/dashboard/1" });
+ });
+
+ it("does not strip a path that only shares a prefix with the basename", () => {
+ window.MetabaseRoot = "/metabase/";
+
+ // `/metabased` is not under the `/metabase` basename
+ expect(
+ getLocationWithoutBasename({ pathname: "/metabased/auto/dashboard/1" }),
+ ).toEqual({ pathname: "/metabased/auto/dashboard/1" });
+ });
+
+ it("leaves the location untouched when there is no pathname", () => {
+ window.MetabaseRoot = "/metabase/";
+
+ expect(getLocationWithoutBasename({ search: "?foo=bar" })).toEqual({
+ search: "?foo=bar",
+ });
+ });
+});