|
1 |
| -import { computed, h } from 'vue' |
2 |
| -import type { CreateAppFunction, App, ComponentOptions } from 'vue' |
3 |
| -import { createRouter, RouterView, START_LOCATION } from 'vue-router' |
4 |
| -import type { Router, RouterHistory } from 'vue-router' |
| 1 | +import { createApp, createSSRApp, computed, h } from 'vue' |
| 2 | +import type { App, ComponentOptions } from 'vue' |
| 3 | +import { |
| 4 | + createRouter, |
| 5 | + createWebHistory, |
| 6 | + createMemoryHistory, |
| 7 | + RouterView, |
| 8 | + START_LOCATION, |
| 9 | +} from 'vue-router' |
| 10 | +import type { Router } from 'vue-router' |
5 | 11 | import { removeEndingSlash } from '@vuepress/shared'
|
6 | 12 | import { clientAppEnhances } from '@internal/clientAppEnhances'
|
7 | 13 | import { clientAppRootComponents } from '@internal/clientAppRootComponents'
|
@@ -29,26 +35,24 @@ import {
|
29 | 35 | import { Content, OutboundLink } from './components'
|
30 | 36 | import { withBase } from './utils'
|
31 | 37 |
|
32 |
| -export type AppCreator = CreateAppFunction<Element> |
33 |
| -export type HistoryCreator = (base?: string) => RouterHistory |
34 |
| -export type CreateVueAppResult = { |
35 |
| - app: App |
36 |
| - router: Router |
37 |
| -} |
| 38 | +/** |
| 39 | + * - use `createApp` in dev mode |
| 40 | + * - use `createSSRApp` in build mode |
| 41 | + */ |
| 42 | +const appCreator = __DEV__ ? createApp : createSSRApp |
38 | 43 |
|
39 | 44 | /**
|
40 |
| - * Create a vue app |
41 |
| - * |
42 |
| - * Accepting different app creator and history creator, so it |
43 |
| - * can be reused for both client side and server side |
| 45 | + * - use `createWebHistory` in dev mode and build mode client bundle |
| 46 | + * - use `createMemoryHistory` in build mode server bundle |
44 | 47 | */
|
45 |
| -export const createVueApp = async ({ |
46 |
| - appCreator, |
47 |
| - historyCreator, |
48 |
| -}: { |
49 |
| - appCreator: AppCreator |
50 |
| - historyCreator: HistoryCreator |
51 |
| -}): Promise<CreateVueAppResult> => { |
| 48 | +const historyCreator = __SSR__ ? createMemoryHistory : createWebHistory |
| 49 | + |
| 50 | +export type CreateVueAppFunction = () => Promise<{ |
| 51 | + app: App |
| 52 | + router: Router |
| 53 | +}> |
| 54 | + |
| 55 | +export const createVueApp: CreateVueAppFunction = async () => { |
52 | 56 | // options to create vue app
|
53 | 57 | const appOptions: ComponentOptions = {
|
54 | 58 | setup() {
|
@@ -197,3 +201,12 @@ export const createVueApp = async ({
|
197 | 201 | router,
|
198 | 202 | }
|
199 | 203 | }
|
| 204 | + |
| 205 | +// mount app in client bundle |
| 206 | +if (!__SSR__) { |
| 207 | + createVueApp().then(({ app, router }) => { |
| 208 | + router.isReady().then(() => { |
| 209 | + app.mount('#app') |
| 210 | + }) |
| 211 | + }) |
| 212 | +} |
0 commit comments