examples/vue/prefetching-nuxt4
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Vue Query Prefetching Example (Nuxt 4)#
Prefetches a character when you hover its link, so the detail view opens without a loading state.
Built with Nuxt 4.
How it works#
app/services/characters/queries.tsdefinescharactersQueries.list()andcharactersQueries.detail(id)withqueryOptions; the detail usesskipTokenwhen there is noid.app/services/characters/api.tswaits 500 ms before each request to the Rick and Morty API.app/components/CharacterItem.vuecallsqueryClient.querywithstaleTime: 10 * 1000onmouseenterandfocusin, so a character is prefetched at most once every 10 seconds. It also observes the same query withenabled: falseand shows the name in bold once data is cached.app/pages/index.vuelists the characters withuseQuery(charactersQueries.list())and shows the selected one with a seconduseQuery, which renders at once when the character was prefetched.app/pages/character/[id].vuereads the character for the route'sidwithuseQuery, so opening the details of a prefetched character renders without a loading state.app/plugins/vue-query.tsis a Nuxt plugin that creates aQueryClientand installsVueQueryPlugin. On the server it storesdehydrate(queryClient)inuseState('vue-query')onapp:rendered, and on the client it callshydratewith that state onapp:created.
To run this example:
npm installnpm run dev