Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix: Implement caching for clientLoader to prevent repeated calls (#4772)

Robert Brennan7a8e207985b8
diff --git a/frontend/src/routes/_oh._index/route.tsx b/frontend/src/routes/_oh._index/route.tsx
index 7fcc6152bc..41d6c181c0 100644
--- a/frontend/src/routes/_oh._index/route.tsx
+++ b/frontend/src/routes/_oh._index/route.tsx
@@ -25,6 +25,9 @@ import { generateGitHubAuthUrl } from "#/utils/generate-github-auth-url";
import { GitHubRepositoriesSuggestionBox } from "#/components/github-repositories-suggestion-box";
import { convertZipToBase64 } from "#/utils/convert-zip-to-base64";
+let cachedRepositories: ReturnType<
+ typeof retrieveAllGitHubUserRepositories
+> | null = null;
export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
let isSaas = false;
let githubClientId: string | null = null;
@@ -45,9 +48,12 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
let repositories: ReturnType<
typeof retrieveAllGitHubUserRepositories
> | null = null;
- if (ghToken) {
+ if (cachedRepositories) {
+ repositories = cachedRepositories;
+ } else if (ghToken) {
const data = retrieveAllGitHubUserRepositories(ghToken);
repositories = data;
+ cachedRepositories = data;
}
let githubAuthUrl: string | null = null;
diff --git a/frontend/src/routes/_oh.app.tsx b/frontend/src/routes/_oh.app.tsx
index 731bf4cdf9..b128b54e2b 100644
--- a/frontend/src/routes/_oh.app.tsx
+++ b/frontend/src/routes/_oh.app.tsx
@@ -69,6 +69,8 @@ const isAgentStateChange = (
data.extras instanceof Object &&
"agent_state" in data.extras;
+let lastCommitCached: GitHubCommit | null = null;
+let repoForLastCommit: string | null = null;
export const clientLoader = async () => {
const ghToken = localStorage.getItem("ghToken");
@@ -82,14 +84,16 @@ export const clientLoader = async () => {
if (repo) localStorage.setItem("repo", repo);
- let lastCommit: GitHubCommit | null = null;
- if (ghToken && repo) {
- const data = await retrieveLatestGitHubCommit(ghToken, repo);
- if (isGitHubErrorReponse(data)) {
- // TODO: Handle error
- console.error("Failed to retrieve latest commit", data);
- } else {
- [lastCommit] = data;
+ if (!lastCommitCached || repoForLastCommit !== repo) {
+ if (ghToken && repo) {
+ const data = await retrieveLatestGitHubCommit(ghToken, repo);
+ if (isGitHubErrorReponse(data)) {
+ // TODO: Handle error
+ console.error("Failed to retrieve latest commit", data);
+ } else {
+ [lastCommitCached] = data;
+ repoForLastCommit = repo;
+ }
}
}
@@ -99,7 +103,7 @@ export const clientLoader = async () => {
ghToken,
repo,
q,
- lastCommit,
+ lastCommit: lastCommitCached,
});
};
@@ -116,7 +120,6 @@ export const clientAction = async ({ request }: ClientActionFunctionArgs) => {
};
function App() {
- console.log("render app");
const dispatch = useDispatch();
const { files, importedProjectZip } = useSelector(
(state: RootState) => state.initalQuery,
diff --git a/frontend/src/routes/_oh.tsx b/frontend/src/routes/_oh.tsx
index b6594f7e73..022ad20d1e 100644
--- a/frontend/src/routes/_oh.tsx
+++ b/frontend/src/routes/_oh.tsx
@@ -34,6 +34,9 @@ import { AnalyticsConsentFormModal } from "#/components/analytics-consent-form-m
import { setCurrentAgentState } from "#/state/agentSlice";
import AgentState from "#/types/AgentState";
+// Cache for clientLoader results
+let clientLoaderCache: any = null; // eslint-disable-line @typescript-eslint/no-explicit-any
+
export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
try {
const config = await OpenHands.getConfig();
@@ -57,23 +60,28 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
let isAuthed = false;
let githubAuthUrl: string | null = null;
-
- try {
- isAuthed = await userIsAuthenticated();
- if (!isAuthed && window.__GITHUB_CLIENT_ID__) {
- const requestUrl = new URL(request.url);
- githubAuthUrl = generateGitHubAuthUrl(
- window.__GITHUB_CLIENT_ID__,
- requestUrl,
- );
+ let user: GitHubUser | GitHubErrorReponse | null = null;
+ if (!clientLoaderCache || clientLoaderCache.ghToken !== ghToken) {
+ try {
+ isAuthed = await userIsAuthenticated();
+ if (!isAuthed && window.__GITHUB_CLIENT_ID__) {
+ const requestUrl = new URL(request.url);
+ githubAuthUrl = generateGitHubAuthUrl(
+ window.__GITHUB_CLIENT_ID__,
+ requestUrl,
+ );
+ }
+ } catch (error) {
+ isAuthed = false;
+ githubAuthUrl = null;
}
- } catch (error) {
- isAuthed = false;
- githubAuthUrl = null;
- }
- let user: GitHubUser | GitHubErrorReponse | null = null;
- if (ghToken) user = await retrieveGitHubUser(ghToken);
+ if (ghToken) user = await retrieveGitHubUser(ghToken);
+ } else {
+ isAuthed = clientLoaderCache.isAuthed;
+ githubAuthUrl = clientLoaderCache.githubAuthUrl;
+ user = clientLoaderCache.user;
+ }
const settings = getSettings();
await i18n.changeLanguage(settings.LANGUAGE);
@@ -84,7 +92,8 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
token = null;
}
- return defer({
+ // Store the results in cache
+ clientLoaderCache = {
token,
ghToken,
isAuthed,
@@ -93,7 +102,9 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
settingsIsUpdated,
settings,
analyticsConsent,
- });
+ };
+
+ return defer(clientLoaderCache);
};
export function ErrorBoundary() {