@@ -4,7 +4,7 @@ Vue Query supports prefetching multiple queries on the server and then _dehydrat
44
55### Nuxt 3
66
7- First create ` vue-query.ts ` file in your ` plugins ` directory with following content:
7+ First create ` vue-query.ts ` file in your ` plugins ` directory with the following content:
88``` ts
99import {
1010 VueQueryPlugin ,
@@ -23,14 +23,12 @@ export default (nuxt) => {
2323
2424 nuxt .vueApp .use (VueQueryPlugin , options );
2525
26- // @ts-expect-error Nuxt process variable
2726 if (process .server ) {
2827 nuxt .hooks .hook (" app:rendered" , () => {
2928 nuxt .nuxtState [" vue-query" ] = dehydrate (queryClient );
3029 });
3130 }
3231
33- // @ts-expect-error Nuxt process variable
3432 if (process .client ) {
3533 nuxt .hooks .hook (" app:created" , () => {
3634 hydrate (queryClient , nuxt .nuxtState [" vue-query" ]);
@@ -39,7 +37,8 @@ export default (nuxt) => {
3937};
4038```
4139
42- Then use query in your component
40+ Now you are ready to prefetch some data in your pages with ` onServerPrefetch ` .
41+ - Prefetch all the queries that you need with ` queryClient.prefetchQuery ` or ` suspense `
4342
4443``` ts
4544export default defineComponent ({
@@ -57,35 +56,43 @@ export default defineComponent({
5756
5857### Nuxt 2
5958
60- Use ` useNuxtQueryProvider ` inside setup function of your layout component
59+ First create ` vue-query.js ` file in your ` plugins ` directory with the following content:
6160
6261``` js
63- // layouts/default.vue
64- < template>
65- < div>
66- < nuxt / >
67- < / div>
68- < / template>
62+ import Vue from " vue" ;
63+ import { VueQueryPlugin , QueryClient , hydrate } from " vue-query" ;
6964
70- < script>
71- import { defineComponent } from " @nuxtjs/composition-api" ;
72- import { useNuxtQueryProvider } from " vue-query/nuxt" ;
65+ export default (context ) => {
66+ // Modify your Vue Query global settings here
67+ const queryClient = new QueryClient ({
68+ defaultOptions: { queries: { staleTime: 1000 } },
69+ });
70+ const options = { queryClient };
7371
74- export default defineComponent ({
75- setup () {
76- useNuxtQueryProvider ();
77- },
78- });
79- < / script>
72+ Vue .use (VueQueryPlugin, options);
8073
74+ if (process .client ) {
75+ if (context .nuxtState && context .nuxtState [" vue-query" ]) {
76+ hydrate (queryClient, context .nuxtState [" vue-query" ]);
77+ }
78+ }
79+ };
80+ ```
81+
82+ Add this plugin to your ` nuxt.config.js `
83+ ``` js
84+ module .exports = {
85+ ...
86+ plugins: [' ~/plugins/vue-query.js' ],
87+ };
8188```
8289
8390Now you are ready to prefetch some data in your pages with ` onServerPrefetch ` .
8491
85- - Use ` useQueryClient ` to get server-side instance of queryClient
8692- Use ` useContext ` to get nuxt context
87- - Prefetch all the queries that you need with ` prefetchQuery `
88- - Use ` useNuxtDehydrate ` to dehydrate the query cache and pass it to the client-side via nuxt context.
93+ - Use ` useQueryClient ` to get server-side instance of ` queryClient `
94+ - Prefetch all the queries that you need with ` queryClient.prefetchQuery ` or ` suspense `
95+ - Dehydrate ` queryClient ` to the ` nuxtContext `
8996
9097``` js
9198// pages/todos.vue
@@ -102,22 +109,21 @@ import {
102109 onServerPrefetch ,
103110 useContext ,
104111} from " @nuxtjs/composition-api" ;
105- import { useQuery , useQueryClient } from " vue-query" ;
106- import { useNuxtDehydrate } from " vue-query/nuxt" ;
112+ import { useQuery , useQueryClient , dehydrate } from " vue-query" ;
107113
108114export default defineComponent ({
109115 setup () {
110116 // This will be prefetched and sent from the server
111- const { refetch , data } = useQuery (" todos" , getTodos);
117+ const { refetch , data , suspense } = useQuery (" todos" , getTodos);
112118 // This won't be prefetched, it will start fetching on client side
113119 const { data2 } = useQuery (" todos2" , getTodos);
114120
115121 onServerPrefetch (async () => {
116- const { ssrContext } = useContext ();
122+ const { ssrContext } = useContext ();
117123 const queryClient = useQueryClient ();
118- await queryClient . prefetchQuery ( " todos " , getTodos );
124+ await suspense ( );
119125
120- useNuxtDehydrate ( ssrContext, queryClient);
126+ ssrContext . nuxt . vueQueryState = dehydrate ( queryClient);
121127 });
122128
123129 return {
@@ -129,7 +135,7 @@ export default defineComponent({
129135< / script>
130136```
131137
132- As demonstrated, it's fine to prefetch some queries and let others fetch on the queryClient. This means you can control what content server renders or not by adding or removing ` prefetchQuery ` for a specific query.
138+ As demonstrated, it's fine to prefetch some queries and let others fetch on the queryClient. This means you can control what content server renders or not by adding or removing ` prefetchQuery ` or ` suspense ` for a specific query.
133139
134140## Using Vite SSR
135141
0 commit comments