Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(frontend): Add React Query for Data Fetching (#10190)

Abhimanyu Yadav29395665c3e2
diff --git a/autogpt_platform/frontend/package.json b/autogpt_platform/frontend/package.json
index 868f48b329..b061e4b613 100644
--- a/autogpt_platform/frontend/package.json
+++ b/autogpt_platform/frontend/package.json
@@ -50,6 +50,7 @@
"@sentry/nextjs": "9.27.0",
"@supabase/ssr": "0.6.1",
"@supabase/supabase-js": "2.50.0",
+ "@tanstack/react-query": "5.80.7",
"@tanstack/react-table": "8.21.3",
"@types/jaro-winkler": "0.2.4",
"@xyflow/react": "12.6.4",
diff --git a/autogpt_platform/frontend/pnpm-lock.yaml b/autogpt_platform/frontend/pnpm-lock.yaml
index a30e183729..c16ddbb08c 100644
--- a/autogpt_platform/frontend/pnpm-lock.yaml
+++ b/autogpt_platform/frontend/pnpm-lock.yaml
@@ -86,6 +86,9 @@ importers:
'@supabase/supabase-js':
specifier: 2.50.0
version: 2.50.0
+ '@tanstack/react-query':
+ specifier: 5.80.7
+ version: 5.80.7([email protected])
'@tanstack/react-table':
specifier: 8.21.3
@@ -2501,6 +2504,14 @@ packages:
resolution: {integrity: sha512-JQ5TuMi45Owi4/BIMAJBoSQoOJu12oOk/gADqlcUL9JEdHB8vyjUSsxqeNXnmXHjYKMi2WcYtezGEEhqUI/E2g==}
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-s09l5zeUKC8q7DCCCIkVSns8zZrK4ZDT6ryEjxNBFi68G4z2EBobBS7rdOY3r6W1WbUDpc1fe5oY+YO/+2UVUg==}
+
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-u2F0VK6+anItoEvB3+rfvTO9GEh2vb00Je05OwlUe/A0lkJBgW1HckiY3f9YZa+jx6IOe4dHPh10dyp9aY3iRQ==}
+ peerDependencies:
+ react: ^18 || ^19
+
'@tanstack/[email protected]':
resolution: {integrity: sha512-5nNMTSETP4ykGegmVkhjcS8tTLW6Vl4axfEGQN3v0zdHYbK4UfoqfPChclTrJ4EoK9QynqAu9oUf8VEmrpZ5Ww==}
engines: {node: '>=12'}
@@ -8935,6 +8946,13 @@ snapshots:
dependencies:
tslib: 2.8.1
+ '@tanstack/[email protected]': {}
+
+ dependencies:
+ '@tanstack/query-core': 5.80.7
+ react: 18.3.1
+
dependencies:
'@tanstack/table-core': 8.21.3
diff --git a/autogpt_platform/frontend/src/app/providers.tsx b/autogpt_platform/frontend/src/app/providers.tsx
index db9725d3f2..45f9fa5cc3 100644
--- a/autogpt_platform/frontend/src/app/providers.tsx
+++ b/autogpt_platform/frontend/src/app/providers.tsx
@@ -8,19 +8,24 @@ import { TooltipProvider } from "@/components/ui/tooltip";
import CredentialsProvider from "@/components/integrations/credentials-provider";
import { LaunchDarklyProvider } from "@/components/feature-flag/feature-flag-provider";
import OnboardingProvider from "@/components/onboarding/onboarding-provider";
+import { QueryClientProvider } from "@tanstack/react-query";
+import { getQueryClient } from "@/lib/react-query/queryClient";
export function Providers({ children, ...props }: ThemeProviderProps) {
+ const queryClient = getQueryClient();
return (
- <NextThemesProvider {...props}>
- <BackendAPIProvider>
- <CredentialsProvider>
- <LaunchDarklyProvider>
- <OnboardingProvider>
- <TooltipProvider>{children}</TooltipProvider>
- </OnboardingProvider>
- </LaunchDarklyProvider>
- </CredentialsProvider>
- </BackendAPIProvider>
- </NextThemesProvider>
+ <QueryClientProvider client={queryClient}>
+ <NextThemesProvider {...props}>
+ <BackendAPIProvider>
+ <CredentialsProvider>
+ <LaunchDarklyProvider>
+ <OnboardingProvider>
+ <TooltipProvider>{children}</TooltipProvider>
+ </OnboardingProvider>
+ </LaunchDarklyProvider>
+ </CredentialsProvider>
+ </BackendAPIProvider>
+ </NextThemesProvider>
+ </QueryClientProvider>
);
}
diff --git a/autogpt_platform/frontend/src/lib/react-query/queryClient.ts b/autogpt_platform/frontend/src/lib/react-query/queryClient.ts
new file mode 100644
index 0000000000..38bf858d21
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/react-query/queryClient.ts
@@ -0,0 +1,35 @@
+"use client";
+
+import { isServer, QueryClient } from "@tanstack/react-query";
+
+function makeQueryClient() {
+ return new QueryClient({
+ defaultOptions: {
+ queries: {
+ // Added this because if staleTime is 0 (default), the data fetched on the server becomes stale immediately on the client, and it refetches again.
+ staleTime: 60 * 1000,
+
+ // Highlighting some important defaults to avoid confusion
+ // Queries are stale by default → triggers background refetch
+ // Refetch triggers: on mount, window focus, reconnect
+ // Failed queries retry 3 times with exponential backoff
+ // Inactive queries are GC'd after 5 mins (gcTime = 5 * 60 * 1000)
+ // Structural sharing is enabled for efficient data comparison
+ // For more info, visit https://tanstack.com/query/latest/docs/framework/react/guides/important-defaults
+ },
+ },
+ });
+}
+
+let browserQueryClient: QueryClient | undefined = undefined;
+
+export function getQueryClient() {
+ if (isServer) {
+ // Server: create new client every time (so one user's data doesn't leak to another)
+ return makeQueryClient();
+ } else {
+ // Client: reuse the same client (to keep cache + avoid bugs if React suspends)
+ if (!browserQueryClient) browserQueryClient = makeQueryClient();
+ return browserQueryClient;
+ }
+}