From a30aef712961b1e2c6cd7c69946fd669f658d4e2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 5 Aug 2026 19:08:06 -0400 Subject: [PATCH] perf: Avoid fetching relationships for newly created documents (#13308) * perf: Avoid fetching relationships for documents we literally just created * fix: Editor remount on title change --- app/models/Document.ts | 12 ++++- app/scenes/Document/components/DataLoader.tsx | 54 +++++++++++-------- app/scenes/Document/components/References.tsx | 9 +++- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/app/models/Document.ts b/app/models/Document.ts index 2e7276372a..9222bba05e 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -1,4 +1,4 @@ -import { addDays, differenceInDays } from "date-fns"; +import { addDays, differenceInDays, differenceInSeconds } from "date-fns"; import i18n, { t } from "i18next"; import { capitalize, floor } from "es-toolkit/compat"; import { action, autorun, comparer, computed, observable, set } from "mobx"; @@ -409,6 +409,16 @@ export default class Document extends ArchivableModel implements Searchable { return this.createdAt === this.updatedAt; } + /** + * Whether the document was created moments ago, and so cannot yet have views, comments, shares, + * or backlinks of its own. + * + * @returns true if the document was created within the last ten seconds. + */ + get isJustCreated(): boolean { + return differenceInSeconds(new Date(), new Date(this.createdAt)) < 10; + } + @computed get isFromTemplate(): boolean { return !!this.templateId; diff --git a/app/scenes/Document/components/DataLoader.tsx b/app/scenes/Document/components/DataLoader.tsx index dd651a6bde..9aead2a8f4 100644 --- a/app/scenes/Document/components/DataLoader.tsx +++ b/app/scenes/Document/components/DataLoader.tsx @@ -97,6 +97,11 @@ function DataLoader({ match, children }: Props) { const location = useLocation(); const query = useQuery(); const missingPolicy = !can || Object.keys(can).length === 0; + const isJustCreated = React.useMemo( + () => !!document?.isJustCreated, + // eslint-disable-next-line react-hooks/exhaustive-deps + [document?.id] + ); useDocumentSidebar(); @@ -148,7 +153,12 @@ function DataLoader({ match, children }: Props) { React.useEffect(() => { async function fetchViews() { - if (document?.id && !document?.isDeleted && !revisionId) { + if ( + document?.id && + !document?.isDeleted && + !revisionId && + !isJustCreated + ) { try { await views.fetchPage({ documentId: document.id, @@ -159,7 +169,7 @@ function DataLoader({ match, children }: Props) { } } void fetchViews(); - }, [document?.id, document?.isDeleted, revisionId, views]); + }, [document?.id, document?.isDeleted, revisionId, views, isJustCreated]); const onCreateLink = React.useCallback( async (params: Properties, nested?: boolean) => { @@ -205,7 +215,7 @@ function DataLoader({ match, children }: Props) { // Prevents unauthorized request to load share information for the document // when viewing a public share link if (can.read && !document.isDeleted && !revisionId) { - if (team.commentingEnabled) { + if (team.commentingEnabled && !isJustCreated) { void comments.fetchAll({ documentId: document.id, limit: 100, @@ -213,11 +223,15 @@ function DataLoader({ match, children }: Props) { }); } - shares.fetchOne({ documentId: document.id }).catch((err) => { - if (!(err instanceof NotFoundError)) { - throw err; - } - }); + // A newly created document has no share of its own, though it can still inherit one + // from a parent. + if (!isJustCreated || document.parentDocumentId) { + shares.fetchOne({ documentId: document.id }).catch((err) => { + if (!(err instanceof NotFoundError)) { + throw err; + } + }); + } } } }, [ @@ -230,6 +244,7 @@ function DataLoader({ match, children }: Props) { shares, revisionId, missingPolicy, + isJustCreated, ]); // Auto-enter presentation mode when ?present=true query param is set @@ -265,26 +280,21 @@ function DataLoader({ match, children }: Props) { ); } - // Redirect to the canonical URL if the document slug has changed, e.g. - // after a rename, so the browser address bar stays in sync. const canonicalUrl = updateDocumentPath(match.url, document); - if (location.pathname !== canonicalUrl) { - return ( - - ); - } - const canEdit = can.update && !document.isArchived && !revisionId; const readOnly = !isEditing || !canEdit; return ( <> + {location.pathname !== canonicalUrl && ( + + )} {!revision && } {children({ diff --git a/app/scenes/Document/components/References.tsx b/app/scenes/Document/components/References.tsx index c0e1535e1e..990a5030eb 100644 --- a/app/scenes/Document/components/References.tsx +++ b/app/scenes/Document/components/References.tsx @@ -26,12 +26,17 @@ function References({ document }: Props) { const locationSidebarContext = useLocationSidebarContext(); const { sharedTree, isShare } = useShare(); const [activeTab, setActiveTab] = useState("children"); + const isJustCreated = useMemo( + () => document.isJustCreated, + // eslint-disable-next-line react-hooks/exhaustive-deps + [document.id] + ); useEffect(() => { - if (!isShare) { + if (!isShare && !isJustCreated) { void documents.fetchRelationships(document.id); } - }, [isShare, documents, document.id]); + }, [isShare, documents, document.id, isJustCreated]); const children = useChildren(document, sharedTree); const backlinks = useBacklinks(document, sharedTree);