Skip to content

Commit 3c0b390

Browse files
committed
⚡️(frontend) fetch document without content when not needed
To improve performance, especially for documents with large content, an optional query parameter `without_content` has been added to the document fetching API endpoint. When this parameter is set to true, the API will return the document metadata without the content, allowing the frontend to load faster and reduce unnecessary data transfer.
1 parent c767263 commit 3c0b390

File tree

7 files changed

+26
-14
lines changed

7 files changed

+26
-14
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-inline-content/Interlinking/InterlinkingLinkInlineContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const LinkSelected = ({
9595
isEditable,
9696
updateInlineContent,
9797
}: LinkSelectedProps) => {
98-
const { data: doc } = useDoc({ id: docId });
98+
const { data: doc } = useDoc({ id: docId, withoutContent: true });
9999

100100
/**
101101
* Update the content title if the referenced doc title changes

src/frontend/apps/impress/src/features/docs/doc-management/api/useDoc.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { Doc } from '../types';
66

77
export type DocParams = {
88
id: string;
9+
withoutContent?: boolean;
910
};
1011

11-
export const getDoc = async ({ id }: DocParams): Promise<Doc> => {
12-
const response = await fetchAPI(`documents/${id}/`);
12+
export const getDoc = async ({
13+
id,
14+
withoutContent,
15+
}: DocParams): Promise<Doc> => {
16+
const params = withoutContent ? '?without_content=true' : '';
17+
const response = await fetchAPI(`documents/${id}/${params}`);
1318

1419
if (!response.ok) {
1520
throw new APIError('Failed to get the doc', await errorCauses(response));

src/frontend/apps/impress/src/features/docs/doc-management/types.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface Doc {
5353
title?: string;
5454
children?: Doc[];
5555
childrenCount?: number;
56-
content: Base64;
56+
content?: Base64;
5757
created_at: string;
5858
creator: string;
5959
deleted_at: string | null;

src/frontend/servers/y-provider/__tests__/collaborationBackend.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ describe('CollaborationBackend', () => {
1919
const { fetchDocument } = await import('@/api/collaborationBackend');
2020
const documentId = 'test-document-123';
2121

22-
await fetchDocument(documentId, { cookie: 'test-cookie' });
22+
await fetchDocument(
23+
{ name: documentId, withoutContent: true },
24+
{ cookie: 'test-cookie' },
25+
);
2326

2427
expect(axiosGetSpy).toHaveBeenCalledWith(
25-
`http://app-dev:8000/api/v1.0/documents/${documentId}/`,
28+
`http://app-dev:8000/api/v1.0/documents/${documentId}/?without_content=true`,
2629
expect.objectContaining({
2730
headers: expect.objectContaining({
2831
'X-Y-Provider-Key': 'test-yprovider-key',

src/frontend/servers/y-provider/__tests__/hocuspocusWS.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('Server Tests', () => {
228228
wsHocus.stopConnectionAttempt();
229229
expect(data.reason).toBe('permission-denied');
230230
expect(fetchDocumentMock).toHaveBeenCalledExactlyOnceWith(
231-
room,
231+
{ name: room, withoutContent: true },
232232
expect.any(Object),
233233
);
234234
wsHocus.webSocket?.close();
@@ -273,7 +273,7 @@ describe('Server Tests', () => {
273273
wsHocus.stopConnectionAttempt();
274274
expect(data.reason).toBe('permission-denied');
275275
expect(fetchDocumentMock).toHaveBeenCalledExactlyOnceWith(
276-
room,
276+
{ name: room, withoutContent: true },
277277
expect.any(Object),
278278
);
279279
wsHocus.webSocket?.close();
@@ -322,7 +322,7 @@ describe('Server Tests', () => {
322322
wsHocus.destroy();
323323

324324
expect(fetchDocumentMock).toHaveBeenCalledWith(
325-
room,
325+
{ name: room, withoutContent: true },
326326
expect.any(Object),
327327
);
328328

@@ -371,7 +371,7 @@ describe('Server Tests', () => {
371371
wsHocus.destroy();
372372

373373
expect(fetchDocumentMock).toHaveBeenCalledWith(
374-
room,
374+
{ name: room, withoutContent: true },
375375
expect.any(Object),
376376
);
377377

src/frontend/servers/y-provider/src/api/collaborationBackend.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Base64 = string;
1717
interface Doc {
1818
id: string;
1919
title?: string;
20-
content: Base64;
20+
content?: Base64;
2121
creator: string;
2222
is_favorite: boolean;
2323
link_reach: 'restricted' | 'public' | 'authenticated';
@@ -74,10 +74,11 @@ async function fetch<T>(
7474
}
7575

7676
export function fetchDocument(
77-
name: string,
77+
{ name, withoutContent }: { name: string; withoutContent?: boolean },
7878
requestHeaders: IncomingHttpHeaders,
7979
): Promise<Doc> {
80-
return fetch<Doc>(`/api/v1.0/documents/${name}/`, requestHeaders);
80+
const params = withoutContent ? '?without_content=true' : '';
81+
return fetch<Doc>(`/api/v1.0/documents/${name}/${params}`, requestHeaders);
8182
}
8283

8384
export function fetchCurrentUser(

src/frontend/servers/y-provider/src/servers/hocuspocusServer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export const hocuspocusServer = new Server({
3939
let canEdit = false;
4040

4141
try {
42-
const document = await fetchDocument(documentName, requestHeaders);
42+
const document = await fetchDocument(
43+
{ name: documentName, withoutContent: true },
44+
requestHeaders,
45+
);
4346

4447
if (!document.abilities.retrieve) {
4548
logger(

0 commit comments

Comments
 (0)