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

README.md

17 lines1.0 KB

1# TanStack Solid Query Prefetching Example (SolidStart)
2​
3Prefetches a character when you hover its link, so the detail view opens without a loading state.
4​
5Built with [SolidStart](https://start.solidjs.com).
6​
7## How it works
8​
9- `src/services/characters/queries.ts` defines `charactersQueries.list()` and `charactersQueries.detail(id)` with `queryOptions`. The detail options use `skipToken` when there is no `id`.
10- `src/services/characters/api.ts` fetches characters from the Rick and Morty API after a 500 ms delay.
11- In `src/routes/index.tsx`, each `CharacterItem` calls `queryClient.query(...)` with a 10-second `staleTime` on `mouseenter` and `focusin`, so a character is fetched again only when its cached data is older than that. A `useQuery` with `enabled: false` only observes the cache and turns the name bold once the data is there.
12- `src/routes/character/[id].tsx` reads the same `charactersQueries.detail(id)` with `useQuery`, so a prefetched character renders without a loading state.
13​
14To run this example:
15​
16- `npm install`
17- `npm run dev`