Skip to content

Commit

Permalink
refactor(client): resolve root components outside of render function
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 3, 2024
1 parent e653bdc commit 2e4496a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion e2e/docs/.vuepress/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineClientConfig } from 'vuepress/client'
import RootComponentFromUserConfig from './components/RootComponentFromUserConfig.vue'

export default defineClientConfig({
//
rootComponents: [RootComponentFromUserConfig],
})
5 changes: 5 additions & 0 deletions e2e/docs/.vuepress/components/RootComponentFromUserConfig.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div class="root-component-from-user-config">
<p>root component from user config</p>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div class="root-component-from-theme">
<p>root component from theme</p>
</div>
</template>
3 changes: 3 additions & 0 deletions e2e/docs/.vuepress/theme/client/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineClientConfig } from 'vuepress/client'
import RootComponentFromTheme from './components/RootComponentFromTheme.vue'
import CustomLayout from './layouts/CustomLayout.vue'
import Layout from './layouts/Layout.vue'
import NotFound from './layouts/NotFound.vue'
Expand All @@ -19,4 +20,6 @@ export default defineClientConfig({
Layout,
NotFound,
},

rootComponents: [RootComponentFromTheme],
})
15 changes: 15 additions & 0 deletions e2e/tests/client-config/root-components.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
it('should render root components correctly', () => {
cy.visit('/')

cy.get('.root-component-from-theme').should('exist')
cy.get('.root-component-from-theme p').should(
'have.text',
'root component from theme',
)

cy.get('.root-component-from-user-config').should('exist')
cy.get('.root-component-from-user-config p').should(
'have.text',
'root component from user config',
)
})
14 changes: 8 additions & 6 deletions packages/client/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export const createVueApp: CreateVueAppFunction = async () => {
clientConfig.setup?.()
}

// get all root components
const rootComponents = clientConfigs.flatMap(
({ rootComponents = [] }) => rootComponents,
)

// get page layout
const layout = usePageLayout()

return () => [
h(layout.value),
...clientConfigs.flatMap(({ rootComponents = [] }) =>
rootComponents.map((component) => h(component)),
),
]
// render layout and root components
return () => [h(layout.value), rootComponents]
},
})

Expand Down

0 comments on commit 2e4496a

Please sign in to comment.