Skip to content

Commit 34a5364

Browse files
committed
fix(client): make hydration work properly (close #123)
1 parent 10d16f9 commit 34a5364

File tree

12 files changed

+73
-73
lines changed

12 files changed

+73
-73
lines changed

packages/@vuepress/bundler-vite/src/build/createBuild.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
22
import { build } from 'vite'
3-
import type { ServerEntry } from '@vuepress/client'
3+
import type { CreateVueAppFunction } from '@vuepress/client'
44
import type { App, Bundler } from '@vuepress/core'
55
import { chalk, fs, ora, withSpinner } from '@vuepress/utils'
66
import type { ViteBundlerOptions } from '../types'
@@ -51,13 +51,15 @@ export const createBuild = (
5151
) as OutputChunk
5252

5353
// load the compiled server bundle
54-
const { createServerApp } = require(app.dir.dest(
54+
const { createVueApp } = require(app.dir.dest(
5555
'.server',
5656
serverEntryChunk.fileName
57-
)) as ServerEntry
57+
)) as {
58+
createVueApp: CreateVueAppFunction
59+
}
5860

5961
// create vue ssr app
60-
const { app: vueApp, router: vueRouter } = await createServerApp()
62+
const { app: vueApp, router: vueRouter } = await createVueApp()
6163

6264
// pre-render pages to html files
6365
const spinner = ora()

packages/@vuepress/bundler-vite/src/build/resolveViteConfig.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export const resolveViteConfig = ({
2222
cssCodeSplit: false,
2323
polyfillDynamicImport: false,
2424
rollupOptions: {
25-
input: isServer
26-
? app.dir.client('lib/server.js')
27-
: app.dir.client('lib/client.js'),
25+
input: app.dir.client('lib/app.js'),
2826
preserveEntrySignatures: 'allow-extension',
2927
},
3028
minify: isServer ? false : !app.env.isDebug,

packages/@vuepress/bundler-vite/src/plugin/createPlugin.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ export const createPlugin = ({
4747
if (req.url!.endsWith('.html')) {
4848
res.statusCode = 200
4949
const template = fs.readFileSync(app.options.templateDev).toString()
50+
51+
// here we use `lib/index.js` instead of `lib/app.js` as the client entry to
52+
// ensure all client files are loaded correctly (might be an issue of vite)
5053
const clientEntrySrc = `/@fs/${removeLeadingSlash(
51-
app.dir.client('lib/client.js')
54+
app.dir.client('lib/index.js')
5255
)}`
56+
5357
res.end(
5458
template.replace(
5559
/<\/body>/,

packages/@vuepress/bundler-webpack/src/build/createBuild.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as webpack from 'webpack'
2-
import type { ServerEntry } from '@vuepress/client'
2+
import type { CreateVueAppFunction } from '@vuepress/client'
33
import type { App, Bundler } from '@vuepress/core'
44
import { chalk, fs, ora, withSpinner } from '@vuepress/utils'
55
import type { WebpackBundlerOptions } from '../types'
@@ -73,12 +73,12 @@ export const createBuild = (
7373
} = resolveClientManifestMeta(clientManifest)
7474

7575
// load the compiled server bundle
76-
const { createServerApp } = require(app.dir.dest(
77-
'.server/app'
78-
)) as ServerEntry
76+
const { createVueApp } = require(app.dir.dest('.server/app')) as {
77+
createVueApp: CreateVueAppFunction
78+
}
7979

8080
// create vue ssr app
81-
const { app: vueApp, router: vueRouter } = await createServerApp()
81+
const { app: vueApp, router: vueRouter } = await createVueApp()
8282

8383
// pre-render pages to html files
8484
const spinner = ora()

packages/@vuepress/bundler-webpack/src/build/createServerConfig.ts

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ export const createServerConfig = (
1717
isBuild,
1818
})
1919

20-
// server entry
21-
config.entry('app').add(app.dir.client('lib/server.js'))
22-
2320
// server output
2421
// remove after pages rendered
2522
config.output

packages/@vuepress/bundler-webpack/src/config/createBaseConfig.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Config from 'webpack-chain'
22
import type { App } from '@vuepress/core'
33
import type { WebpackBundlerOptions } from '../types'
44
import { handleDevtool } from './handleDevtool'
5+
import { handleEntry } from './handleEntry'
56
import { handleMode } from './handleMode'
67
import { handleModule } from './handleModule'
78
import { handleNode } from './handleNode'
@@ -23,6 +24,11 @@ export const createBaseConfig = ({
2324
// create new webpack-chain config
2425
const config = new Config()
2526

27+
/**
28+
* entry
29+
*/
30+
handleEntry({ app, config })
31+
2632
/**
2733
* mode
2834
*/

packages/@vuepress/bundler-webpack/src/config/createClientBaseConfig.ts

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ export const createClientBaseConfig = ({
1919
isBuild,
2020
})
2121

22-
// client entry
23-
config.entry('app').add(app.dir.client('lib/client.js'))
24-
2522
// client output
2623
config.output
2724
.path(app.dir.dest())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type * as Config from 'webpack-chain'
2+
import type { App } from '@vuepress/core'
3+
4+
/**
5+
* Set webpack entry
6+
*/
7+
export const handleEntry = ({
8+
app,
9+
config,
10+
}: {
11+
app: App
12+
config: Config
13+
}): void => {
14+
// set client app as entry point
15+
config.entry('app').add(app.dir.client('lib/app.js'))
16+
}

packages/@vuepress/client/src/app.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
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'
511
import { removeEndingSlash } from '@vuepress/shared'
612
import { clientAppEnhances } from '@internal/clientAppEnhances'
713
import { clientAppRootComponents } from '@internal/clientAppRootComponents'
@@ -29,26 +35,24 @@ import {
2935
import { Content, OutboundLink } from './components'
3036
import { withBase } from './utils'
3137

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
3843

3944
/**
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
4447
*/
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 () => {
5256
// options to create vue app
5357
const appOptions: ComponentOptions = {
5458
setup() {
@@ -197,3 +201,12 @@ export const createVueApp = async ({
197201
router,
198202
}
199203
}
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+
}

packages/@vuepress/client/src/client.ts

-15
This file was deleted.

packages/@vuepress/client/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ export * from './types'
55
export * from './utils'
66

77
export type { PageHeader } from '@vuepress/shared'
8-
export type { ServerEntry } from './server'

packages/@vuepress/client/src/server.ts

-17
This file was deleted.

0 commit comments

Comments
 (0)