Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

prefetch.tsx

36 lines1.1 KB

1import { useQueryClient } from '@tanstack/solid-query'
2import { isServer } from 'solid-js/web'
3import { Title } from 'solid-start'
4import { UserInfo, userInfoQueryOpts } from '~/components/user-info'
5​
6export default function Prefetch() {
7 const queryClient = useQueryClient()
8​
9 if (isServer) {
10 void queryClient.prefetchQuery(userInfoQueryOpts({ sleep: 500 }))
11 }
12​
13 return (
14 <main>
15 <Title>Solid Query - Prefetch</Title>
16​
17 <h1>Solid Query - Prefetch Example</h1>
18​
19 <div class="description">
20 <p>
21 In some cases you may want to prefetch a query on the server before
22 the component with the relevant `createQuery` call is mounted. A major
23 use case for this is in router data loaders, in order to avoid request
24 waterfalls.
25 </p>
26 <p>
27 In this example we prefetch the user query (on the server only). There
28 should be no extra `fetchUser.start` and `fetchUser.done` logs in the
29 console on the client when refreshing the page.
30 </p>
31 </div>
32​
33 <UserInfo sleep={500} deferStream />
34 </main>
35 )