Skip to content

Commit 83e626a

Browse files
committed
fix(nuxt): global pinia instance leads to whole nuxt context not being garbage collected
1 parent 140f7be commit 83e626a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createPinia, setActivePinia } from 'pinia'
22
import type { Pinia } from 'pinia'
3-
import { defineNuxtPlugin, type Plugin } from '#app'
4-
import { toRaw } from 'vue'
3+
import { defineNuxtPlugin, useNuxtApp, type Plugin } from '#app'
54

65
const plugin: Plugin<{ pinia: Pinia }> = defineNuxtPlugin({
76
name: 'pinia',
@@ -10,9 +9,7 @@ const plugin: Plugin<{ pinia: Pinia }> = defineNuxtPlugin({
109
nuxtApp.vueApp.use(pinia)
1110
setActivePinia(pinia)
1211

13-
if (import.meta.server) {
14-
nuxtApp.payload.pinia = toRaw(pinia.state.value)
15-
} else if (nuxtApp.payload && nuxtApp.payload.pinia) {
12+
if (nuxtApp.payload && nuxtApp.payload.pinia) {
1613
pinia.state.value = nuxtApp.payload.pinia as any
1714
}
1815

@@ -23,6 +20,13 @@ const plugin: Plugin<{ pinia: Pinia }> = defineNuxtPlugin({
2320
},
2421
}
2522
},
23+
hooks: {
24+
'app:rendered'() {
25+
const nuxtApp = useNuxtApp()
26+
nuxtApp.payload.pinia = (nuxtApp.$pinia as Pinia).state.value
27+
setActivePinia(undefined)
28+
},
29+
},
2630
})
2731

2832
export default plugin

0 commit comments

Comments
 (0)