Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix: never retry on unauth request

Carl-Gerhard Lindesvärd1b9f7172587a
diff --git a/apps/start/src/integrations/tanstack-query/root-provider.tsx b/apps/start/src/integrations/tanstack-query/root-provider.tsx
index 41fdf7c86..db14bd122 100644
--- a/apps/start/src/integrations/tanstack-query/root-provider.tsx
+++ b/apps/start/src/integrations/tanstack-query/root-provider.tsx
@@ -2,12 +2,24 @@ import type { AppRouter } from '@openpanel/trpc';
import { QueryClient } from '@tanstack/react-query';
import { createIsomorphicFn } from '@tanstack/react-start';
import { getRequestHeaders } from '@tanstack/react-start/server';
-import { createTRPCClient, httpLink } from '@trpc/client';
+import { createTRPCClient, httpLink, TRPCClientError } from '@trpc/client';
import { createTRPCOptionsProxy } from '@trpc/tanstack-react-query';
import { useMemo } from 'react';
import superjson from 'superjson';
import { TRPCProvider } from '@/integrations/trpc/react';
+const DEFAULT_RETRY_COUNT = 3;
+
+function shouldRetryQuery(failureCount: number, error: unknown) {
+ if (error instanceof TRPCClientError) {
+ const status = error.data?.httpStatus;
+ if (typeof status === 'number' && status >= 400 && status < 500) {
+ return false;
+ }
+ }
+ return failureCount < DEFAULT_RETRY_COUNT;
+}
+
export const getIsomorphicHeaders = createIsomorphicFn()
.server(() => {
const headers = getRequestHeaders();
@@ -80,6 +92,7 @@ export function getContext(apiUrl: string) {
staleTime: 1000 * 60 * 5,
gcTime: 1000 * 60 * 10,
refetchOnReconnect: false,
+ retry: shouldRetryQuery,
},
dehydrate: { serializeData: superjson.serialize },
hydrate: { deserializeData: superjson.deserialize },