export default defineComponent({
async serverPrefetch() {
const store = useStore()
- store.$state.counter++
+ store.counter++
},
setup() {
const doubleCount = computed(() => store.$state.counter * 2)
function increment() {
- store.$state.counter++
+ store.counter++
}
return {
-import Vue from 'vue'
import { createApp } from './main'
-import { PiniaSsr, getRootState } from '../../../src'
-
-Vue.use(PiniaSsr)
export default function (context: any) {
return new Promise((resolve) => {
- const { app } = createApp()
+ const { app, pinia } = createApp()
// This `rendered` hook is called when the app has finished rendering
context.rendered = () => {
// When we attach the state to the context, and the `template` option
// is used for the renderer, the state will automatically be
// serialized and injected into the HTML as `window.__INITIAL_STATE__`.
- context.state = getRootState(context.req)
+ context.state = pinia.state.value
}
resolve(app)
+import { createPinia } from '../../../src'
import Vue from 'vue'
// import VueCompositionApi from '@vue/composition-api'
import App from './App'
export function createApp() {
// create the app instance, injecting both the router and the store
+ const pinia = createPinia()
+ Vue.use(pinia)
const app = new Vue({
// @ts-ignore
+ pinia,
render: (h) => h(App),
})
// expose the app, the router and the store.
- return { app }
+ return { app, pinia }
}
+++ /dev/null
-import Vue from 'vue'
-import { PiniaSsr } from '../../src'
-import { mockWarn } from 'jest-mock-warn'
-
-describe('SSR', () => {
- mockWarn()
-
- it('should warn when installed in the browser', () => {
- const mixinSpy = jest.spyOn(Vue, 'mixin')
- Vue.use(PiniaSsr)
- expect(/seems to be used in the client bundle/i).toHaveBeenWarned()
- expect(mixinSpy).not.toHaveBeenCalled()
- mixinSpy.mockRestore()
- })
-})
install(Vue) {
// localApp = app
this.Vue = Vue
+ Vue.prototype.$pinia = pinia
// Equivalent of
// app.config.globalProperties.$pinia = pinia
// Make pinia accessible everywhere through this.$pinia
// FIXME: typings
if (options.pinia) {
- ;(this as any).$pinia = options.pinia
// HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L25
- // TODO: check if necessary
- // if (!(this as any)._provided) {
- // const provideCache = {}
- // Object.defineProperty(this, '_provided', {
- // get: () => provideCache,
- // set: (v) => Object.assign(provideCache, v),
- // })
- // }
- // ;(this as any)._provided[piniaSymbol as any] = options.pinia
- } else if (options.parent && options.parent.$pinia) {
- ;(this as any).$pinia = options.parent.$pinia
+ if (!(this as any)._provided) {
+ const provideCache = {}
+ Object.defineProperty(this, '_provided', {
+ get: () => provideCache,
+ set: (v) => Object.assign(provideCache, v),
+ })
+ }
+ ;(this as any)._provided[piniaSymbol as any] = options.pinia
}
},
})
+++ /dev/null
-import { VueConstructor } from 'vue'
-// FIXME: migrate to setActivePinia
-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 client 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 TODO: fix usage with nuxt-composition-api https://github.com/posva/pinia/issues/179
- if (context.ssrContext) 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 to 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
- }
- },
- })
-}
Ref,
getCurrentInstance,
markRaw,
+ inject,
} from '@vue/composition-api'
import {
StateTree,
setActivePinia,
getActivePinia,
PiniaCustomProperties,
+ piniaSymbol,
} from './rootStore'
import Vue from 'vue'
const { id, state, getters, actions } = options
return function useStore(pinia?: Pinia | null): Store<Id, S, G, A> {
- const vm = getCurrentInstance()
- pinia = pinia || (vm && ((vm as any).$pinia as Pinia))
+ // const vm = getCurrentInstance()
+ // pinia = pinia || (vm && ((vm as any).$pinia as Pinia))
+ pinia = pinia || (getCurrentInstance() && inject(piniaSymbol))
if (pinia) setActivePinia(pinia)
pinia = getActivePinia()
+
let stores = storesMap.get(pinia)
if (!stores) storesMap.set(pinia, (stores = new Map()))