Open History to see the last commit
1import { MutationCache, QueryClient } from '@tanstack/solid-query'
2import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
3import { PersistQueryClientProvider } from '@tanstack/solid-query-persist-client'
4import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
5import { Route, Router } from '@solidjs/router'
6β
7import { moviesMutations, moviesQueries } from './services/movies/queries'
8import List from './routes/list'
9import Detail, { preload as detailPreload } from './routes/detail'
10β
11const persister = createAsyncStoragePersister({
12 storage: window.localStorage,
13})
14β
15const queryClient = new QueryClient({
16 defaultOptions: {
17 queries: {
18 gcTime: 24 * 60 * 60 * 1000, // 24 hours
19 staleTime: 2000,
20 retry: 0,
21 },
22 },
23 // configure global cache callbacks to show error notifications
24 mutationCache: new MutationCache({
25 onError: (error) => {
26 showNotification(error.message, 'error')
27 },
28 }),
29})
30β
31// we need a default mutation function so that paused mutations can resume after a page reload
32queryClient.setMutationDefaults(moviesQueries.all(), {
33 ...moviesMutations.update(),
34 onSuccess: (data) => {
35 showNotification(data.message, 'success')
36 },
37})
38β
39function showNotification(message: string, type: 'success' | 'error') {
40 const el = document.createElement('div')
41 el.style.cssText = `
42 position: fixed; top: 16px; right: 16px; z-index: 9999;
43 padding: 12px 24px; border-radius: 4px; color: white;
44 background: ${type === 'success' ? '#22c55e' : '#ef4444'};
45 animation: fadeIn 0.3s ease-in;
46 `
47 el.textContent = message
48 document.body.appendChild(el)
49 setTimeout(() => el.remove(), 3000)
50}
51β
52let isRestoring = true
53β
54export default function App() {
55 return (
56 <PersistQueryClientProvider
57 client={queryClient}
58 persistOptions={{ persister }}
59 onSuccess={() => {
60 isRestoring = false
61 // resume mutations after initial restore from localStorage was successful
62 queryClient.resumePausedMutations().then(() => {
63 queryClient.invalidateQueries()
64 })
65 }}
66 onError={() => {
67 isRestoring = false
68 }}
69 >
70 <Router>
71 <Route path="/" component={List} />
72 <Route
73 path="/:movieId"
74 component={Detail}
75 preload={detailPreload(queryClient, () => isRestoring)}
76 />
77 </Router>
78 <SolidQueryDevtools initialIsOpen />
79 </PersistQueryClientProvider>
80 )
81}