Skip to content

Commit 71cd016

Browse files
lebaudantoineAntoLC
authored andcommitted
⚡️(frontend) optimize document fetch error handling
Reduce unnecessary fetch requests when retrieving documents with permission or authentication issues. Previous implementation was triggering multiple document requests despite having sufficient error information from initial attempt to determine appropriate user redirection. Additionally, fix issue where resetting the auth cache was triggering redundant authentication verification requests. The responsibility for checking auth status should belong to the 401 page component on mount, rather than being triggered by cache resets during error handling. Known limitations: - Not waiting for async function completion makes code harder to maintain - Added loading spinner as temporary solution to prevent UI flicker - Future improvement should implement consistent error-based redirects rather than rendering error messages directly on document page
1 parent 2a7ffff commit 71cd016

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to
2121
- 🧑‍💻(docker) add .next to .dockerignore #1055
2222
- 🧑‍💻(docker) handle frontend development images with docker compose #1033
2323
- 🧑‍💻(docker) add y-provider config to development environment #1057
24+
- ⚡️(frontend) optimize document fetch error handling #1089
2425

2526
### Fixed
2627

src/frontend/apps/impress/src/pages/docs/[id]/index.tsx

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useEffect, useState } from 'react';
66
import { useTranslation } from 'react-i18next';
77

88
import { Box, Icon, TextErrors } from '@/components';
9+
import { DEFAULT_QUERY_RETRY } from '@/core';
910
import { DocEditor } from '@/docs/doc-editor';
1011
import {
1112
Doc,
@@ -14,7 +15,7 @@ import {
1415
useDoc,
1516
useDocStore,
1617
} from '@/docs/doc-management/';
17-
import { KEY_AUTH, setAuthUrl } from '@/features/auth';
18+
import { KEY_AUTH, setAuthUrl, useAuth } from '@/features/auth';
1819
import { MainLayout } from '@/layouts';
1920
import { useBroadcastStore } from '@/stores';
2021
import { NextPageWithLayout } from '@/types/next';
@@ -56,6 +57,14 @@ const DocPage = ({ id }: DocProps) => {
5657
{
5758
staleTime: 0,
5859
queryKey: [KEY_DOC, { id }],
60+
retryDelay: 1000,
61+
retry: (failureCount, error) => {
62+
if (error.status == 403 || error.status == 401 || error.status == 404) {
63+
return false;
64+
} else {
65+
return failureCount < DEFAULT_QUERY_RETRY;
66+
}
67+
},
5968
},
6069
);
6170

@@ -66,6 +75,7 @@ const DocPage = ({ id }: DocProps) => {
6675
const { replace } = useRouter();
6776
useCollaboration(doc?.id, doc?.content);
6877
const { t } = useTranslation();
78+
const { authenticated } = useAuth();
6979

7080
useEffect(() => {
7181
if (!docQuery || isFetching) {
@@ -93,23 +103,24 @@ const DocPage = ({ id }: DocProps) => {
93103
}, [addTask, doc?.id, queryClient]);
94104

95105
if (isError && error) {
96-
if (error.status === 403) {
97-
void replace(`/403`);
98-
return null;
99-
}
100-
101-
if (error.status === 404) {
102-
void replace(`/404`);
103-
return null;
104-
}
105-
106-
if (error.status === 401) {
107-
void queryClient.resetQueries({
108-
queryKey: [KEY_AUTH],
109-
});
110-
setAuthUrl();
111-
void replace(`/401`);
112-
return null;
106+
if ([403, 404, 401].includes(error.status)) {
107+
if (error.status === 401) {
108+
if (authenticated) {
109+
queryClient.setQueryData([KEY_AUTH], {
110+
user: null,
111+
authenticated: false,
112+
});
113+
}
114+
setAuthUrl();
115+
}
116+
117+
void replace(`/${error.status}`);
118+
119+
return (
120+
<Box $align="center" $justify="center" $height="100%">
121+
<Loader />
122+
</Box>
123+
);
113124
}
114125

115126
return (

0 commit comments

Comments
 (0)