Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(mobile): fix query client confusion causing missed invalidation

Mohamed Bassem0c25cf230b00
diff --git a/packages/shared-react/providers/trpc-provider.tsx b/packages/shared-react/providers/trpc-provider.tsx
index 2c41aa11..5728fca5 100644
--- a/packages/shared-react/providers/trpc-provider.tsx
+++ b/packages/shared-react/providers/trpc-provider.tsx
@@ -13,6 +13,22 @@ interface Settings {
customHeaders?: Record<string, string>;
}
+let browserQueryClient: QueryClient | undefined = undefined;
+
+function getQueryClient() {
+ if (typeof window === "undefined") {
+ // Server: always make a new query client
+ return new QueryClient();
+ } else {
+ // Browser: make a new query client if we don't already have one
+ // This is very important, so we don't re-make a new client if React
+ // suspends during the initial render. This may not be needed if we
+ // have a suspense boundary BELOW the creation of the query client
+ if (!browserQueryClient) browserQueryClient = new QueryClient();
+ return browserQueryClient;
+ }
+}
+
function getTRPCClient(settings: Settings) {
return createTRPCClient<AppRouter>({
links: [
@@ -40,7 +56,7 @@ export function TRPCSettingsProvider({
settings: Settings;
children: React.ReactNode;
}) {
- const queryClient = useMemo(() => new QueryClient(), [settings]);
+ const queryClient = getQueryClient();
const trpcClient = useMemo(() => getTRPCClient(settings), [settings]);
return (