Skip to content

Commit 5ec2a7b

Browse files
committed
handle new component-lifecycle-based style injection strategy
1 parent ac219b3 commit 5ec2a7b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/server/bundle-renderer/create-bundle-runner.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isObject } from 'shared/util'
2+
13
const vm = require('vm')
24
const path = require('path')
35
const resolve = require('resolve')
@@ -70,6 +72,20 @@ function compileModule (files, basedir) {
7072
return evaluateModule
7173
}
7274

75+
function deepClone (val) {
76+
if (isObject(val)) {
77+
const res = {}
78+
for (const key in val) {
79+
res[key] = deepClone(val[key])
80+
}
81+
return res
82+
} else if (Array.isArray(val)) {
83+
return val.slice()
84+
} else {
85+
return val
86+
}
87+
}
88+
7389
export function createBundleRunner (entry, files, basedir, direct) {
7490
const evaluate = compileModule(files, basedir)
7591
if (!direct) {
@@ -87,13 +103,18 @@ export function createBundleRunner (entry, files, basedir, direct) {
87103
// each render, it simply calls the exported function. This avoids the
88104
// module evaluation costs but requires the source code to be structured
89105
// slightly differently.
90-
const context = createContext()
106+
const initialExposedContext = {}
107+
const context = createContext(initialExposedContext)
91108
const runner = evaluate(entry, context, {})
92109
if (typeof runner !== 'function') {
93110
throw new Error('direct mode expects bundle export to be a function.')
94111
}
95112
return (_context = {}) => {
96113
context.__VUE_SSR_CONTEXT__ = _context
114+
// vue-style-loader styles imported outside of component lifecycle hooks
115+
if (initialExposedContext._styles) {
116+
_context._styles = deepClone(initialExposedContext._styles)
117+
}
97118
return runner(_context)
98119
}
99120
}

src/server/render.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,26 @@ function renderNode (node, isRoot, context) {
4343
const Ctor = node.componentOptions.Ctor
4444
const getKey = Ctor.options.serverCacheKey
4545
const name = Ctor.options.name
46+
const injectStyles = Ctor.options._injectStyles
4647
const cache = context.cache
4748
if (isDef(getKey) && isDef(cache) && isDef(name)) {
4849
const key = name + '::' + getKey(node.componentOptions.propsData)
4950
const { has, get } = context
5051
if (isDef(has)) {
5152
(has: any)(key, hit => {
5253
if (hit === true && isDef(get)) {
53-
(get: any)(key, res => write(res, next))
54+
(get: any)(key, res => {
55+
injectStyles && injectStyles.call({})
56+
write(res, next)
57+
})
5458
} else {
5559
renderComponentWithCache(node, isRoot, key, context)
5660
}
5761
})
5862
} else if (isDef(get)) {
5963
(get: any)(key, res => {
6064
if (isDef(res)) {
65+
injectStyles && injectStyles.call({})
6166
write(res, next)
6267
} else {
6368
renderComponentWithCache(node, isRoot, key, context)

0 commit comments

Comments
 (0)