-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (23 loc) · 895 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { createSSRApp, defineComponent, h, computed, reactive } from 'vue';
import { renderToString } from 'vue/server-renderer';
const store = {
// initial state could be hydrated
state: reactive({ items: null }),
// pretend to fetch some data from an api
async fetchData() {
this.state.items = ['hello', 'world'];
}
}
const App = defineComponent(async () => {
const msg = computed(() => store.state.items?.join(' '));
// If msg value is falsy then we are either in ssr context or on the client
// and the initial state was not modified/hydrated.
// In both cases we need to fetch data.
if (!msg.value)
await store.fetchData();
return () => h('div', null, msg.value);
})
const app = createSSRApp(App);
const html = await renderToString(app);
// in real world serve this html and append store state for hydration on client
console.log(html);