Skip to content

Commit 2a1f79d

Browse files
committed
run bundle in the same global context when runInNewContext is false (fix vuejs#5559)
1 parent 9478fde commit 2a1f79d

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function createContext (context) {
2222
return sandbox
2323
}
2424

25-
function compileModule (files, basedir) {
25+
function compileModule (files, basedir, runInNewContext) {
2626
const compiledScripts = {}
2727
const resolvedModules = {}
2828

@@ -46,7 +46,9 @@ function compileModule (files, basedir) {
4646
}
4747

4848
const script = getCompiledScript(filename)
49-
const compiledWrapper = script.runInNewContext(context)
49+
const compiledWrapper = runInNewContext
50+
? script.runInNewContext(context)
51+
: script.runInThisContext()
5052
const m = { exports: {}}
5153
const r = file => {
5254
file = path.join('.', file)
@@ -87,7 +89,7 @@ function deepClone (val) {
8789
}
8890

8991
export function createBundleRunner (entry, files, basedir, runInNewContext) {
90-
const evaluate = compileModule(files, basedir)
92+
const evaluate = compileModule(files, basedir, runInNewContext)
9193
if (runInNewContext) {
9294
// new context mode: creates a fresh context and re-evaluate the bundle
9395
// on each render. Ensures entire application state is fresh for each
@@ -102,19 +104,17 @@ export function createBundleRunner (entry, files, basedir, runInNewContext) {
102104
// each render, it simply calls the exported function. This avoids the
103105
// module evaluation costs but requires the source code to be structured
104106
// slightly differently.
105-
106-
// the initial context is only used for collecting possible non-component
107-
// styles injected by vue-style-loader.
108-
const initialContext = {}
109-
const sharedContext = createContext(initialContext)
110-
111107
let runner // lazy creation so that errors can be caught by user
108+
let initialContext
112109
return (userContext = {}) => new Promise(resolve => {
113110
if (!runner) {
114-
runner = evaluate(entry, sharedContext)
111+
// the initial context is only used for collecting possible non-component
112+
// styles injected by vue-style-loader.
113+
initialContext = global.__VUE_SSR_CONTEXT__ = {}
114+
runner = evaluate(entry)
115115
// On subsequent renders, __VUE_SSR_CONTEXT__ will not be avaialbe
116116
// to prevent cross-request pollution.
117-
delete sharedContext.__VUE_SSR_CONTEXT__
117+
delete global.__VUE_SSR_CONTEXT__
118118
if (typeof runner !== 'function') {
119119
throw new Error(
120120
'bundle export should be a function when using ' +

0 commit comments

Comments
 (0)