Skip to content

Commit

Permalink
fix: use createSSRApp to enable more efficient hydration
Browse files Browse the repository at this point in the history
This does not seem to be working correctly.

Running `pnpm docs:now`, you can verify that the DarkModeSwitch is not
re-rendered as expected when toggling the theme (always displays the sun)
  • Loading branch information
ElMassimo committed Jul 11, 2022
1 parent 677d27e commit 6c2f647
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 1 addition & 3 deletions packages/hydration/vue.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { h, createApp as createClientApp, createStaticVNode, createSSRApp } from 'vue'
import { h, createStaticVNode, createSSRApp as createVueApp } from 'vue'
import type { DefineComponent as Component, Component as App } from 'vue'
import type { Props, Slots } from './types'
import { onDispose } from './hydration'

const createVueApp = import.meta.env.SSR ? createSSRApp : createClientApp

// Internal: Creates a Vue app and mounts it on the specified island root.
export default function createVueIsland (component: Component, id: string, el: Element, props: Props, slots: Slots | undefined) {
const slotFns = slots && Object.fromEntries(Object.entries(slots).map(([slotName, content]) => {
Expand Down
13 changes: 10 additions & 3 deletions packages/iles/src/client/app/composables/vueRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { AppContext, Component, VNode, AsyncComponentLoader } from 'vue'
import { h, getCurrentInstance, createApp, createSSRApp, ssrContextKey, withCtx } from 'vue'
import { h, getCurrentInstance, createSSRApp, ssrContextKey, withCtx } from 'vue'

const newApp = import.meta.env.SSR ? createApp : createSSRApp
const newApp = createSSRApp

export type Nodes = undefined | VNode<any, any, any> | VNode<any, any, any>[]
export type VueRenderable = AsyncComponentLoader | Component | Nodes | ((props?: any) => Nodes | Promise<Nodes>)
export type VNodeRenderer = (content: VueRenderable) => Promise<string>

const commentStartRegex = /^(<!--\[-->|<!--]-->|<!---->)/g
const commentEndRegex = /(<!--\[-->|<!--]-->|<!---->)$/

export function useVueRenderer (): VNodeRenderer {
return withCtx((async (content) => {
if (!content) return ''
Expand All @@ -29,7 +32,11 @@ export function useVueRenderer (): VNodeRenderer {
Object.assign(proxyApp._context, { ...appContext, provides })

const { renderToString } = await import('@vue/server-renderer')
return await renderToString(proxyApp, ssrContext)
const str = await renderToString(proxyApp, ssrContext)

return import.meta.env.DEV
? str.replace(commentStartRegex, '').replace(commentEndRegex, '')
: str
}) as VNodeRenderer, getCurrentInstance()) as VNodeRenderer
}

Expand Down
2 changes: 1 addition & 1 deletion packages/iles/src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function renderPage (
let content = await renderToString(app, { islandsByPath, renderers })

// Remove comments from Vue renderer to allow plain text, RSS, or JSON output.
content = content.replace(commentsRegex, '')
// content = content.replace(commentsRegex, '')

// Skip HTML shell to allow Vue to render plain text, RSS, or JSON output.
if (!route.outputFilename.endsWith('.html'))
Expand Down

0 comments on commit 6c2f647

Please sign in to comment.