-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ssrPlugin.ts
51 lines (44 loc) · 1.58 KB
/
ssrPlugin.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { VueConstructor } from 'vue/types'
import { setActiveReq } from './rootStore'
import { SetupContext } from '@vue/composition-api'
export const PiniaSsr = (vue: VueConstructor) => {
const isServer = typeof window === 'undefined'
if (!isServer) {
console.warn(
'`PiniaSsrPlugin` seems to be used in the browser bundle. You should only call it on the server entry: https://github.com/posva/pinia#raw-vue-ssr'
)
return
}
vue.mixin({
beforeCreate() {
// @ts-ignore
const { setup, serverPrefetch } = this.$options
if (setup) {
// @ts-ignore
this.$options.setup = (props: any, context: SetupContext) => {
// @ts-ignore
setActiveReq(context.ssrContext.req)
return setup(props, context)
}
}
if (serverPrefetch) {
const patchedServerPrefetch = Array.isArray(serverPrefetch)
? serverPrefetch.slice()
: // serverPrefetch not being an array cannot be triggered due tue options merge
// https://github.com/vuejs/vue/blob/7912f75c5eb09e0aef3e4bfd8a3bb78cad7540d7/src/core/util/options.js#L149
/* istanbul ignore next */
[serverPrefetch]
for (let i = 0; i < patchedServerPrefetch.length; i++) {
const original = patchedServerPrefetch[i]
patchedServerPrefetch[i] = function() {
// @ts-ignore
setActiveReq(this.$ssrContext.req)
return original.call(this)
}
}
// @ts-ignore
this.$options.serverPrefetch = patchedServerPrefetch
}
},
})
}