Skip to content

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

Give the remaining app routes their own chunks (#80160)

Romeo Van Snickda126bb88c79
diff --git a/frontend/src/metabase/auth/components/Login/Login.tsx b/frontend/src/metabase/auth/components/Login/Login.tsx
index 44591cdb689..1ace1e60996 100644
--- a/frontend/src/metabase/auth/components/Login/Login.tsx
+++ b/frontend/src/metabase/auth/components/Login/Login.tsx
@@ -1,10 +1,11 @@
+import { useMount } from "react-use";
import { t } from "ttag";
import _ from "underscore";
import { usePageTitle } from "metabase/hooks/use-page-title";
import type { AuthProvider } from "metabase/plugins/types";
import { useSelector } from "metabase/redux";
-import { useParams, useSearchParams } from "metabase/router";
+import { prefetchPage, useParams, useSearchParams } from "metabase/router";
import { getApplicationName } from "metabase/selectors/whitelabel";
import { Box, Divider } from "metabase/ui";
@@ -25,6 +26,15 @@ export const Login = (): JSX.Element => {
usePageTitle(t`Login`);
+ // Signing in lands on the home page, which is a chunk of its own. Ask for it
+ // while the user types, so it is there when they arrive. A login that carries
+ // a redirect goes somewhere else, and that page loads on its own terms.
+ useMount(() => {
+ if (!redirectUrl) {
+ prefetchPage("/");
+ }
+ });
+
const [passwordProvider, otherProviders] = _.partition(
providers,
(provider) => provider.name === "password",
diff --git a/frontend/src/metabase/router/prefetch.ts b/frontend/src/metabase/router/prefetch.ts
index b9e4bfd397d..82bfd0dd3d7 100644
--- a/frontend/src/metabase/router/prefetch.ts
+++ b/frontend/src/metabase/router/prefetch.ts
@@ -1,8 +1,9 @@
type LoadPage = () => Promise<unknown>;
type Registration = {
- pathPrefix: string;
+ path: string;
load: LoadPage;
+ isExact: boolean;
isStarted: boolean;
};
@@ -16,13 +17,19 @@ const registrations: Registration[] = [];
* is a good signal that the click is coming, and acting on it buys most of that
* time back.
*
- * `pathPrefix` matches the start of a link's target, so one registration covers
- * a page and everything below it. Two pages may register the same prefix, which
- * is what a route that renders one page or another depending on the license
- * needs.
+ * `path` matches the start of a link's target, so one registration covers a page
+ * and everything below it. Two pages may register the same prefix, which is what
+ * a route that renders one page or another depending on the license needs.
+ *
+ * `exact` matches the whole path instead. The home page needs it: every path
+ * starts with "/", so a prefix registration would fetch it from any link.
*/
-export function registerPagePrefetch(pathPrefix: string, load: LoadPage): void {
- registrations.push({ pathPrefix, load, isStarted: false });
+export function registerPagePrefetch(
+ path: string,
+ load: LoadPage,
+ { exact = false }: { exact?: boolean } = {},
+): void {
+ registrations.push({ path, load, isExact: exact, isStarted: false });
}
/**
@@ -35,7 +42,11 @@ export function registerPagePrefetch(pathPrefix: string, load: LoadPage): void {
*/
export function prefetchPage(path: string): void {
for (const registration of registrations) {
- if (registration.isStarted || !path.startsWith(registration.pathPrefix)) {
+ const matches = registration.isExact
+ ? path === registration.path
+ : path.startsWith(registration.path);
+
+ if (registration.isStarted || !matches) {
continue;
}
diff --git a/frontend/src/metabase/router/prefetch.unit.spec.ts b/frontend/src/metabase/router/prefetch.unit.spec.ts
index c9f1340b7e2..5d66ae730ae 100644
--- a/frontend/src/metabase/router/prefetch.unit.spec.ts
+++ b/frontend/src/metabase/router/prefetch.unit.spec.ts
@@ -27,6 +27,18 @@ describe("prefetchPage", () => {
expect(load).toHaveBeenCalledTimes(1);
});
+ it("matches only the whole path when the registration is exact", () => {
+ const path = uniquePath();
+ const load = jest.fn().mockResolvedValue(undefined);
+ registerPagePrefetch(path, load, { exact: true });
+
+ prefetchPage(`${path}/42`);
+ expect(load).not.toHaveBeenCalled();
+
+ prefetchPage(path);
+ expect(load).toHaveBeenCalledTimes(1);
+ });
+
it("leaves an unrelated path alone", () => {
const load = jest.fn().mockResolvedValue(undefined);
registerPagePrefetch(uniquePath(), load);
diff --git a/frontend/src/metabase/routes.tsx b/frontend/src/metabase/routes.tsx
index a22c312c72b..1518f176345 100644
--- a/frontend/src/metabase/routes.tsx
+++ b/frontend/src/metabase/routes.tsx
@@ -9,18 +9,8 @@ import { Login } from "metabase/auth/components/Login";
import { Logout } from "metabase/auth/components/Logout";
import { ResetPassword } from "metabase/auth/components/ResetPassword";
import { SsoReload } from "metabase/auth/components/SsoReload";
-import {
- BrowseDatabases,
- BrowseMetrics,
- BrowseModels,
- BrowseSchemas,
- BrowseTables,
- TablePermalinkRedirect,
-} from "metabase/browse";
import { ArchiveCollectionModal } from "metabase/collections/components/ArchiveCollectionModal";
-import CollectionLanding from "metabase/collections/components/CollectionLanding";
import { MoveCollectionModal } from "metabase/collections/components/MoveCollectionModal";
-import { TrashCollectionLanding } from "metabase/collections/components/TrashCollectionLanding";
import { Unauthorized } from "metabase/common/components/ErrorPages";
import {
lazyModalRoute,
@@ -29,11 +19,8 @@ import {
import { MoveQuestionsIntoDashboardsModal } from "metabase/common/components/MoveQuestionsIntoDashboardsModal";
import { NotFoundFallbackPage } from "metabase/common/components/NotFoundFallbackPage";
import { UnsubscribePage } from "metabase/common/components/Unsubscribe";
-import { UserCollectionList } from "metabase/common/components/UserCollectionList";
import { getDataStudioRoutes } from "metabase/data-studio/routes";
import { getRoutes as getExplorationsRoutes } from "metabase/explorations/routes";
-import { LandingPageRedirect } from "metabase/home/components/LandingPageRedirect";
-import { Onboarding } from "metabase/home/components/Onboarding";
import { getMetabotRoutes } from "metabase/metabot/routes";
import { getMetricRoutes } from "metabase/metrics/routes";
import NewModelOptions from "metabase/models/containers/NewModelOptions";
@@ -63,9 +50,7 @@ import {
toRouteObjects,
useParams,
} from "metabase/router";
-import { SearchApp } from "metabase/search/containers/SearchApp";
import { RedirectIfSetup } from "metabase/setup/components/RedirectIfSetup";
-import { Setup } from "metabase/setup/components/Setup";
import { getCollectionTimelineRoutes } from "metabase/timelines/collections/routes";
import { LoadCurrentUser } from "./LoadCurrentUser";
@@ -149,6 +134,67 @@ const documentPage = () =>
}),
);
+const setupPage = () =>
+ import(
+ /* webpackChunkName: "setup" */ "metabase/setup/components/Setup"
+ ).then(({ Setup }) => ({ Component: Setup }));
+
+/**
+ * The home page, in its own chunk. The route also covers the redirect to a
+ * configured landing page, so an instance that sets one fetches this chunk once
+ * before it leaves "/".
+ */
+const landingPage = () =>
+ import(
+ /* webpackChunkName: "home" */ "metabase/home/components/LandingPageRedirect"
+ ).then(({ LandingPageRedirect }) => ({ Component: LandingPageRedirect }));
+
+const onboardingPage = () =>
+ import(
+ /* webpackChunkName: "onboarding" */ "metabase/home/components/Onboarding"
+ ).then(({ Onboarding }) => ({ Component: Onboarding }));
+
+const searchApp = () =>
+ import(
+ /* webpackChunkName: "search" */ "metabase/search/containers/SearchApp"
+ ).then(({ SearchApp }) => ({ Component: SearchApp }));
+
+const collectionLanding = () =>
+ import(
+ /* webpackChunkName: "collection" */ "metabase/collections/components/CollectionLanding"
+ ).then((module) => ({ Component: module.default }));
+
+const trashCollectionLanding = () =>
+ import(
+ /* webpackChunkName: "trash-collection" */ "metabase/collections/components/TrashCollectionLanding"
+ ).then(({ TrashCollectionLanding }) => ({
+ Component: TrashCollectionLanding,
+ }));
+
+const userCollectionList = () =>
+ import(
+ /* webpackChunkName: "user-collection-list" */ "metabase/common/components/UserCollectionList"
+ ).then(({ UserCollectionList }) => ({ Component: UserCollectionList }));
+
+/**
+ * The browse pages share one chunk: they are one module apiece behind a single
+ * barrel, and a visitor to one of them commonly walks into the next.
+ */
+const browsePage =
+ (
+ name:
+ | "BrowseMetrics"
+ | "BrowseModels"
+ | "BrowseDatabases"
+ | "BrowseSchemas"
+ | "BrowseTables"
+ | "TablePermalinkRedirect",
+ ) =>
+ () =>
+ import(/* webpackChunkName: "browse" */ "metabase/browse").then(
+ (module) => ({ Component: module[name] }),
+ );
+
const commentsSidesheet = () =>
import(
/* webpackChunkName: "comments-sidesheet" */ "metabase/documents/components/CommentsSidesheet"
@@ -177,6 +223,13 @@ registerPagePrefetch("/document/", commentsSidesheet);
registerPagePrefetch("/dashboard/", dashboardApp);
registerPagePrefetch("/auto/dashboard/", automaticDashboardApp);
registerPagePrefetch("/explore", metricsViewerPage);
+// The login page asks for this one by hand, so a user who signs in has the home
+// page in hand by the time they land on it. Exact, because every path starts
+// with "/".
+registerPagePrefetch("/", landingPage, { exact: true });
+registerPagePrefetch("/collection/", collectionLanding);
+registerPagePrefetch("/trash", trashCollectionLanding);
+registerPagePrefetch("/browse", browsePage("BrowseModels"));
export const getRoutes = (store: AppStore): RouteObject[] => [
{
@@ -185,7 +238,7 @@ export const getRoutes = (store: AppStore): RouteObject[] => [
// SETUP
{
element: <RedirectIfSetup />,
- children: [{ path: "/setup", element: <Setup /> }],
+ children: [{ path: "/setup", lazy: setupPage }],
},
// For compatibility: use the standard setup for embedding
@@ -228,18 +281,18 @@ export const getRoutes = (store: AppStore): RouteObject[] => [
: []),
// The global all hands routes, things in here are for all the folks
- { path: "/", element: <LandingPageRedirect /> },
+ { path: "/", lazy: landingPage },
{
path: "getting-started",
element: <CanAccessOnboarding />,
- children: [{ index: true, element: <Onboarding /> }],
+ children: [{ index: true, lazy: onboardingPage }],
},
- { path: "search", element: <SearchApp /> },
+ { path: "search", lazy: searchApp },
// Send historical /archive route to trash - can remove in v52
{ path: "archive", element: redirect("../trash") },
- { path: "trash", element: <TrashCollectionLanding /> },
+ { path: "trash", lazy: trashCollectionLanding },
{
path: "document/:entityId",
@@ -267,7 +320,7 @@ export const getRoutes = (store: AppStore): RouteObject[] => [
{
path: "collection/users",
element: <IsAdmin />,
- children: [{ index: true, element: <UserCollectionList /> }],
+ children: [{ index: true, lazy: userCollectionList }],
},
{
@@ -297,7 +350,7 @@ export const getRoutes = (store: AppStore): RouteObject[] => [
{
path: "collection/:slug",
- element: <CollectionLanding />,
+ lazy: collectionLanding,
children: [
...toRouteObjects(
<>
@@ -429,21 +482,24 @@ export const getRoutes = (store: AppStore): RouteObject[] => [
path: "browse",
children: [
{ index: true, element: redirect("/browse/models") },
- { path: "metrics", element: <BrowseMetrics /> },
- { path: "models", element: <BrowseModels /> },
- { path: "databases", element: <BrowseDatabases /> },
- { path: "databases/:slug", element: <BrowseSchemas /> },
+ { path: "metrics", lazy: browsePage("BrowseMetrics") },
+ { path: "models", lazy: browsePage("BrowseModels") },
+ { path: "databases", lazy: browsePage("BrowseDatabases") },
+ {
+ path: "databases/:slug",
+ lazy: browsePage("BrowseSchemas"),
+ },
{
path: "databases/:dbId/schema/:schemaName",
- element: <BrowseTables />,
+ lazy: browsePage("BrowseTables"),
},
{
path: "databases/:dbName/schema/:schemaName/table/:tableName",
- element: <TablePermalinkRedirect />,
+ lazy: browsePage("TablePermalinkRedirect"),
},
{
path: "databases/:dbName/table/:tableName",
- element: <TablePermalinkRedirect />,
+ lazy: browsePage("TablePermalinkRedirect"),
},
...toRouteObjects(PLUGIN_TABLE_EDITING.getRoutes()),