Skip to content

Commit 5e016dd

Browse files
fix(vitrify): fix pinia colada plugin
1 parent 95dbb05 commit 5e016dd

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

.changeset/petite-pets-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vitrify': patch
3+
---
4+
5+
fix(vitrify): fix pinia colada plugin

packages/vitrify/src/node/plugins/pinia/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type PiniaPluginOptions = {
66
colada?: boolean
77
}
88

9-
const piniaonAppCreated: onAppCreatedHook = async ({
9+
const piniaOnAppCreated: onAppCreatedHook = async ({
1010
app,
1111
ctx,
1212
initialState,
@@ -23,7 +23,7 @@ const piniaonAppCreated: onAppCreatedHook = async ({
2323
if (ssrContext) ssrContext.pinia = pinia
2424
}
2525

26-
const piniaOnRenderedHook: OnRenderedHook = ({ app, ssrContext }) => {
26+
const piniaOnRenderedHook: OnRenderedHook = async ({ app, ssrContext }) => {
2727
// SSR Server
2828
if (ssrContext?.initialState && ssrContext.pinia) {
2929
ssrContext.initialState.pinia = ssrContext.pinia.state.value
@@ -35,16 +35,19 @@ const piniaColadaonAppCreated: onAppCreatedHook = async ({
3535
ctx,
3636
initialState
3737
}) => {
38-
if (ctx?.pinia) {
39-
const { PiniaColada, useQueryCache, hydrateQueryCache } = await import(
38+
if (ctx.pinia) {
39+
const { PiniaColada, hydrateQueryCache, useQueryCache } = await import(
4040
'@pinia/colada'
4141
)
42-
4342
app.use(PiniaColada)
4443

45-
// SSR Client
4644
if (initialState?.piniaColada) {
47-
hydrateQueryCache(useQueryCache(ctx.pinia), initialState.piniaColada)
45+
app.runWithContext(() =>
46+
hydrateQueryCache(
47+
useQueryCache(ctx.pinia),
48+
initialState.piniaColada || {}
49+
)
50+
)
4851
}
4952
}
5053
}
@@ -61,8 +64,8 @@ const piniaColadaOnRenderedHook: OnRenderedHook = async ({
6164
if (ssrContext.initialState.pinia?._pc_query) {
6265
delete ssrContext.initialState.pinia._pc_query
6366
}
64-
ssrContext.initialState.piniaColada = serializeQueryCache(
65-
useQueryCache(ssrContext.pinia)
67+
ssrContext.initialState.piniaColada = app.runWithContext(() =>
68+
serializeQueryCache(useQueryCache())
6669
)
6770
}
6871
}
@@ -72,7 +75,7 @@ export const PiniaPlugin: VitrifyPlugin<PiniaPluginOptions> = async ({
7275
pwa = false,
7376
options = {}
7477
}) => {
75-
const onAppCreated = [piniaonAppCreated]
78+
const onAppCreated = [piniaOnAppCreated]
7679
const onRendered = [piniaOnRenderedHook]
7780
if (options.colada) {
7881
onAppCreated.push(piniaColadaonAppCreated)

0 commit comments

Comments
 (0)