Skip to content

Commit

Permalink
docs(vue-query): update SSR guide for nuxt2 (#8001)
Browse files Browse the repository at this point in the history
* docs: update SSR guide for nuxt2

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
DamianOsipiuk and autofix-ci[bot] authored Sep 1, 2024
1 parent 2be2a79 commit cd91357
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions docs/framework/vue/guides/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ export default (context) => {
const queryClient = new QueryClient({
defaultOptions: { queries: { staleTime: 5000 } },
})
const options = { queryClient }

Vue.use(VueQueryPlugin, options)
if (process.server) {
context.ssrContext.VueQuery = queryClient
}

if (process.client) {
Vue.use(VueQueryPlugin, { queryClient })

if (context.nuxtState && context.nuxtState.vueQueryState) {
hydrate(queryClient, context.nuxtState.vueQueryState)
}
Expand All @@ -100,7 +103,7 @@ Add this plugin to your `nuxt.config.js`
module.exports = {
...
plugins: ['~/plugins/vue-query.js'],
};
}
```

Now you are ready to prefetch some data in your pages with `onServerPrefetch`.
Expand All @@ -110,7 +113,7 @@ Now you are ready to prefetch some data in your pages with `onServerPrefetch`.
- Prefetch all the queries that you need with `queryClient.prefetchQuery` or `suspense`
- Dehydrate `queryClient` to the `nuxtContext`

```js
```vue
// pages/todos.vue
<template>
<div>
Expand All @@ -124,30 +127,45 @@ import {
defineComponent,
onServerPrefetch,
useContext,
} from "@nuxtjs/composition-api";
import { useQuery, useQueryClient, dehydrate } from "@tanstack/vue-query";
} from '@nuxtjs/composition-api'
import { useQuery, useQueryClient, dehydrate } from '@tanstack/vue-query'
export default defineComponent({
setup() {
// Get QueryClient either from SSR context, or Vue context
const { ssrContext } = useContext()
// Make sure to provide `queryClient` as a second parameter to `useQuery` calls
const queryClient =
(ssrContext != null && ssrContext.VueQuery) || useQueryClient()
// This will be prefetched and sent from the server
const { refetch, data, suspense } = useQuery("todos", getTodos);
const { data, refetch, suspense } = useQuery(
{
queryKey: ['todos'],
queryFn: getTodos,
},
queryClient,
)
// This won't be prefetched, it will start fetching on client side
const { data2 } = useQuery("todos2", getTodos);

const { ssrContext } = useContext();
const queryClient = useQueryClient();
const { data2 } = useQuery(
{
queryKey: 'todos2',
queryFn: getTodos,
},
queryClient,
)
onServerPrefetch(async () => {
await suspense();
ssrContext.nuxt.vueQueryState = dehydrate(queryClient);
});
await suspense()
ssrContext.nuxt.vueQueryState = dehydrate(queryClient)
})
return {
refetch,
data,
};
}
},
});
})
</script>
```

Expand Down

0 comments on commit cd91357

Please sign in to comment.