Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix concurrent GET /api/v1/web-client/config calls (#13905)

Tim O'Farrellef452b6544b7
diff --git a/frontend/__tests__/utils/permission-guard.test.ts b/frontend/__tests__/utils/permission-guard.test.ts
index 5b1d19dd51..7d9b43afc0 100644
--- a/frontend/__tests__/utils/permission-guard.test.ts
+++ b/frontend/__tests__/utils/permission-guard.test.ts
@@ -38,6 +38,7 @@ vi.mock("#/query-client-config", () => ({
queryClient: {
getQueryData: vi.fn(() => mockConfig),
setQueryData: vi.fn(),
+ fetchQuery: vi.fn(() => Promise.resolve(mockConfig)),
},
}));
diff --git a/frontend/src/components/providers/posthog-wrapper.tsx b/frontend/src/components/providers/posthog-wrapper.tsx
index 3f15533508..7a66fff311 100644
--- a/frontend/src/components/providers/posthog-wrapper.tsx
+++ b/frontend/src/components/providers/posthog-wrapper.tsx
@@ -1,6 +1,8 @@
import React from "react";
import { PostHogProvider } from "posthog-js/react";
+import { queryClient } from "#/query-client-config";
import OptionService from "#/api/option-service/option-service.api";
+import { QUERY_KEYS, CONFIG_CACHE_OPTIONS } from "#/hooks/query/query-keys";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
const POSTHOG_BOOTSTRAP_KEY = "posthog_bootstrap";
@@ -47,7 +49,12 @@ export function PostHogWrapper({ children }: { children: React.ReactNode }) {
React.useEffect(() => {
(async () => {
try {
- const config = await OptionService.getConfig();
+ // Use fetchQuery for automatic caching and deduplication
+ const config = await queryClient.fetchQuery({
+ queryKey: QUERY_KEYS.WEB_CLIENT_CONFIG,
+ queryFn: OptionService.getConfig,
+ ...CONFIG_CACHE_OPTIONS,
+ });
setPosthogClientKey(config.posthog_client_key);
} catch {
displayErrorToast("Error fetching PostHog client key");
diff --git a/frontend/src/hooks/query/query-keys.ts b/frontend/src/hooks/query/query-keys.ts
new file mode 100644
index 0000000000..b976849daa
--- /dev/null
+++ b/frontend/src/hooks/query/query-keys.ts
@@ -0,0 +1,17 @@
+/**
+ * Centralized query keys and cache configuration for TanStack Query.
+ * Using constants ensures type safety and prevents typos.
+ */
+
+export const QUERY_KEYS = {
+ /** Web client configuration from the server */
+ WEB_CLIENT_CONFIG: ["web-client-config"] as const,
+} as const;
+
+/** Cache configuration shared across all config-related queries */
+export const CONFIG_CACHE_OPTIONS = {
+ staleTime: 1000 * 60 * 5, // 5 minutes
+ gcTime: 1000 * 60 * 15, // 15 minutes
+} as const;
+
+export type QueryKeys = (typeof QUERY_KEYS)[keyof typeof QUERY_KEYS];
diff --git a/frontend/src/hooks/query/use-config.ts b/frontend/src/hooks/query/use-config.ts
index 8cb877f6f9..c43a0859d3 100644
--- a/frontend/src/hooks/query/use-config.ts
+++ b/frontend/src/hooks/query/use-config.ts
@@ -1,6 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import OptionService from "#/api/option-service/option-service.api";
import { useIsOnIntermediatePage } from "#/hooks/use-is-on-intermediate-page";
+import { QUERY_KEYS, CONFIG_CACHE_OPTIONS } from "./query-keys";
interface UseConfigOptions {
enabled?: boolean;
@@ -10,10 +11,9 @@ export const useConfig = (options?: UseConfigOptions) => {
const isOnIntermediatePage = useIsOnIntermediatePage();
return useQuery({
- queryKey: ["web-client-config"],
+ queryKey: QUERY_KEYS.WEB_CLIENT_CONFIG,
queryFn: OptionService.getConfig,
- staleTime: 1000 * 60 * 5, // 5 minutes
- gcTime: 1000 * 60 * 15, // 15 minutes,
+ ...CONFIG_CACHE_OPTIONS,
enabled: options?.enabled ?? !isOnIntermediatePage,
});
};
diff --git a/frontend/src/routes/billing.tsx b/frontend/src/routes/billing.tsx
index eaac3189a7..c4ad23c08c 100644
--- a/frontend/src/routes/billing.tsx
+++ b/frontend/src/routes/billing.tsx
@@ -16,14 +16,15 @@ import { isBillingHidden } from "#/utils/org/billing-visibility";
import { queryClient } from "#/query-client-config";
import OptionService from "#/api/option-service/option-service.api";
import { WebClientConfig } from "#/api/option-service/option.types";
+import { QUERY_KEYS, CONFIG_CACHE_OPTIONS } from "#/hooks/query/query-keys";
import { getFirstAvailablePath } from "#/utils/settings-utils";
export const clientLoader = async () => {
- let config = queryClient.getQueryData<WebClientConfig>(["web-client-config"]);
- if (!config) {
- config = await OptionService.getConfig();
- queryClient.setQueryData<WebClientConfig>(["web-client-config"], config);
- }
+ const config = await queryClient.fetchQuery<WebClientConfig>({
+ queryKey: QUERY_KEYS.WEB_CLIENT_CONFIG,
+ queryFn: OptionService.getConfig,
+ ...CONFIG_CACHE_OPTIONS,
+ });
const isSaas = config?.app_mode === "saas";
const featureFlags = config?.feature_flags;
diff --git a/frontend/src/routes/settings.tsx b/frontend/src/routes/settings.tsx
index 317037b10d..f4e037c864 100644
--- a/frontend/src/routes/settings.tsx
+++ b/frontend/src/routes/settings.tsx
@@ -6,6 +6,7 @@ import OptionService from "#/api/option-service/option-service.api";
import { queryClient } from "#/query-client-config";
import { SettingsLayout } from "#/components/features/settings";
import { WebClientConfig } from "#/api/option-service/option.types";
+import { QUERY_KEYS, CONFIG_CACHE_OPTIONS } from "#/hooks/query/query-keys";
import { Organization } from "#/types/org";
import { Typography } from "#/ui/typography";
import { useSettingsNavItems } from "#/hooks/use-settings-nav-items";
@@ -36,11 +37,11 @@ export const clientLoader = async ({ request }: Route.ClientLoaderArgs) => {
const { pathname } = url;
// Step 1: Get config first (needed for all checks, no user data required)
- let config = queryClient.getQueryData<WebClientConfig>(["web-client-config"]);
- if (!config) {
- config = await OptionService.getConfig();
- queryClient.setQueryData<WebClientConfig>(["web-client-config"], config);
- }
+ const config = await queryClient.fetchQuery<WebClientConfig>({
+ queryKey: QUERY_KEYS.WEB_CLIENT_CONFIG,
+ queryFn: OptionService.getConfig,
+ ...CONFIG_CACHE_OPTIONS,
+ });
const isSaas = config?.app_mode === "saas";
const featureFlags = config?.feature_flags;
diff --git a/frontend/src/utils/org/permission-guard.ts b/frontend/src/utils/org/permission-guard.ts
index fe8e5c67fe..4a5e44fdfb 100644
--- a/frontend/src/utils/org/permission-guard.ts
+++ b/frontend/src/utils/org/permission-guard.ts
@@ -1,21 +1,22 @@
import { redirect } from "react-router";
+import { queryClient } from "#/query-client-config";
import OptionService from "#/api/option-service/option-service.api";
import { WebClientConfig } from "#/api/option-service/option.types";
-import { queryClient } from "#/query-client-config";
+import { QUERY_KEYS, CONFIG_CACHE_OPTIONS } from "#/hooks/query/query-keys";
import { getFirstAvailablePath } from "#/utils/settings-utils";
import { getActiveOrganizationUser } from "./permission-checks";
import { PermissionKey, rolePermissions } from "./permissions";
/**
- * Helper to get config, using cache or fetching if needed.
+ * Helper to get config, using fetchQuery for automatic caching and deduplication.
+ * Uses the shared query key from QUERY_KEYS to ensure consistency across the app.
*/
async function getConfig(): Promise<WebClientConfig | undefined> {
- let config = queryClient.getQueryData<WebClientConfig>(["web-client-config"]);
- if (!config) {
- config = await OptionService.getConfig();
- queryClient.setQueryData<WebClientConfig>(["web-client-config"], config);
- }
- return config;
+ return queryClient.fetchQuery({
+ queryKey: QUERY_KEYS.WEB_CLIENT_CONFIG,
+ queryFn: OptionService.getConfig,
+ ...CONFIG_CACHE_OPTIONS,
+ });
}
/**