Single source of truth for lazy route components

Introduce a route map pairing each lazy-loaded scene with its path
patterns and a prefetch function. The route definitions render from the
map and hover prefetching matches against it, so adding a new
code-split route is a single map entry rather than parallel updates in
two files.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45zR5AhPpLURRoDQUsAgX
This commit is contained in:
Claude committed 2026-07-15 03:17:07 +00:00
1 parent b700579c26
commit b8a1363ff3
4 files changed
+129 -82

No files matched your search

+45 -36
View File
@@ -12,30 +12,18 @@ import WebsocketProvider from "~/components/WebsocketProvider";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import usePolicy from "~/hooks/usePolicy";
import useQueryNotices from "~/hooks/useQueryNotices";
import lazy from "~/utils/lazyWithRetry";
import {
archivePath,
draftsPath,
homePath,
searchPath,
settingsPath,
matchDocumentSlug as documentSlug,
matchCollectionSlug as collectionSlug,
trashPath,
debugPath,
matchDocumentEdit,
matchDocumentHistory,
} from "~/utils/routeHelpers";
import env from "~/env";
import { routeMap } from "./map";
const SettingsRoutes = lazy(() => import("./settings"));
const Archive = lazy(() => import("~/scenes/Archive"));
const Collection = lazy(() => import("~/scenes/Collection"));
const Document = lazy(() => import("~/scenes/Document"));
const Drafts = lazy(() => import("~/scenes/Drafts"));
const Home = lazy(() => import("~/scenes/Home"));
const Search = lazy(() => import("~/scenes/Search"));
const Trash = lazy(() => import("~/scenes/Trash"));
const Debug = lazy(() => import("~/scenes/Developer/Debug"));
const Changesets = lazy(() => import("~/scenes/Developer/Changesets"));
const SettingsRoutes = routeMap.settings.Component;
const RedirectDocument = ({
match,
@@ -70,15 +58,30 @@ function AuthenticatedRoutes() {
>
<Switch>
{can.createDocument && (
<Route exact path={draftsPath()} component={Drafts} />
<Route
exact
path={routeMap.drafts.paths}
component={routeMap.drafts.Component}
/>
)}
{can.createDocument && (
<Route exact path={archivePath()} component={Archive} />
<Route
exact
path={routeMap.archive.paths}
component={routeMap.archive.Component}
/>
)}
{can.createDocument && (
<Route exact path={trashPath()} component={Trash} />
<Route
exact
path={routeMap.trash.paths}
component={routeMap.trash.Component}
/>
)}
<Route path={`${homePath()}/:tab?`} component={Home} />
<Route
path={routeMap.home.paths}
component={routeMap.home.Component}
/>
<Redirect from="/dashboard" to={homePath()} />
<Redirect exact from="/starred" to={homePath()} />
<Redirect exact from="/templates" to={settingsPath("templates")} />
@@ -90,13 +93,8 @@ function AuthenticatedRoutes() {
/>
<Route
exact
path={`/collection/${collectionSlug}/overview/edit`}
component={Collection}
/>
<Route
exact
path={`/collection/${collectionSlug}/:tab?`}
component={Collection}
path={routeMap.collection.paths}
component={routeMap.collection.Component}
/>
<Route exact path="/doc/new" component={DocumentNew} />
<Route
@@ -106,25 +104,36 @@ function AuthenticatedRoutes() {
/>
<Route
exact
path={`/doc/${documentSlug}/history/:revisionId?`}
component={Document}
path={matchDocumentHistory}
component={routeMap.document.Component}
/>
<Route
exact
path={`/doc/${documentSlug}/edit`}
component={Document}
path={matchDocumentEdit}
component={routeMap.document.Component}
/>
<Route
path={routeMap.document.paths}
component={routeMap.document.Component}
/>
<Route
exact
path={routeMap.search.paths}
component={routeMap.search.Component}
/>
<Route path={`/doc/${documentSlug}`} component={Document} />
<Route exact path={`${searchPath()}/:query?`} component={Search} />
{env.isDevelopment && (
<Route exact path={debugPath()} component={Debug} />
<Route
exact
path={routeMap.debug.paths}
component={routeMap.debug.Component}
/>
)}
{env.isDevelopment && (
<Route
exact
path={`${debugPath()}/changesets`}
component={Changesets}
path={routeMap.changesets.paths}
component={routeMap.changesets.Component}
/>
)}
<Route exact path="/404" component={Error404} />
+2 -1
View File
@@ -8,10 +8,11 @@ import env from "~/env";
import lazy from "~/utils/lazyWithRetry";
import { matchDocumentSlug as documentSlug } from "~/utils/routeHelpers";
import useAutoRefresh from "~/hooks/useAutoRefresh";
import { routeMap } from "./map";
const Authenticated = lazy(() => import("~/components/Authenticated"));
const AuthenticatedRoutes = lazy(() => import("./authenticated"));
const Shared = lazy(() => import("~/scenes/Shared"));
const Shared = routeMap.shared.Component;
const Login = lazy(() => import("~/scenes/Login"));
const Logout = lazy(() => import("~/scenes/Logout"));
const OAuthAuthorize = lazy(() => import("~/scenes/Login/OAuthAuthorize"));
+73
View File
@@ -0,0 +1,73 @@
// oxlint-disable no-explicit-any -- ComponentType<any> is the standard React pattern for generic component constraints
import type * as React from "react";
import { createLazyComponent } from "~/components/LazyLoad";
import {
archivePath,
debugPath,
draftsPath,
homePath,
matchCollectionEdit,
matchCollectionSlug,
matchDocumentSlug,
searchPath,
settingsPath,
trashPath,
} from "~/utils/routeHelpers";
/**
* A lazy-loaded route in the application, pairing the path patterns it
* renders at with the code-split component and a way to prefetch it.
*/
export interface RouteMapEntry {
/** Patterns, in react-router syntax, matching the locations the scene renders at. */
paths: string[];
/** The lazy-loaded component rendered for the route. */
Component: React.LazyExoticComponent<React.ComponentType<any>>;
/** Imports the code-split chunk(s) for the route ahead of render. */
prefetch: () => Promise<unknown>;
}
/**
* The single source of truth for lazy-loaded routes that are reachable
* through in-app navigation. Route definitions (see `./index` and
* `./authenticated`) render `Component` at `paths`, and hovering a link is
* used as a signal to call `prefetch` (see `./prefetch`) so the code is
* already available when the user navigates.
*
* Note: Prefetch matching checks entries in order and patterns are matched
* non-exactly, so list more specific routes before routes they share a
* prefix with.
*/
export const routeMap = {
drafts: defineRoute([draftsPath()], () => import("~/scenes/Drafts")),
archive: defineRoute([archivePath()], () => import("~/scenes/Archive")),
trash: defineRoute([trashPath()], () => import("~/scenes/Trash")),
home: defineRoute([`${homePath()}/:tab?`], () => import("~/scenes/Home")),
search: defineRoute(
[`${searchPath()}/:query?`],
() => import("~/scenes/Search")
),
collection: defineRoute(
[matchCollectionEdit, `/collection/${matchCollectionSlug}/:tab?`],
() => import("~/scenes/Collection")
),
document: defineRoute(
[`/doc/${matchDocumentSlug}`],
() => import("~/scenes/Document")
),
settings: defineRoute([settingsPath()], () => import("./settings")),
shared: defineRoute(["/s/:shareId"], () => import("~/scenes/Shared")),
changesets: defineRoute(
[`${debugPath()}/changesets`],
() => import("~/scenes/Developer/Changesets")
),
debug: defineRoute([debugPath()], () => import("~/scenes/Developer/Debug")),
};
function defineRoute(
paths: string[],
load: () => Promise<{ default: React.ComponentType<any> }>
): RouteMapEntry {
const { Component, preload } = createLazyComponent(load);
return { paths, Component, prefetch: preload };
}
+9 -45
View File
@@ -1,16 +1,7 @@
import type { LocationDescriptor } from "history";
import { matchPath } from "react-router-dom";
import env from "~/env";
import {
archivePath,
draftsPath,
homePath,
matchCollectionSlug,
matchDocumentSlug,
searchPath,
settingsPath,
trashPath,
} from "~/utils/routeHelpers";
import { routeMap } from "./map";
/**
* Prefetches the lazy-loaded components associated with the route that the
@@ -29,42 +20,15 @@ export function prefetchRouteComponents(to: LocationDescriptor): void {
}
const pathname = path.split(/[?#]/)[0];
const route = routes.find(({ paths }) =>
paths.some((pattern) => matchPath(pathname, { path: pattern }))
);
void route?.load().catch(() => {
// When hosted as a public share the entire app renders the shared scene.
const route = env.ROOT_SHARE_ID
? routeMap.shared
: Object.values(routeMap).find(({ paths }) =>
paths.some((pattern) => matchPath(pathname, { path: pattern }))
);
void route?.prefetch().catch(() => {
// Prefetching is best effort.
});
}
interface PrefetchableRoute {
/** Route patterns, in react-router syntax, that render the component. */
paths: string[];
/** Imports the code-split chunk(s) associated with the route. */
load: () => Promise<unknown>;
}
/**
* Mirrors the lazy-loaded routes in `./index` and `./authenticated` — the
* import specifiers must match those used there so the same chunk is loaded.
*/
const routes: PrefetchableRoute[] = env.ROOT_SHARE_ID
? [{ paths: ["/"], load: () => import("~/scenes/Shared") }]
: [
{ paths: [draftsPath()], load: () => import("~/scenes/Drafts") },
{ paths: [archivePath()], load: () => import("~/scenes/Archive") },
{ paths: [trashPath()], load: () => import("~/scenes/Trash") },
{ paths: [homePath()], load: () => import("~/scenes/Home") },
{ paths: [searchPath()], load: () => import("~/scenes/Search") },
{
paths: [`/doc/${matchDocumentSlug}`, `/d/${matchDocumentSlug}`],
load: () => import("~/scenes/Document"),
},
{
paths: [`/collection/${matchCollectionSlug}`],
load: () => import("~/scenes/Collection"),
},
{ paths: [settingsPath()], load: () => import("./settings") },
{ paths: ["/s/:shareId"], load: () => import("~/scenes/Shared") },
];