Skip to content

Commit 3bf33d2

Browse files
committed
⚡️(frontend) reduce unblocking time for config
We will serve the config from the cache if available in waiting for the config to be loaded. It will remove the loading time for the config except when the config is not available in the cache.
1 parent 101cef7 commit 3bf33d2

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to
1313
- 🚸(backend) make document search on title accent-insensitive #874
1414
- 🚩 add homepage feature flag #861
1515

16+
## Changed
17+
18+
⚡️(frontend) reduce unblocking time for config #867
1619

1720
## [3.1.0] - 2025-04-07
1821

src/frontend/apps/impress/src/core/config/api/useConfig.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,47 @@ interface ConfigResponse {
1919
SENTRY_DSN?: string;
2020
}
2121

22+
const LOCAL_STORAGE_KEY = 'docs_config';
23+
24+
function getCachedTranslation() {
25+
try {
26+
const jsonString = localStorage.getItem(LOCAL_STORAGE_KEY);
27+
return jsonString ? (JSON.parse(jsonString) as ConfigResponse) : undefined;
28+
} catch {
29+
return undefined;
30+
}
31+
}
32+
33+
function setCachedTranslation(translations: ConfigResponse) {
34+
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(translations));
35+
}
36+
2237
export const getConfig = async (): Promise<ConfigResponse> => {
2338
const response = await fetchAPI(`config/`);
2439

2540
if (!response.ok) {
2641
throw new APIError('Failed to get the doc', await errorCauses(response));
2742
}
2843

29-
return response.json() as Promise<ConfigResponse>;
44+
const config = response.json() as Promise<ConfigResponse>;
45+
setCachedTranslation(await config);
46+
47+
return config;
3048
};
3149

3250
export const KEY_CONFIG = 'config';
3351

3452
export function useConfig() {
35-
return useQuery<ConfigResponse, APIError, ConfigResponse>({
53+
const cachedData = getCachedTranslation();
54+
const oneHour = 1000 * 60 * 60;
55+
56+
const response = useQuery<ConfigResponse, APIError, ConfigResponse>({
3657
queryKey: [KEY_CONFIG],
3758
queryFn: () => getConfig(),
38-
staleTime: Infinity,
59+
initialData: cachedData,
60+
staleTime: oneHour,
61+
initialDataUpdatedAt: Date.now() - oneHour, // Force initial data to be considered stale
3962
});
63+
64+
return response;
4065
}

src/frontend/apps/impress/src/features/language/hooks/useLanguageSynchronizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const useLanguageSynchronizer = () => {
1616

1717
const availableBackendLanguages = useMemo(() => {
1818
return conf?.LANGUAGES.map(([locale]) => locale);
19-
}, [conf]);
19+
}, [conf?.LANGUAGES]);
2020

2121
const synchronizeLanguage = useCallback(
2222
async (direction?: 'toBackend' | 'toFrontend') => {

0 commit comments

Comments
 (0)