Skip to content

Commit ea0a2b2

Browse files
committed
perf: reduce memory usage during rendering
1 parent 406fa67 commit ea0a2b2

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

packages/bundler-vite/src/build/build.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,19 @@ export const build = async (
6868
const { createVueApp } = await importFile<{
6969
createVueApp: CreateVueAppFunction
7070
}>(serverEntryPath)
71+
// create vue ssr app
72+
const { app: vueApp, router: vueRouter } = await createVueApp()
73+
const { renderToString } = await import('vue/server-renderer')
7174

7275
// pre-render pages to html files
7376
for (const page of app.pages) {
74-
if (spinner) {
75-
spinner.text = `Rendering pages ${chalk.magenta(page.path)}`
76-
}
77+
if (spinner) spinner.text = `Rendering pages ${chalk.magenta(page.path)}`
7778
await renderPage({
7879
app,
7980
page,
80-
createVueApp,
81+
vueApp,
82+
vueRouter,
83+
renderToString,
8184
ssrTemplate,
8285
output: clientOutput.output,
8386
outputEntryChunk: clientEntryChunk,

packages/bundler-vite/src/build/renderPage.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { CreateVueAppFunction } from '@vuepress/client'
21
import type { App, Page } from '@vuepress/core'
32
import { fs, renderHead } from '@vuepress/utils'
43
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
4+
import { ssrContextKey } from 'vue'
5+
import type { App as VueApp } from 'vue'
6+
import type { Router } from 'vue-router'
57
import type { SSRContext } from 'vue/server-renderer'
68
import { renderPagePrefetchLinks } from './renderPagePrefetchLinks.js'
79
import { renderPagePreloadLinks } from './renderPagePreloadLinks.js'
@@ -12,35 +14,36 @@ import { resolvePageChunkFiles } from './resolvePageChunkFiles.js'
1214
export const renderPage = async ({
1315
app,
1416
page,
15-
createVueApp,
17+
vueApp,
18+
vueRouter,
19+
renderToString,
1620
ssrTemplate,
1721
output,
1822
outputEntryChunk,
1923
outputCssAsset,
2024
}: {
2125
app: App
2226
page: Page
23-
createVueApp: CreateVueAppFunction
27+
vueApp: VueApp
28+
vueRouter: Router
29+
renderToString: (input: VueApp, context: SSRContext) => Promise<string>
2430
ssrTemplate: string
2531
output: RollupOutput['output']
2632
outputEntryChunk: OutputChunk
2733
outputCssAsset: OutputAsset | undefined
2834
}): Promise<void> => {
29-
// create vue ssr app
30-
const { app: vueApp, router: vueRouter } = await createVueApp()
31-
3235
// switch to current page route
3336
await vueRouter.push(page.path)
3437
await vueRouter.isReady()
3538

3639
// create vue ssr context with default values
40+
delete vueApp._context.provides[ssrContextKey]
3741
const ssrContext: SSRContext = {
3842
lang: 'en',
3943
head: [],
4044
}
4145

4246
// render current page to string
43-
const { renderToString } = await import('vue/server-renderer')
4447
const pageRendered = await renderToString(vueApp, ssrContext)
4548

4649
// resolve page chunks

packages/bundler-webpack/src/build/build.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export const build = async (
9191
const { createVueApp } = await importFileDefault<{
9292
createVueApp: CreateVueAppFunction
9393
}>(serverEntryPath)
94+
// create vue ssr app
95+
const { app: vueApp, router: vueRouter } = await createVueApp()
96+
const { renderToString } = await import('vue/server-renderer')
9497

9598
// pre-render pages to html files
9699
for (const page of app.pages) {
@@ -100,7 +103,9 @@ export const build = async (
100103
await renderPage({
101104
app,
102105
page,
103-
createVueApp,
106+
vueApp,
107+
vueRouter,
108+
renderToString,
104109
ssrTemplate,
105110
allFilesMeta,
106111
initialFilesMeta,

packages/bundler-webpack/src/build/renderPage.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { CreateVueAppFunction } from '@vuepress/client'
21
import type { App, Page } from '@vuepress/core'
32
import type { VuepressSSRContext } from '@vuepress/shared'
43
import { fs, renderHead } from '@vuepress/utils'
4+
import { ssrContextKey } from 'vue'
5+
import type { App as VueApp } from 'vue'
6+
import type { Router } from 'vue-router'
57
import type { SSRContext } from 'vue/server-renderer'
68
import { renderPagePrefetchLinks } from './renderPagePrefetchLinks.js'
79
import { renderPagePreloadLinks } from './renderPagePreloadLinks.js'
@@ -25,7 +27,9 @@ interface PageRenderContext extends SSRContext, VuepressSSRContext {
2527
export const renderPage = async ({
2628
app,
2729
page,
28-
createVueApp,
30+
vueApp,
31+
vueRouter,
32+
renderToString,
2933
ssrTemplate,
3034
allFilesMeta,
3135
initialFilesMeta,
@@ -34,29 +38,28 @@ export const renderPage = async ({
3438
}: {
3539
app: App
3640
page: Page
37-
createVueApp: CreateVueAppFunction
41+
vueApp: VueApp
42+
vueRouter: Router
43+
renderToString: (input: VueApp, context: SSRContext) => Promise<string>
3844
ssrTemplate: string
3945
allFilesMeta: FileMeta[]
4046
initialFilesMeta: FileMeta[]
4147
asyncFilesMeta: FileMeta[]
4248
moduleFilesMetaMap: ModuleFilesMetaMap
4349
}): Promise<void> => {
44-
// create vue ssr app
45-
const { app: vueApp, router: vueRouter } = await createVueApp()
46-
4750
// switch to current page route
4851
await vueRouter.push(page.path)
4952
await vueRouter.isReady()
5053

5154
// create vue ssr context with default values
55+
delete vueApp._context.provides[ssrContextKey]
5256
const ssrContext: PageRenderContext = {
5357
_registeredComponents: new Set(),
5458
lang: 'en',
5559
head: [],
5660
}
5761

5862
// render current page to string
59-
const { renderToString } = await import('vue/server-renderer')
6063
const pageRendered = await renderToString(vueApp, ssrContext)
6164

6265
// resolve client files that used by this page

0 commit comments

Comments
 (0)