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
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-05 19:08:06 -04:00
1 parent 5a6182c0dc
commit a30aef7129
3 files changed
+50 -25

No files matched your search

+11 -1
View File
@@ -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;
+32 -22
View File
@@ -97,6 +97,11 @@ function DataLoader({ match, children }: Props) {
const location = useLocation<LocationState>();
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<Document>, 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 (
<Redirect
to={{
pathname: canonicalUrl,
state: location.state,
hash: location.hash,
}}
/>
);
}
const canEdit = can.update && !document.isArchived && !revisionId;
const readOnly = !isEditing || !canEdit;
return (
<>
{location.pathname !== canonicalUrl && (
<Redirect
to={{
pathname: canonicalUrl,
state: location.state,
hash: location.hash,
}}
/>
)}
{!revision && <MarkAsViewed document={document} />}
<React.Fragment key={canEdit ? "edit" : "read"}>
{children({
@@ -26,12 +26,17 @@ function References({ document }: Props) {
const locationSidebarContext = useLocationSidebarContext();
const { sharedTree, isShare } = useShare();
const [activeTab, setActiveTab] = useState<TabType>("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);