Skip to content

Commit 9344e00

Browse files
committed
🚧(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 da45292 commit 9344e00

File tree

1 file changed

+18
-17
lines changed
  • src/frontend/apps/impress/src/pages/docs/[id]

1 file changed

+18
-17
lines changed

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

Lines changed: 18 additions & 17 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,6 @@ import {
1415
useDoc,
1516
useDocStore,
1617
} from '@/docs/doc-management/';
17-
import { KEY_AUTH } from '@/features/auth';
1818
import { MainLayout } from '@/layouts';
1919
import { useBroadcastStore } from '@/stores';
2020
import { NextPageWithLayout } from '@/types/next';
@@ -56,6 +56,14 @@ const DocPage = ({ id }: DocProps) => {
5656
{
5757
staleTime: 0,
5858
queryKey: [KEY_DOC, { id }],
59+
retryDelay: 1000,
60+
retry: (failureCount, error) => {
61+
if (error.status == 403 || error.status == 401 || error.status == 404) {
62+
return false;
63+
} else {
64+
return failureCount < DEFAULT_QUERY_RETRY;
65+
}
66+
},
5967
},
6068
);
6169

@@ -101,24 +109,17 @@ const DocPage = ({ id }: DocProps) => {
101109
}, [addTask, doc?.id, queryClient]);
102110

103111
if (isError && error) {
104-
if (error.status === 403) {
105-
void replace(`/403`);
106-
return null;
107-
}
108-
109-
if (error.status === 404) {
110-
void replace(`/404`);
111-
return null;
112-
}
113-
114-
if (error.status === 401) {
115-
void queryClient.resetQueries({
116-
queryKey: [KEY_AUTH],
117-
});
112+
if ([403, 404, 401].includes(error.status)) {
118113
void replace(
119-
`/401?returnTo=${encodeURIComponent(window.location.pathname)}`,
114+
error.status === 401
115+
? `/${error.status}?returnTo=${encodeURIComponent(window.location.pathname)}`
116+
: `/${error.status}`,
117+
);
118+
return (
119+
<Box $align="center" $justify="center" $height="100%">
120+
<Loader />
121+
</Box>
120122
);
121-
return null;
122123
}
123124

124125
return (

0 commit comments

Comments
 (0)