Skip to content

Commit cb02c70

Browse files
authored
docs: add vite-ssr example and docs (#76)
1 parent e701752 commit cb02c70

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Suspense (experimental)](examples/suspense.md)
1414
- [Vue 2.x (experimental)](examples/vue-2.x.md)
1515
- [Nuxt.js (experimental)](examples/nuxt.md)
16+
- [Vite SSR (experimental)](examples/vite-ssr.md)
1617

1718
- Guides & Concepts
1819

docs/examples/vite-ssr.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- [Open in Codesandbox](https://codesandbox.io/s/github/frandiox/vite-ssr-vue-query/tree/main/)
2+
- [View Source](https://github.com/frandiox/vite-ssr-vue-query)
3+
4+
<iframe src="https://codesandbox.io/embed/github/frandiox/vite-ssr-vue-query/tree/main?hidenavigation=1&view=preview&codemirror=1"
5+
style="height:700px; border:0; overflow:hidden;"
6+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
7+
></iframe>

docs/guides/ssr.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,99 @@ export default defineComponent({
8888

8989
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.
9090

91+
## Using Vite SSR
92+
93+
Pass VueQuery client cache to [vite-ssr](https://github.com/frandiox/vite-ssr) in order to serialize the state in the DOM:
94+
95+
```js
96+
// main.js (entry point)
97+
import App from "./App.vue";
98+
import viteSSR from "vite-ssr/vue";
99+
import { QueryClient, hydrate, dehydrate } from "vue-query";
100+
101+
export default viteSSR(
102+
App,
103+
{
104+
routes: [
105+
/* ... */
106+
],
107+
transformState(state, defaultTransformer) {
108+
if (import.meta.env.SSR) {
109+
// Dehydrate client cache in server
110+
state.vueQuery = dehydrate(state.vueQuery);
111+
}
112+
113+
return defaultTransformer(state);
114+
},
115+
},
116+
// This is Vite SSR main hook, which is called once per request.
117+
// Use it to set up Vue Query Client:
118+
({ initialState }) => {
119+
// Create VueQuery client
120+
const client = new QueryClient();
121+
122+
// Hydrate existing state in browser
123+
if (!import.meta.env.SSR) hydrate(client, initialState.vueQuery);
124+
125+
// Save reference to the client
126+
initialState.vueQuery = client;
127+
}
128+
);
129+
```
130+
131+
After that, provide the client in the root component using the saved reference to make it available globally:
132+
133+
```html
134+
<!-- App.vue -->
135+
<template>
136+
<div>
137+
<RouterView />
138+
</div>
139+
</template>
140+
141+
<script>
142+
import { useContext } from "vite-ssr/vue";
143+
import { useQueryProvider } from "vue-query";
144+
145+
export default {
146+
setup() {
147+
const { initialState } = useContext();
148+
useQueryProvider(initialState.vueQuery);
149+
},
150+
};
151+
</script>
152+
```
153+
154+
Finally, call VueQuery from any component:
155+
156+
```html
157+
<!-- MyComponent.vue -->
158+
<template>
159+
<div>
160+
<button @click="refetch">Refetch</button>
161+
<p>{{ data }}</p>
162+
</div>
163+
</template>
164+
165+
<script>
166+
import { useQuery } from "vue-query";
167+
import { onServerPrefetch } from "vue";
168+
169+
export default {
170+
setup() {
171+
// This will be prefetched and sent from the server
172+
const { refetch, data, suspense } = useQuery("todos", getTodos);
173+
onServerPrefetch(suspense);
174+
175+
return {
176+
refetch,
177+
data,
178+
};
179+
},
180+
};
181+
</script>
182+
```
183+
91184
## Tips, Tricks and Caveats
92185
93186
### Only successful queries are included in dehydration

0 commit comments

Comments
 (0)