diff --git a/app/routes/authenticated.tsx b/app/routes/authenticated.tsx index 88214194c5..75a645caab 100644 --- a/app/routes/authenticated.tsx +++ b/app/routes/authenticated.tsx @@ -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() { > {can.createDocument && ( - + )} {can.createDocument && ( - + )} {can.createDocument && ( - + )} - + @@ -90,13 +93,8 @@ function AuthenticatedRoutes() { /> - + + - - {env.isDevelopment && ( - + )} {env.isDevelopment && ( )} diff --git a/app/routes/index.tsx b/app/routes/index.tsx index c5bb922de7..c219237a2d 100644 --- a/app/routes/index.tsx +++ b/app/routes/index.tsx @@ -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")); diff --git a/app/routes/map.ts b/app/routes/map.ts new file mode 100644 index 0000000000..33144d4ee7 --- /dev/null +++ b/app/routes/map.ts @@ -0,0 +1,73 @@ +// oxlint-disable no-explicit-any -- ComponentType 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>; + /** Imports the code-split chunk(s) for the route ahead of render. */ + prefetch: () => Promise; +} + +/** + * 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 }> +): RouteMapEntry { + const { Component, preload } = createLazyComponent(load); + return { paths, Component, prefetch: preload }; +} diff --git a/app/routes/prefetch.ts b/app/routes/prefetch.ts index e93026b4f4..cf3a58058a 100644 --- a/app/routes/prefetch.ts +++ b/app/routes/prefetch.ts @@ -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; -} - -/** - * 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") }, - ];