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