Add requestIdleCallback to usePreloadTranslations

This commit is contained in:
Kalle
2026-08-29 16:06:37 +03:00
parent 33524c50ab
commit f1e750881c

View File

@@ -79,6 +79,8 @@ import { isRevalidation, metaTags, type SerializeFrom } from "./utils/remix";
import { requestContextMiddleware } from "./utils/request-context-middleware.server";
import { APP_ICON_URL, pwaSplashScreenImageUrl } from "./utils/urls";
const PRELOAD_TRANSLATION_TIMEOUT_MS = 3000;
export const middleware: Route.MiddlewareFunction[] = [
redirectsMiddleware,
requestContextMiddleware,
@@ -339,7 +341,21 @@ function useLoadingIndicator() {
function usePreloadTranslation() {
React.useEffect(() => {
void generalI18next.loadNamespaces(allI18nNamespaces());
const loadAll = () =>
void generalI18next.loadNamespaces(allI18nNamespaces());
if (typeof window.requestIdleCallback !== "function") {
const timeoutId = window.setTimeout(
loadAll,
PRELOAD_TRANSLATION_TIMEOUT_MS,
);
return () => window.clearTimeout(timeoutId);
}
const idleId = window.requestIdleCallback(loadAll, {
timeout: PRELOAD_TRANSLATION_TIMEOUT_MS,
});
return () => window.cancelIdleCallback(idleId);
}, []);
}