Skip to content

Commit edf61f3

Browse files
docs: nuxt3 example (#170)
1 parent 3a17330 commit edf61f3

File tree

8 files changed

+174
-11
lines changed

8 files changed

+174
-11
lines changed

docs/guides/ssr.md

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,63 @@
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.
82

93
## Using Nuxt.js
104

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
1243

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+
```
1457

15-
To support caching queries on the server and set up hydration:
58+
### Nuxt 2
1659

17-
- Use `useNuxtQueryProvider` inside setup function of your layout component
60+
Use `useNuxtQueryProvider` inside setup function of your layout component
1861

1962
```js
2063
// layouts/default.vue

examples/nuxt3-simple/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
*.log
3+
.nuxt
4+
nuxt.d.ts
5+
.output
6+
.env

examples/nuxt3-simple/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Nuxt 3 Minimal Starter
2+
3+
We recommend to look at the [documentation](https://v3.nuxtjs.org).
4+
5+
## Setup
6+
7+
Make sure to install the dependencies
8+
9+
```bash
10+
yarn install
11+
```
12+
13+
## Development
14+
15+
Start the development server on http://localhost:3000
16+
17+
```bash
18+
yarn dev
19+
```
20+
21+
## Production
22+
23+
Build the application for production:
24+
25+
```bash
26+
yarn build
27+
```
28+
29+
Checkout the [deployment documentation](https://v3.nuxtjs.org/docs/deployment).

examples/nuxt3-simple/app.vue

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<div>{{ data }}</div>
3+
</template>
4+
5+
<script lang="ts">
6+
import { useQuery } from "vue-query";
7+
import { defineComponent, onServerPrefetch } from "vue";
8+
9+
interface Post {
10+
userId: number;
11+
id: number;
12+
title: string;
13+
body: string;
14+
}
15+
16+
const fetcher = async (): Promise<Post[]> =>
17+
await fetch("https://jsonplaceholder.typicode.com/posts").then((response) =>
18+
response.json()
19+
);
20+
21+
export default defineComponent({
22+
setup() {
23+
const { data, suspense } = useQuery("test", fetcher);
24+
25+
onServerPrefetch(async () => {
26+
await suspense();
27+
});
28+
29+
return { data };
30+
},
31+
});
32+
</script>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { defineNuxtConfig } from "nuxt3";
2+
3+
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
4+
export default defineNuxtConfig({});

examples/nuxt3-simple/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"dev": "nuxi dev",
5+
"build": "nuxi build",
6+
"start": "node .output/server/index.mjs"
7+
},
8+
"devDependencies": {
9+
"nuxt3": "latest"
10+
},
11+
"dependencies": {
12+
"vue-query": ">=1.0.0"
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
VueQueryPlugin,
3+
VueQueryPluginOptions,
4+
QueryClient,
5+
hydrate,
6+
dehydrate,
7+
} from "vue-query";
8+
9+
export default (nuxt) => {
10+
// Modify your Vue Query global settings here
11+
const queryClient = new QueryClient({
12+
defaultOptions: { queries: { staleTime: 1000 } },
13+
});
14+
const options: VueQueryPluginOptions = { queryClient };
15+
16+
nuxt.vueApp.use(VueQueryPlugin, options);
17+
18+
// @ts-expect-error Nuxt process variable
19+
if (process.server) {
20+
nuxt.hooks.hook("app:rendered", () => {
21+
nuxt.nuxtState["vue-query"] = dehydrate(queryClient);
22+
});
23+
}
24+
25+
// @ts-expect-error Nuxt process variable
26+
if (process.client) {
27+
nuxt.hooks.hook("app:created", () => {
28+
hydrate(queryClient, nuxt.nuxtState["vue-query"]);
29+
});
30+
}
31+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
// https://v3.nuxtjs.org/concepts/typescript
3+
"extends": "./.nuxt/tsconfig.json"
4+
}

0 commit comments

Comments
 (0)