-
Notifications
You must be signed in to change notification settings - Fork 91
/
entry-server.js
88 lines (72 loc) · 2.16 KB
/
entry-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { createSSRApp } from 'vue'
import { renderToString } from '@vue/server-renderer'
import { createRouter, createMemoryHistory } from 'vue-router'
import { createUrl, getFullPath, withoutSuffix } from '../utils/route'
import { findDependencies, renderPreloadLinks } from '../utils/html'
import { addPagePropsGetterToRoutes } from './utils'
import { renderHeadToString } from '@vueuse/head'
export { ClientOnly } from './components.js'
export default function (
App,
{ routes, base, pageProps = { passToPage: true } },
hook
) {
if (pageProps && pageProps.passToPage) {
addPagePropsGetterToRoutes(routes)
}
return async function (url, { manifest, preload = false, ...extra } = {}) {
const app = createSSRApp(App)
url = createUrl(url)
const routeBase = base && withoutSuffix(base({ url }), '/')
const router = createRouter({
history: createMemoryHistory(routeBase),
routes,
})
app.use(router)
// This can be injected with useSSRContext() in setup functions
const context = {
url,
isClient: false,
initialState: {},
...extra,
}
const fullPath = getFullPath(url, routeBase)
const { head } =
(hook &&
(await hook({
app,
router,
initialRoute: router.resolve(fullPath),
...context,
}))) ||
{}
router.push(fullPath)
await router.isReady()
Object.assign(
context.initialState || {},
router.currentRoute.value.meta.state || {}
)
const body = await renderToString(app, context)
let { headTags = '', htmlAttrs = '', bodyAttrs = '' } = head
? renderHeadToString(head)
: {}
const dependencies = manifest
? findDependencies(context.modules, manifest)
: []
if (preload && dependencies.length > 0) {
headTags += renderPreloadLinks(dependencies)
}
const initialState = JSON.stringify(context.initialState || {})
return {
// This string is replaced at build time
// and injects all the previous variables.
html: `__VITE_SSR_HTML__`,
htmlAttrs,
headTags,
body,
bodyAttrs,
initialState,
dependencies,
}
}
}