Skip to content

Commit

Permalink
fix: simplify vike-vue-{query,pinia} integration
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Sep 12, 2024
1 parent d85d957 commit 08b612f
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 24 deletions.
6 changes: 2 additions & 4 deletions packages/vike-vue-pinia/integration/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config = {
require: {
'vike-vue': '>=0.7.0',
},
passToClient: ['_piniaInitialState'],
onCreateApp: 'import:vike-vue-pinia/__internal/integration/onCreateApp:onCreateApp',
onAfterRenderHtml: 'import:vike-vue-pinia/__internal/integration/onAfterRenderHtml:onAfterRenderHtml',
onBeforeRenderClient: 'import:vike-vue-pinia/__internal/integration/onBeforeRenderClient:onBeforeRenderClient',
Expand All @@ -18,10 +19,7 @@ declare global {
namespace Vike {
interface PageContext {
pinia?: Pinia
// @ts-expect-error TODO: fix type mismatch
fromHtmlRenderer: {
piniaInitialState?: StateTree
}
_piniaInitialState?: StateTree
}
}
}
4 changes: 2 additions & 2 deletions packages/vike-vue-pinia/integration/onAfterRenderHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export { onAfterRenderHtml }
import type { OnAfterRenderHtmlSync } from 'vike-vue/types'

const onAfterRenderHtml: OnAfterRenderHtmlSync = (pageContext): ReturnType<OnAfterRenderHtmlSync> => {
return dehydratePinia(pageContext)
dehydratePinia(pageContext)
}

type PageContext = Parameters<typeof onAfterRenderHtml>[0]
function dehydratePinia(pageContext: PageContext) {
const { pinia } = pageContext
return { piniaInitialState: pinia!.state.value }
pageContext._piniaInitialState = pinia!.state.value
}
7 changes: 2 additions & 5 deletions packages/vike-vue-pinia/integration/onBeforeRenderClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ function hydratePinia(pageContext: PageContext) {
if (!pageContext.isHydration) {
return
}
const {
pinia,
fromHtmlRenderer: { piniaInitialState },
} = pageContext
const { pinia } = pageContext
const piniaInitialState = pageContext._piniaInitialState
if (!pinia || !piniaInitialState) {
// happens if SSR is off
return
}
// @ts-expect-error TODO: fix type mismatch between Config['fromHtmlRenderer'] set by vike-vue and vike-vue-pinia
pinia.state.value = piniaInitialState
}
5 changes: 1 addition & 4 deletions packages/vike-vue-query/integration/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ declare global {
namespace Vike {
interface PageContext {
queryClient?: QueryClient
// @ts-expect-error TODO: fix type mismatch
fromHtmlRenderer: {
vueQueryInitialState?: DehydratedState
}
_vueQueryInitialState?: DehydratedState
}
}
namespace Vike {
Expand Down
4 changes: 2 additions & 2 deletions packages/vike-vue-query/integration/dehydrateVueQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import type { OnAfterRenderHtmlSync } from 'vike-vue/types'

export { dehydrateVueQuery }

const dehydrateVueQuery: OnAfterRenderHtmlSync = ({ queryClient }): ReturnType<OnAfterRenderHtmlSync> => {
return { vueQueryInitialState: dehydrate(queryClient!) }
const dehydrateVueQuery: OnAfterRenderHtmlSync = (pageContext): ReturnType<OnAfterRenderHtmlSync> => {
pageContext._vueQueryInitialState = dehydrate(pageContext.queryClient!)
}
2 changes: 1 addition & 1 deletion packages/vike-vue-query/integration/hydrateVueQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const hydrateVueQuery: OnBeforeRenderClientSync = (pageContext): ReturnType<OnBe
if (!pageContext.isHydration) {
return
}
const { vueQueryInitialState } = pageContext.fromHtmlRenderer
const vueQueryInitialState = pageContext._vueQueryInitialState
const queryClient = 'queryClient' in pageContext ? pageContext.queryClient : null
if (!queryClient || !vueQueryInitialState) {
// happens if SSR is off
Expand Down
1 change: 0 additions & 1 deletion packages/vike-vue/src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ declare global {

/**
* Hook called right after rendering the page's root Vue component to HTML.
* The hook can return additional page context that will be passed to the client under `pageContext.fromHtmlRenderer`.
*
* https://vike.dev/onAfterRenderHtml
*/
Expand Down
4 changes: 3 additions & 1 deletion packages/vike-vue/src/types/PageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ declare global {
// Set by createVueApp()
app?: App

// Set by onRenderHtml()
// TODO/eventually: remove (vike-vue-pinia and vike-vue-query don't use this anymore).
// - Let's wait a bit before removing as old vike-vue-pinia and vike-vue-query versions still need this.
/** @deprecated */
fromHtmlRenderer: Record<string, unknown>

ssrContext?: SSRContext
Expand Down
6 changes: 2 additions & 4 deletions packages/vike-vue/src/types/VikeHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@ type OnBeforeRenderHtmlAsync = (pageContext: PageContextServer) => Promise<void>

/**
* Hook called right after rendering the page's root Vue component to HTML.
* The hook can return additional page context that will be passed to the client under `pageContext.fromHtmlRenderer`.
*
* https://vike.dev/onAfterRenderHtml
*/
type OnAfterRenderHtmlSync = (pageContext: PageContextServer) => PageContextServer['fromHtmlRenderer']
type OnAfterRenderHtmlSync = (pageContext: PageContextServer) => void | PageContextServer['fromHtmlRenderer']
/**
* Hook called right after rendering the page's root Vue component to HTML.
* The hook can return additional page context that will be passed to the client under `pageContext.fromHtmlRenderer`.
*
* https://vike.dev/onAfterRenderHtml
*/
type OnAfterRenderHtmlAsync = (pageContext: PageContextServer) => Promise<PageContextServer['fromHtmlRenderer']>
type OnAfterRenderHtmlAsync = (pageContext: PageContextServer) => Promise<void | PageContextServer['fromHtmlRenderer']>

/**
* Hook called right before mounting the page's root Vue component or when navigating on the client.
Expand Down

0 comments on commit 08b612f

Please sign in to comment.