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
Split documents into its own chunk (#79349)
Romeo Van Snickb750f8d8fa0e
diff --git a/frontend/src/metabase/common/components/ModalRoute.tsx b/frontend/src/metabase/common/components/ModalRoute.tsxindex 0d8ef9d545a..5245ee37cb2 100644--- a/frontend/src/metabase/common/components/ModalRoute.tsx+++ b/frontend/src/metabase/common/components/ModalRoute.tsx@@ -1,6 +1,6 @@import { useCallback } from "react";-import type { Location } from "metabase/router";+import type { Location, RouteObject } from "metabase/router";import { Route, useLocation, useNavigate, useParams } from "metabase/router";import { Modal, type ModalProps } from "metabase/ui";@@ -17,6 +17,8 @@ export type ModalComponentProps = {onClose: () => void;};+type ModalComponent = React.ComponentType<ModalComponentProps>;+type ModalRouteOptions = {/*** Render the modal component on its own instead of wrapping it in a `<Modal>`,@@ -35,8 +37,38 @@ type ModalRouteOptions = {*/export function modalRoute(path: string,- ComposedModal: React.ComponentType<ModalComponentProps>,- { noWrap = false, modalProps }: ModalRouteOptions = {},+ ComposedModal: ModalComponent,+ options: ModalRouteOptions = {},+) {+ const ModalRouteComponent = createModalRouteComponent(ComposedModal, options);++ // Keyed for the plugin route arrays, which React renders as a list.+ return <Route key={path} path={path} element={<ModalRouteComponent />} />;+}++/**+ * `modalRoute` for a modal that lives in a code-split chunk.+ *+ * `route.lazy` cannot supply `children`, so a lazy page's modal children have to+ * be route objects of their own. This keeps the path static, which is what+ * matching needs, and defers only the modal itself.+ */+export function lazyModalRoute(+ path: string,+ loadModal: () => Promise<ModalComponent>,+ options: ModalRouteOptions = {},+): RouteObject {+ return {+ path,+ lazy: async () => ({+ Component: createModalRouteComponent(await loadModal(), options),+ }),+ };+}++function createModalRouteComponent(+ ComposedModal: ModalComponent,+ { noWrap = false, modalProps }: ModalRouteOptions,) {function ModalRouteComponent() {const params = useParams();@@ -73,6 +105,5 @@ export function modalRoute(ComposedModal.displayName || ComposedModal.name}]`;- // Keyed for the plugin route arrays, which React renders as a list.- return <Route key={path} path={path} element={<ModalRouteComponent />} />;+ return ModalRouteComponent;}diff --git a/frontend/src/metabase/common/components/ModalRoute.unit.spec.tsx b/frontend/src/metabase/common/components/ModalRoute.unit.spec.tsxindex 8ce59bd61b4..57e9cc9d2ab 100644--- a/frontend/src/metabase/common/components/ModalRoute.unit.spec.tsx+++ b/frontend/src/metabase/common/components/ModalRoute.unit.spec.tsx@@ -1,9 +1,13 @@import userEvent from "@testing-library/user-event";-import { renderWithProviders, screen } from "__support__/ui";+import { renderRoutes, renderWithProviders, screen } from "__support__/ui";import { Outlet, Route } from "metabase/router";-import { type ModalComponentProps, modalRoute } from "./ModalRoute";+import {+ type ModalComponentProps,+ lazyModalRoute,+ modalRoute,+} from "./ModalRoute";function CollectionPage() {return (@@ -124,3 +128,66 @@ describe("modalRoute", () => {expect(screen.queryByRole("dialog")).not.toBeInTheDocument();});});++describe("lazyModalRoute", () => {+ function setupLazy(initialRoute: string) {+ const { router } = renderRoutes(+ [+ {+ path: "collection/:slug",+ element: <CollectionPage />,+ children: [+ lazyModalRoute("archive", async () => TestModal, { modalProps }),+ ],+ },+ ],+ { initialRoute },+ );++ return { router, pathname: () => router?.location.pathname };+ }++ it("renders the page and the modal when deep-linking to a modal URL", async () => {+ setupLazy("/collection/5/archive");++ expect(await screen.findByText("Modal for 5")).toBeInTheDocument();+ expect(screen.getByText("Collection page")).toBeInTheDocument();+ expect(screen.getByRole("dialog")).toBeInTheDocument();+ });++ it("returns to the parent URL on close", async () => {+ const { pathname } = setupLazy("/collection/5/archive");++ expect(await screen.findByText("Modal for 5")).toBeInTheDocument();+ await close();++ expect(pathname()).toBe("/collection/5");+ expect(screen.queryByText("Modal for 5")).not.toBeInTheDocument();+ expect(screen.getByText("Collection page")).toBeInTheDocument();+ });++ // Opening the modal from the page it belongs to leaves that page mounted while+ // the chunk loads, so the content behind the modal does not blank.+ it("keeps the page mounted while the modal loads", async () => {+ const { router } = renderRoutes(+ [+ {+ path: "collection/:slug",+ element: <CollectionPage />,+ children: [+ lazyModalRoute("archive", async () => TestModal, { modalProps }),+ ],+ },+ ],+ { initialRoute: "/collection/5" },+ );++ router?.navigate("/collection/5/archive");++ expect(screen.getByText("Collection page")).toBeInTheDocument();+ expect(screen.queryByText("Modal for 5")).not.toBeInTheDocument();++ expect(await screen.findByText("Modal for 5")).toBeInTheDocument();+ expect(screen.getByText("Collection page")).toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/routes.tsx b/frontend/src/metabase/routes.tsxindex 580cef25414..cefe784e182 100644--- a/frontend/src/metabase/routes.tsx+++ b/frontend/src/metabase/routes.tsx@@ -22,7 +22,10 @@ import CollectionLanding from "metabase/collections/components/CollectionLandingimport { MoveCollectionModal } from "metabase/collections/components/MoveCollectionModal";import { TrashCollectionLanding } from "metabase/collections/components/TrashCollectionLanding";import { Unauthorized } from "metabase/common/components/ErrorPages";-import { modalRoute } from "metabase/common/components/ModalRoute";+import {+ lazyModalRoute,+ modalRoute,+} from "metabase/common/components/ModalRoute";import { MoveQuestionsIntoDashboardsModal } from "metabase/common/components/MoveQuestionsIntoDashboardsModal";import { NotFoundFallbackPage } from "metabase/common/components/NotFoundFallbackPage";import { UnsubscribePage } from "metabase/common/components/Unsubscribe";@@ -34,8 +37,6 @@ import { AutomaticDashboardApp } from "metabase/dashboard/containers/AutomaticDaimport { DashboardApp } from "metabase/dashboard/containers/DashboardApp/DashboardApp";import { getDataStudioRoutes } from "metabase/data-studio/routes";import { TableDetailPage } from "metabase/detail-view/pages/TableDetailPage";-import { CommentsSidesheet } from "metabase/documents/components/CommentsSidesheet";-import { DocumentPageOuter } from "metabase/documents/routes";import { getRoutes as getExplorationsRoutes } from "metabase/explorations/routes";import { LandingPageRedirect } from "metabase/home/components/LandingPageRedirect";import { Onboarding } from "metabase/home/components/Onboarding";@@ -132,10 +133,24 @@ const metabotQueryBuilder = () =>);/**- * Hovering a link into the query builder starts the fetch, so the chunk is- * usually in hand by the time the click lands. The router still awaits `lazy`- * and still commits the location a tick late, so this removes the round trip- * rather than the asynchrony. See `lazy-route.unit.spec.tsx`.+ * Documents, in their own chunk. It carries the rich text editing stack, which+ * nothing outside the document page needs on first paint.+ */+const documentPage = () =>+ import("metabase/documents/routes").then(({ DocumentPageOuter }) => ({+ Component: DocumentPageOuter,+ }));++const commentsSidesheet = () =>+ import("metabase/documents/components/CommentsSidesheet").then(+ ({ CommentsSidesheet }) => CommentsSidesheet,+ );++/**+ * Hovering a link into one of these chunks starts the fetch, so it is usually in+ * hand by the time the click lands. The router still awaits `lazy` and still+ * commits the location a tick late, so this removes the round trip rather than+ * the asynchrony. See `lazy-route.unit.spec.tsx`.** The paths are prefixes, so `/table/` also covers the table detail page, which* is not the query builder. The chunk is fetched once either way, and someone@@ -145,6 +160,12 @@ registerPagePrefetch("/question", queryBuilder);registerPagePrefetch("/model", queryBuilder);registerPagePrefetch("/table/", queryBuilder);registerPagePrefetch("/question/ask", metabotQueryBuilder);+registerPagePrefetch("/document/", documentPage);+// The sidesheet is a chunk of its own, and its route carries the document id+// before the segment that names it, so a prefix cannot single it out. Registered+// against the document prefix instead: 15 kb fetched alongside a page of 337 kb,+// in exchange for the sidesheet already being there when it is opened.+registerPagePrefetch("/document/", commentsSidesheet);export const getRoutes = (store: AppStore): RouteObject[] => [{@@ -211,12 +232,12 @@ export const getRoutes = (store: AppStore): RouteObject[] => [{path: "document/:entityId",- element: <DocumentPageOuter />,- children: toRouteObjects(- modalRoute("comments/:childTargetId", CommentsSidesheet, {+ lazy: documentPage,+ children: [+ lazyModalRoute("comments/:childTargetId", commentsSidesheet, {noWrap: true,}),- ),+ ],},{