|
1 | | -Vue Query supports two ways of prefetching data on the server and passing that to the queryClient. |
2 | | - |
3 | | -- Prefetch the data yourself and pass it in as `initialData` |
4 | | - - Quick to set up for simple cases |
5 | | - - Has some caveats |
6 | | -- Prefetch the query on the server, dehydrate the cache and rehydrate it on the client |
7 | | - - Requires slightly more setup up front |
| 1 | +Vue Query supports prefetching multiple queries on the server and then _dehydrating_ those queries to the queryClient. This means the server can prerender markup that is immediately available on page load and as soon as JS is available, Vue Query can upgrade or _hydrate_ those queries with the full functionality of the library. This includes refetching those queries on the client if they have become stale since the time they were rendered on the server. |
8 | 2 |
|
9 | 3 | ## Using Nuxt.js |
10 | 4 |
|
11 | | -### Using Hydration |
| 5 | +### Nuxt 3 |
| 6 | + |
| 7 | +First create `vue-query.ts` file in your `plugins` directory with following content: |
| 8 | +```ts |
| 9 | +import { |
| 10 | + VueQueryPlugin, |
| 11 | + VueQueryPluginOptions, |
| 12 | + QueryClient, |
| 13 | + hydrate, |
| 14 | + dehydrate, |
| 15 | +} from "vue-query"; |
| 16 | + |
| 17 | +export default (nuxt) => { |
| 18 | + // Modify your Vue Query global settings here |
| 19 | + const queryClient = new QueryClient({ |
| 20 | + defaultOptions: { queries: { staleTime: 1000 } }, |
| 21 | + }); |
| 22 | + const options: VueQueryPluginOptions = { queryClient }; |
| 23 | + |
| 24 | + nuxt.vueApp.use(VueQueryPlugin, options); |
| 25 | + |
| 26 | + // @ts-expect-error Nuxt process variable |
| 27 | + if (process.server) { |
| 28 | + nuxt.hooks.hook("app:rendered", () => { |
| 29 | + nuxt.nuxtState["vue-query"] = dehydrate(queryClient); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + // @ts-expect-error Nuxt process variable |
| 34 | + if (process.client) { |
| 35 | + nuxt.hooks.hook("app:created", () => { |
| 36 | + hydrate(queryClient, nuxt.nuxtState["vue-query"]); |
| 37 | + }); |
| 38 | + } |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +Then use query in your component |
12 | 43 |
|
13 | | -Vue Query supports prefetching multiple queries on the server in Nuxt.js and then _dehydrating_ those queries to the queryClient. This means the server can prerender markup that is immediately available on page load and as soon as JS is available, Vue Query can upgrade or _hydrate_ those queries with the full functionality of the library. This includes refetching those queries on the client if they have become stale since the time they were rendered on the server. |
| 44 | +```ts |
| 45 | +export default defineComponent({ |
| 46 | + setup() { |
| 47 | + const { data, suspense } = useQuery("test", fetcher); |
| 48 | + |
| 49 | + onServerPrefetch(async () => { |
| 50 | + await suspense(); |
| 51 | + }); |
| 52 | + |
| 53 | + return { data }; |
| 54 | + }, |
| 55 | +}); |
| 56 | +``` |
14 | 57 |
|
15 | | -To support caching queries on the server and set up hydration: |
| 58 | +### Nuxt 2 |
16 | 59 |
|
17 | | -- Use `useNuxtQueryProvider` inside setup function of your layout component |
| 60 | +Use `useNuxtQueryProvider` inside setup function of your layout component |
18 | 61 |
|
19 | 62 | ```js |
20 | 63 | // layouts/default.vue |
|
0 commit comments