perf: Keep the loaded collection when the document title changes (#14283)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-24 07:55:11 -04:00
1 parent d035e0fdff
commit 201038c36d
3 files changed
+45 -5

No files matched your search

+36 -1
View File
@@ -12,7 +12,11 @@ import { Document, Event } from "@server/models";
import { parser, schema } from "@server/editor";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import { buildDocument, buildUser } from "@server/test/factories";
import {
buildCollection,
buildDocument,
buildUser,
} from "@server/test/factories";
import { withAPIContext } from "@server/test/support";
import documentUpdater from "./documentUpdater";
@@ -192,6 +196,37 @@ describe("documentUpdater", () => {
notifyUpdateSpy.mockRestore();
});
it("should keep the loaded collection when the title changes", async () => {
const user = await buildUser();
const collection = await buildCollection({
userId: user.id,
teamId: user.teamId,
});
const { id } = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: collection.id,
});
const document = await Document.findByPk(id, {
userId: user.id,
rejectOnEmpty: true,
});
const loadedCollection = document.collection;
const result = await withAPIContext(user, (ctx) =>
documentUpdater(ctx, {
title: "New Title",
document,
})
);
expect(result).toBe(document);
expect(result.collection).toBe(loadedCollection);
expect(result.collection?.documentStructure?.[0].title).toEqual(
"New Title"
);
});
describe("replace", () => {
it("should update document content when changing text", async () => {
const user = await buildUser();
+2 -3
View File
@@ -178,9 +178,8 @@ export default async function documentUpdater(
}
// Publishing can move the document to another collection, and the save
// hooks that maintain the collection structure replace the loaded
// collection with one that has no membership data. Reload in those cases
// so that policies stay accurate.
// hooks can attach a collection that has no membership data. Reload in
// those cases so that policies stay accurate.
if (publish || document.collection !== collection) {
return await Document.findByPk(document.id, {
userId: user.id,
+7 -1
View File
@@ -542,7 +542,13 @@ class Document extends ArchivableModel<
}
await collection.updateDocument(model, { transaction });
model.collection = collection;
// Keep a loaded collection so that its membership data stays available.
if (model.collection?.id === collection.id) {
model.collection.documentStructure = collection.documentStructure;
} else {
model.collection = collection;
}
}
@AfterCreate