Skip to content

Commit

Permalink
feat(nuxt): add buildModule
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 8, 2021
1 parent b3db04a commit b1566f7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions nuxt/plugin.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// @ts-check
import Vue from 'vue'
// @ts-ignore: this must be pinia to load the local module
import { setActiveReq, setStateProvider, getRootState } from 'pinia'
import { setActivePinia, PiniaPlugin, createPinia } from 'pinia'

Vue.use(PiniaPlugin)

/** @type {import('@nuxt/types').Plugin} */
const myPlugin = (context) => {
// console.log('🍍 Pinia Nuxt plugin installed')
const myPlugin = (context, inject) => {
// console.log(context)

const pinia = createPinia()
context.app.pinia = pinia
setActivePinia(pinia)

inject('pinia', pinia)

// TODO: figure this out
if (process.server) {
setActiveReq(context.req)
context.beforeNuxtRender(({ nuxtState }) => {
nuxtState.pinia = getRootState(context.req)
nuxtState.pinia = pinia.state.value
})
} else if (context.nuxtState && context.nuxtState.pinia) {
setStateProvider(() => context.nuxtState.pinia)
pinia.state.value = context.nuxtState.pinia
}
}

Expand Down

0 comments on commit b1566f7

Please sign in to comment.