Spaces:
Running

axeronis commited on
Commit
06c7d5e
·
1 Parent(s): 2764763

Perf: Add React Query localStorage persistence

Browse files

- Installed @tanstack/react-query-persist-client
- Cache now survives page refresh (24h)
- Uses localStorage key 'microfunnel_query_cache'
- Pages using useQuery (dashboard, chats, channels) load instantly

frontend/app/providers.tsx CHANGED
@@ -1,16 +1,34 @@
1
  'use client'
2
 
3
- import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4
- import { useState } from 'react'
 
 
5
  import { AuthProvider } from '@/contexts/AuthContext'
6
  import { ToastProvider } from '@/contexts/ToastContext'
7
 
 
 
 
 
 
 
 
 
 
8
  export function Providers({ children }: { children: React.ReactNode }) {
 
 
 
 
 
 
 
9
  const [queryClient] = useState(() => new QueryClient({
10
  defaultOptions: {
11
  queries: {
12
  staleTime: 5 * 60 * 1000, // 5 minutes - data is fresh
13
- gcTime: 30 * 60 * 1000, // 30 minutes - keep in cache
14
  refetchOnWindowFocus: false, // Don't refetch on tab focus
15
  refetchOnMount: false, // Use cached data on mount
16
  refetchOnReconnect: false, // Don't refetch on reconnect
@@ -19,13 +37,31 @@ export function Providers({ children }: { children: React.ReactNode }) {
19
  },
20
  }))
21
 
 
 
 
 
 
 
 
 
 
 
 
22
  return (
23
- <QueryClientProvider client={queryClient}>
 
 
 
 
 
 
 
24
  <AuthProvider>
25
  <ToastProvider>
26
  {children}
27
  </ToastProvider>
28
  </AuthProvider>
29
- </QueryClientProvider>
30
  )
31
  }
 
1
  'use client'
2
 
3
+ import { QueryClient } from '@tanstack/react-query'
4
+ import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
5
+ import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
6
+ import { useState, useEffect } from 'react'
7
  import { AuthProvider } from '@/contexts/AuthContext'
8
  import { ToastProvider } from '@/contexts/ToastContext'
9
 
10
+ // Create persister for localStorage (only on client)
11
+ const createPersister = () => {
12
+ if (typeof window === 'undefined') return undefined
13
+ return createSyncStoragePersister({
14
+ storage: window.localStorage,
15
+ key: 'microfunnel_query_cache',
16
+ })
17
+ }
18
+
19
  export function Providers({ children }: { children: React.ReactNode }) {
20
+ const [persister, setPersister] = useState<ReturnType<typeof createSyncStoragePersister> | undefined>(undefined)
21
+
22
+ // Create persister on client only
23
+ useEffect(() => {
24
+ setPersister(createPersister())
25
+ }, [])
26
+
27
  const [queryClient] = useState(() => new QueryClient({
28
  defaultOptions: {
29
  queries: {
30
  staleTime: 5 * 60 * 1000, // 5 minutes - data is fresh
31
+ gcTime: 24 * 60 * 60 * 1000, // 24 hours - keep in cache for persistence
32
  refetchOnWindowFocus: false, // Don't refetch on tab focus
33
  refetchOnMount: false, // Use cached data on mount
34
  refetchOnReconnect: false, // Don't refetch on reconnect
 
37
  },
38
  }))
39
 
40
+ // If no persister yet (SSR), just use regular QueryClientProvider
41
+ if (!persister) {
42
+ return (
43
+ <AuthProvider>
44
+ <ToastProvider>
45
+ {children}
46
+ </ToastProvider>
47
+ </AuthProvider>
48
+ )
49
+ }
50
+
51
  return (
52
+ <PersistQueryClientProvider
53
+ client={queryClient}
54
+ persistOptions={{
55
+ persister,
56
+ maxAge: 24 * 60 * 60 * 1000, // 24 hours
57
+ buster: 'v1', // Change this to invalidate all caches
58
+ }}
59
+ >
60
  <AuthProvider>
61
  <ToastProvider>
62
  {children}
63
  </ToastProvider>
64
  </AuthProvider>
65
+ </PersistQueryClientProvider>
66
  )
67
  }
frontend/package-lock.json CHANGED
@@ -14,6 +14,7 @@
14
  "@react-oauth/google": "^0.12.2",
15
  "@supabase/supabase-js": "^2.84.0",
16
  "@tanstack/react-query": "^5.90.10",
 
17
  "axios": "^1.13.2",
18
  "class-variance-authority": "^0.7.1",
19
  "clsx": "^2.1.1",
@@ -1913,28 +1914,59 @@
1913
  }
1914
  },
1915
  "node_modules/@tanstack/query-core": {
1916
- "version": "5.90.10",
1917
- "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.90.10.tgz",
1918
- "integrity": "sha512-EhZVFu9rl7GfRNuJLJ3Y7wtbTnENsvzp+YpcAV7kCYiXni1v8qZh++lpw4ch4rrwC0u/EZRnBHIehzCGzwXDSQ==",
1919
  "license": "MIT",
1920
  "funding": {
1921
  "type": "github",
1922
  "url": "https://github.com/sponsors/tannerlinsley"
1923
  }
1924
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
1925
  "node_modules/@tanstack/react-query": {
1926
- "version": "5.90.10",
1927
- "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.90.10.tgz",
1928
- "integrity": "sha512-BKLss9Y8PQ9IUjPYQiv3/Zmlx92uxffUOX8ZZNoQlCIZBJPT5M+GOMQj7xislvVQ6l1BstBjcX0XB/aHfFYVNw==",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1929
  "license": "MIT",
1930
  "dependencies": {
1931
- "@tanstack/query-core": "5.90.10"
1932
  },
1933
  "funding": {
1934
  "type": "github",
1935
  "url": "https://github.com/sponsors/tannerlinsley"
1936
  },
1937
  "peerDependencies": {
 
1938
  "react": "^18 || ^19"
1939
  }
1940
  },
 
14
  "@react-oauth/google": "^0.12.2",
15
  "@supabase/supabase-js": "^2.84.0",
16
  "@tanstack/react-query": "^5.90.10",
17
+ "@tanstack/react-query-persist-client": "^5.90.14",
18
  "axios": "^1.13.2",
19
  "class-variance-authority": "^0.7.1",
20
  "clsx": "^2.1.1",
 
1914
  }
1915
  },
