From 0e704e6ed26c07260ffb20cf486cdd7dedc9fa8c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 26 Sep 2026 13:41:58 -0400 Subject: [PATCH] perf: Load templates on demand, add fetchAllIfNeeded to stores (#13895) * perf: Load templates on demand and cache fetchAll results fetchAll on the base store now returns the first result for repeat calls with the same params, with a force option for callers that need fresh data. Templates load when the command bar, document header, or block menu needs them instead of on every page load. * Make fetchAll caching opt-in via fetchAllIfNeeded fetchAll always fetches again. fetchAllIfNeeded returns the previous result for the same params until a model is added to or removed from the store. Mount-effect callers for reference lists use it. --- .../CommandBar/useTemplatesAction.tsx | 11 +++++- app/components/IconPicker/index.tsx | 2 +- app/components/Sidebar/App.tsx | 2 +- app/editor/components/BlockMenu.tsx | 14 +++++-- app/hooks/useSettingsConfig.ts | 2 +- app/hooks/useTemplateMenuActions.tsx | 8 +++- app/stores/base/Store.ts | 37 +++++++++++++++++-- plugins/github/client/Settings.tsx | 2 +- plugins/gitlab/client/Settings.tsx | 2 +- plugins/linear/client/Settings.tsx | 2 +- plugins/slack/client/Settings.tsx | 2 +- 11 files changed, 67 insertions(+), 17 deletions(-) diff --git a/app/components/CommandBar/useTemplatesAction.tsx b/app/components/CommandBar/useTemplatesAction.tsx index c4ae42406c..8ce09d2d81 100644 --- a/app/components/CommandBar/useTemplatesAction.tsx +++ b/app/components/CommandBar/useTemplatesAction.tsx @@ -1,3 +1,4 @@ +import { useKBar } from "kbar"; import { NewDocumentIcon, ShapesIcon } from "outline-icons"; import { useEffect, useMemo } from "react"; import Icon from "@shared/components/Icon"; @@ -12,10 +13,16 @@ import { newDocumentPath } from "~/utils/routeHelpers"; const useTemplatesAction = () => { const { templates } = useStores(); + const { showing } = useKBar((state) => ({ + showing: state.visualState !== "hidden", + })); + // Templates are only needed once the command bar is opened. useEffect(() => { - void templates.fetchAll(); - }, [templates]); + if (showing) { + void templates.fetchAllIfNeeded(); + } + }, [showing, templates]); const actions = useMemo( () => diff --git a/app/components/IconPicker/index.tsx b/app/components/IconPicker/index.tsx index 036424a56d..8799a558fd 100644 --- a/app/components/IconPicker/index.tsx +++ b/app/components/IconPicker/index.tsx @@ -173,7 +173,7 @@ const IconPicker = ({ React.useEffect(() => { if (open) { - void emojis.fetchAll(); + void emojis.fetchAllIfNeeded(); } }, [open, emojis]); diff --git a/app/components/Sidebar/App.tsx b/app/components/Sidebar/App.tsx index 1d6d63aa40..f662b5e676 100644 --- a/app/components/Sidebar/App.tsx +++ b/app/components/Sidebar/App.tsx @@ -59,7 +59,7 @@ function AppSidebar() { }, [history]); useEffect(() => { - void collections.fetchAll(); + void collections.fetchAllIfNeeded(); if (!user.isViewer) { void documents.fetchDrafts(); diff --git a/app/editor/components/BlockMenu.tsx b/app/editor/components/BlockMenu.tsx index 74b18d4b02..5737fdf700 100644 --- a/app/editor/components/BlockMenu.tsx +++ b/app/editor/components/BlockMenu.tsx @@ -1,7 +1,7 @@ import { DocumentIcon, ShapesIcon } from "outline-icons"; import { cloneDeep } from "es-toolkit/compat"; import { observer } from "mobx-react"; -import { useCallback, useMemo } from "react"; +import { useCallback, useEffect, useMemo } from "react"; import { useTranslation } from "react-i18next"; import Icon from "@shared/components/Icon"; import type { MenuItem } from "@shared/editor/types"; @@ -18,8 +18,10 @@ import SuggestionsMenuItem from "./SuggestionsMenuItem"; /** * Hook that returns a template menu item with children for inserting template * content into the editor, or undefined if no templates are available. + * + * @param isActive whether the block menu is open, templates load when it is. */ -function useTemplateMenuItem(): MenuItem | undefined { +function useTemplateMenuItem(isActive: boolean): MenuItem | undefined { const { t } = useTranslation(); const user = useCurrentUser({ rejectOnEmpty: false }); const { documents, templates: templatesStore } = useStores(); @@ -28,6 +30,12 @@ function useTemplateMenuItem(): MenuItem | undefined { const document = documentId ? documents.get(documentId) : undefined; const collectionId = document?.collectionId; + useEffect(() => { + if (isActive && user) { + void templatesStore.fetchAllIfNeeded(); + } + }, [isActive, user, templatesStore]); + return useMemo(() => { if (!user) { return undefined; @@ -109,7 +117,7 @@ type Props = Omit & function BlockMenu(props: Props) { const { t } = useTranslation(); const { elementRef } = useEditor(); - const templateMenuItem = useTemplateMenuItem(); + const templateMenuItem = useTemplateMenuItem(props.isActive); const items = useMemo(() => { const baseItems = getMenuItems(t, elementRef); diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index 898e23db20..39b9462438 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -74,7 +74,7 @@ const useSettingsConfig = () => { const { t } = useTranslation(); useEffect(() => { - void integrations.fetchAll(); + void integrations.fetchAllIfNeeded(); }, [integrations]); const config = useComputed(() => { diff --git a/app/hooks/useTemplateMenuActions.tsx b/app/hooks/useTemplateMenuActions.tsx index 097bfd126c..f1d4a30765 100644 --- a/app/hooks/useTemplateMenuActions.tsx +++ b/app/hooks/useTemplateMenuActions.tsx @@ -1,5 +1,5 @@ import { DocumentIcon } from "outline-icons"; -import { useCallback } from "react"; +import { useCallback, useEffect } from "react"; import { useTranslation } from "react-i18next"; import Icon from "@shared/components/Icon"; import { TextHelper } from "@shared/utils/TextHelper"; @@ -38,6 +38,12 @@ export function useTemplateMenuActions({ const { t } = useTranslation(); const document = documents.get(documentId); + useEffect(() => { + if (onSelectTemplate) { + void templatesStore.fetchAllIfNeeded(); + } + }, [onSelectTemplate, templatesStore]); + const templateToAction = useCallback( (template: Template): Action => createAction({ diff --git a/app/stores/base/Store.ts b/app/stores/base/Store.ts index 73c6f47990..d0177368f5 100644 --- a/app/stores/base/Store.ts +++ b/app/stores/base/Store.ts @@ -133,6 +133,7 @@ export default abstract class Store { @action clear() { this.data.clear(); + this.fetchAllResults.clear(); void this.persistence?.clear(); } @@ -252,9 +253,13 @@ export default abstract class Store { const newModel: T = new ModelClass(item, this); this.data.set(newModel.id, newModel); this.persistence?.persist(newModel.id); + this.fetchAllResults.clear(); return newModel; } + if (!this.data.has(item.id)) { + this.fetchAllResults.clear(); + } this.data.set(item.id, item); this.persistence?.persist(item.id); return item; @@ -315,6 +320,7 @@ export default abstract class Store { } LifecycleManager.executeHooks(model.constructor, "afterRemove", model); + this.fetchAllResults.clear(); // Persists the soft-deleted model, or removes the persisted record if the // model was hard-deleted from the store. @@ -527,12 +533,17 @@ export default abstract class Store { return this.fetchPaginated(`/${this.apiEndpoint}.list`, params); }; + /** + * Fetches every page of the list endpoint. + * + * @param params the request parameters. + * @returns all fetched items. + */ @action fetchAll = async ( - // oxlint-disable-next-line no-explicit-any - params?: Record + params: FetchPageParams = {} ): Promise> => { - const limit = params?.limit ?? Pagination.defaultLimit; + const limit = params.limit ?? Pagination.defaultLimit; const response = await this.fetchPage({ ...params, limit }); invariant( @@ -553,15 +564,30 @@ export default abstract class Store { ...(fetchPages.length ? await Promise.all(fetchPages) : []), ]); - if (params?.withRelations) { + if (params.withRelations) { await Promise.all( this.orderedData.map((integration) => integration.loadRelations()) ); } + this.fetchAllResults.set(JSON.stringify(params), results); return results; }; + /** + * Fetches every page of the list endpoint unless the same params were + * fetched before and no model has been added to or removed from the store + * since. Use for reference lists that are loaded on mount. + * + * @param params the request parameters. + * @returns all fetched items. + */ + fetchAllIfNeeded = async ( + params: FetchPageParams = {} + ): Promise> => + this.fetchAllResults.get(JSON.stringify(params)) ?? + (await this.fetchAll(params)); + @computed get orderedData(): T[] { return orderBy(Array.from(this.data.values()), "createdAt", "desc"); @@ -635,4 +661,7 @@ export default abstract class Store { }); } } + + /** Results of `fetchAll` calls keyed by their params, see `fetchAllIfNeeded`. */ + private fetchAllResults = new Map>(); } diff --git a/plugins/github/client/Settings.tsx b/plugins/github/client/Settings.tsx index 9b00440b56..0020bbac74 100644 --- a/plugins/github/client/Settings.tsx +++ b/plugins/github/client/Settings.tsx @@ -32,7 +32,7 @@ function GitHub() { const githubAppName = env.GITHUB_APP_NAME; React.useEffect(() => { - void integrations.fetchAll({ + void integrations.fetchAllIfNeeded({ service: IntegrationService.GitHub, withRelations: true, }); diff --git a/plugins/gitlab/client/Settings.tsx b/plugins/gitlab/client/Settings.tsx index be3df4e547..bdc23bd2c8 100644 --- a/plugins/gitlab/client/Settings.tsx +++ b/plugins/gitlab/client/Settings.tsx @@ -31,7 +31,7 @@ function GitLab() { const appName = env.APP_NAME; React.useEffect(() => { - void integrations.fetchAll({ + void integrations.fetchAllIfNeeded({ service: IntegrationService.GitLab, withRelations: true, }); diff --git a/plugins/linear/client/Settings.tsx b/plugins/linear/client/Settings.tsx index d77bd1a3a4..d688f7e933 100644 --- a/plugins/linear/client/Settings.tsx +++ b/plugins/linear/client/Settings.tsx @@ -29,7 +29,7 @@ function Linear() { const appName = env.APP_NAME; React.useEffect(() => { - void integrations.fetchAll({ + void integrations.fetchAllIfNeeded({ service: IntegrationService.Linear, withRelations: true, }); diff --git a/plugins/slack/client/Settings.tsx b/plugins/slack/client/Settings.tsx index c15c6fab2d..5ced52634b 100644 --- a/plugins/slack/client/Settings.tsx +++ b/plugins/slack/client/Settings.tsx @@ -34,7 +34,7 @@ function Slack() { const error = query.get("error"); React.useEffect(() => { - void collections.fetchAll(); + void collections.fetchAllIfNeeded(); }, [collections, integrations]); const commandIntegration = integrations.find({