⚡️(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.
This commit is contained in:
committed by
Manuel Raynaud
parent
ffae927c93
commit
1ac6b42ae3
@@ -95,7 +95,7 @@ export const LinkSelected = ({
|
|||||||
isEditable,
|
isEditable,
|
||||||
updateInlineContent,
|
updateInlineContent,
|
||||||
}: LinkSelectedProps) => {
|
}: LinkSelectedProps) => {
|
||||||
const { data: doc } = useDoc({ id: docId });
|
const { data: doc } = useDoc({ id: docId, withoutContent: true });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the content title if the referenced doc title changes
|
* Update the content title if the referenced doc title changes
|
||||||
|
|||||||
@@ -6,10 +6,15 @@ import { Doc } from '../types';
|
|||||||
|
|
||||||
export type DocParams = {
|
export type DocParams = {
|
||||||
id: string;
|
id: string;
|
||||||
|
withoutContent?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDoc = async ({ id }: DocParams): Promise<Doc> => {
|
export const getDoc = async ({
|
||||||
const response = await fetchAPI(`documents/${id}/`);
|
id,
|
||||||
|
withoutContent,
|
||||||
|
}: DocParams): Promise<Doc> => {
|
||||||
|
const params = withoutContent ? '?without_content=true' : '';
|
||||||
|
const response = await fetchAPI(`documents/${id}/${params}`);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new APIError('Failed to get the doc', await errorCauses(response));
|
throw new APIError('Failed to get the doc', await errorCauses(response));
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export interface Doc {
|
|||||||
title?: string;
|
title?: string;
|
||||||
children?: Doc[];
|
children?: Doc[];
|
||||||
childrenCount?: number;
|
childrenCount?: number;
|
||||||
content: Base64;
|
content?: Base64;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
creator: string;
|
creator: string;
|
||||||
deleted_at: string | null;
|
deleted_at: string | null;
|
||||||
|
|||||||
@@ -19,10 +19,13 @@ describe('CollaborationBackend', () => {
|
|||||||
const { fetchDocument } = await import('@/api/collaborationBackend');
|
const { fetchDocument } = await import('@/api/collaborationBackend');
|
||||||
const documentId = 'test-document-123';
|
const documentId = 'test-document-123';
|
||||||
|
|
||||||
await fetchDocument(documentId, { cookie: 'test-cookie' });
|
await fetchDocument(
|
||||||
|
{ name: documentId, withoutContent: true },
|
||||||
|
{ cookie: 'test-cookie' },
|
||||||
|
);
|
||||||
|
|
||||||
expect(axiosGetSpy).toHaveBeenCalledWith(
|
expect(axiosGetSpy).toHaveBeenCalledWith(
|
||||||
`http://app-dev:8000/api/v1.0/documents/${documentId}/`,
|
`http://app-dev:8000/api/v1.0/documents/${documentId}/?without_content=true`,
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
headers: expect.objectContaining({
|
headers: expect.objectContaining({
|
||||||
'X-Y-Provider-Key': 'test-yprovider-key',
|
'X-Y-Provider-Key': 'test-yprovider-key',
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ describe('Server Tests', () => {
|
|||||||
wsHocus.stopConnectionAttempt();
|
wsHocus.stopConnectionAttempt();
|
||||||
expect(data.reason).toBe('permission-denied');
|
expect(data.reason).toBe('permission-denied');
|
||||||
expect(fetchDocumentMock).toHaveBeenCalledExactlyOnceWith(
|
expect(fetchDocumentMock).toHaveBeenCalledExactlyOnceWith(
|
||||||
room,
|
{ name: room, withoutContent: true },
|
||||||
expect.any(Object),
|
expect.any(Object),
|
||||||
);
|
);
|
||||||
wsHocus.webSocket?.close();
|
wsHocus.webSocket?.close();
|
||||||
@@ -273,7 +273,7 @@ describe('Server Tests', () => {
|
|||||||
wsHocus.stopConnectionAttempt();
|
wsHocus.stopConnectionAttempt();
|
||||||
expect(data.reason).toBe('permission-denied');
|
expect(data.reason).toBe('permission-denied');
|
||||||
expect(fetchDocumentMock).toHaveBeenCalledExactlyOnceWith(
|
expect(fetchDocumentMock).toHaveBeenCalledExactlyOnceWith(
|
||||||
room,
|
{ name: room, withoutContent: true },
|
||||||
expect.any(Object),
|
expect.any(Object),
|
||||||
);
|
);
|
||||||
wsHocus.webSocket?.close();
|
wsHocus.webSocket?.close();
|
||||||
@@ -322,7 +322,7 @@ describe('Server Tests', () => {
|
|||||||
wsHocus.destroy();
|
wsHocus.destroy();
|
||||||
|
|
||||||
expect(fetchDocumentMock).toHaveBeenCalledWith(
|
expect(fetchDocumentMock).toHaveBeenCalledWith(
|
||||||
room,
|
{ name: room, withoutContent: true },
|
||||||
expect.any(Object),
|
expect.any(Object),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -371,7 +371,7 @@ describe('Server Tests', () => {
|
|||||||
wsHocus.destroy();
|
wsHocus.destroy();
|
||||||
|
|
||||||
expect(fetchDocumentMock).toHaveBeenCalledWith(
|
expect(fetchDocumentMock).toHaveBeenCalledWith(
|
||||||
room,
|
{ name: room, withoutContent: true },
|
||||||
expect.any(Object),
|
expect.any(Object),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type Base64 = string;
|
|||||||
interface Doc {
|
interface Doc {
|
||||||
id: string;
|
id: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
content: Base64;
|
content?: Base64;
|
||||||
creator: string;
|
creator: string;
|
||||||
is_favorite: boolean;
|
is_favorite: boolean;
|
||||||
link_reach: 'restricted' | 'public' | 'authenticated';
|
link_reach: 'restricted' | 'public' | 'authenticated';
|
||||||
@@ -74,10 +74,11 @@ async function fetch<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function fetchDocument(
|
export function fetchDocument(
|
||||||
name: string,
|
{ name, withoutContent }: { name: string; withoutContent?: boolean },
|
||||||
requestHeaders: IncomingHttpHeaders,
|
requestHeaders: IncomingHttpHeaders,
|
||||||
): Promise<Doc> {
|
): Promise<Doc> {
|
||||||
return fetch<Doc>(`/api/v1.0/documents/${name}/`, requestHeaders);
|
const params = withoutContent ? '?without_content=true' : '';
|
||||||
|
return fetch<Doc>(`/api/v1.0/documents/${name}/${params}`, requestHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchCurrentUser(
|
export function fetchCurrentUser(
|
||||||
|
|||||||
@@ -39,7 +39,10 @@ export const hocuspocusServer = new Server({
|
|||||||
let canEdit;
|
let canEdit;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const document = await fetchDocument(documentName, requestHeaders);
|
const document = await fetchDocument(
|
||||||
|
{ name: documentName, withoutContent: true },
|
||||||
|
requestHeaders,
|
||||||
|
);
|
||||||
|
|
||||||
if (!document.abilities.retrieve) {
|
if (!document.abilities.retrieve) {
|
||||||
logger(
|
logger(
|
||||||
|
|||||||
Reference in New Issue
Block a user