1916
  "node_modules/@tanstack/query-core": {
1917
+ "version": "5.90.12",
1918
+ "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.90.12.tgz",
1919
+ "integrity": "sha512-T1/8t5DhV/SisWjDnaiU2drl6ySvsHj1bHBCWNXd+/T+Hh1cf6JodyEYMd5sgwm+b/mETT4EV3H+zCVczCU5hg==",
1920
  "license": "MIT",
1921
  "funding": {
1922
  "type": "github",
1923
  "url": "https://github.com/sponsors/tannerlinsley"
1924
  }
1925
  },
1926
+ "node_modules/@tanstack/query-persist-client-core": {
1927
+ "version": "5.91.11",
1928
+ "resolved": "https://registry.npmjs.org/@tanstack/query-persist-client-core/-/query-persist-client-core-5.91.11.tgz",
1929
+ "integrity": "sha512-NNpRGxQY/nVOdzfs5QbevPjGsUVoEiFwqxxaopLyu6todwtDOCfIOfhXSmpMVXBiCxUn7kqaUB1iwaBKqoAVRQ==",
1930
+ "license": "MIT",
1931
+ "dependencies": {
1932
+ "@tanstack/query-core": "5.90.12"
1933
+ },
1934
+ "funding": {
1935
+ "type": "github",
1936
+ "url": "https://github.com/sponsors/tannerlinsley"
1937
+ }
1938
+ },
1939
  "node_modules/@tanstack/react-query": {
1940
+ "version": "5.90.12",
1941
+ "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.90.12.tgz",
1942
+ "integrity": "sha512-graRZspg7EoEaw0a8faiUASCyJrqjKPdqJ9EwuDRUF9mEYJ1YPczI9H+/agJ0mOJkPCJDk0lsz5QTrLZ/jQ2rg==",
1943
+ "license": "MIT",
1944
+ "peer": true,
1945
+ "dependencies": {
1946
+ "@tanstack/query-core": "5.90.12"
1947
+ },
1948
+ "funding": {
1949
+ "type": "github",
1950
+ "url": "https://github.com/sponsors/tannerlinsley"
1951
+ },
1952
+ "peerDependencies": {
1953
+ "react": "^18 || ^19"
1954
+ }
1955
+ },
1956
+ "node_modules/@tanstack/react-query-persist-client": {
1957
+ "version": "5.90.14",
1958
+ "resolved": "https://registry.npmjs.org/@tanstack/react-query-persist-client/-/react-query-persist-client-5.90.14.tgz",
1959
+ "integrity": "sha512-jTGnr/DBlzV/UYqU+b8bZWECBuqh3Q3g7Ih50IktZkvmwTUsidQhhl2JyknNYVZkl5AgMfPAywNKZLTI82wmlA==",
1960
  "license": "MIT",
1961
  "dependencies": {
1962
+ "@tanstack/query-persist-client-core": "5.91.11"
1963
  },
1964
  "funding": {
1965
  "type": "github",
1966
  "url": "https://github.com/sponsors/tannerlinsley"
1967
  },
1968
  "peerDependencies": {
1969
+ "@tanstack/react-query": "^5.90.12",
1970
  "react": "^18 || ^19"
1971
  }
1972
  },
frontend/package.json CHANGED
@@ -15,6 +15,7 @@
15
  "@react-oauth/google": "^0.12.2",
16
  "@supabase/supabase-js": "^2.84.0",
17
  "@tanstack/react-query": "^5.90.10",
 
18
  "axios": "^1.13.2",
19
  "class-variance-authority": "^0.7.1",
20
  "clsx": "^2.1.1",
 
15
  "@react-oauth/google": "^0.12.2",
16
  "@supabase/supabase-js": "^2.84.0",
17
  "@tanstack/react-query": "^5.90.10",
18
+ "@tanstack/react-query-persist-client": "^5.90.14",
19
  "axios": "^1.13.2",
20
  "class-variance-authority": "^0.7.1",
21
  "clsx": "^2.1.1",