Skip to content

Commit

Permalink
fix(nuxt): plugin injection on latest Nuxt 3 context (#1433)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
brownsugar and posva committed Jul 11, 2022
1 parent 08fa257 commit bd0c52f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/nuxt/src/templates/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const PiniaNuxtPlugin: Plugin = (context, inject) => {
// make sure to inject pinia after installing the plugin because in Nuxt 3, inject defines a non configurable getter
// on app.config.globalProperties
// add $pinia to the context
inject('pinia', pinia)
if (isVue2) {
inject('pinia', pinia)
} else {
// @ts-expect-error: vue 3 only
context.provide('pinia', pinia)
}
// to allow accessing pinia without the $
// TODO: remove this in deprecation
context.pinia = pinia
Expand All @@ -43,10 +48,14 @@ const PiniaNuxtPlugin: Plugin = (context, inject) => {
})
} else {
// there is no beforeNuxtRender in Nuxt 3
context.nuxtState.pinia = pinia.state.value
// @ts-expect-error: vue 3 only
context.ssrContext.payload.pinia = pinia.state.value
}
} else {
const source = isVue2 ? context.nuxtState : context.payload
if (source && source.pinia) {
pinia.state.value = source.pinia
}
} else if (context.nuxtState && context.nuxtState.pinia) {
pinia.state.value = context.nuxtState.pinia
}
}

Expand Down

0 comments on commit bd0c52f

Please sign in to comment.