Skip to content

Commit bf7cbca

Browse files
authored
Merge pull request #30 from i5sing/hotfix/heap-out-of-memory
fix vue ssr heap out of memory
2 parents dbf6b06 + 17dcef8 commit bf7cbca

File tree

6 files changed

+25
-22
lines changed

6 files changed

+25
-22
lines changed

src/app/app.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import VueRouter from 'vue-router';
77
import store from './vuex';
88
import App from './app.vue';
99

10-
import router from './router/index';
10+
import { createRouter } from './router/index';
1111

1212
Vue.use(VueRouter);
1313
Vue.use(Carousel3d);
1414

15-
const app = new Vue({
16-
router,
17-
store,
18-
render: h => h(App)
19-
});
20-
21-
export {app, router, store};
15+
export const createApp = () => {
16+
const router = createRouter();
17+
const app = new Vue({
18+
router,
19+
store,
20+
render: h => h(App)
21+
});
22+
return { app, router, store };
23+
}

src/app/client-entry.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* Created by zhaofeng on 2016/12/2.
33
*/
4-
import {WOW} from 'wowjs';
4+
import { WOW } from 'wowjs';
55
import LazyLoad from 'vanilla-lazyload';
66
import "jquery";
7+
import { createApp } from './app';
78

8-
import {app, store} from './app'
9+
const { app, store } = createApp();
910

1011
//将服务端渲染时候的状态写入vuex
1112
if (window.__INITIAL_STATE__) {
@@ -17,4 +18,4 @@ app.$mount('#app');
1718

1819
new WOW().init();
1920

20-
window.lazyload = new LazyLoad();
21+
window.lazyload = new LazyLoad();

src/app/router/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
* Created by zhaofeng on 2016/12/2.
33
*/
44
import VueRouter from 'vue-router';
5-
65
import * as HomePage from '../page/home.vue';
76

87
/**
98
* Vue Router
109
*/
11-
export default new VueRouter({
10+
export const createRouter = () => new VueRouter({
1211
// mode: 'history',
1312
routes: [
1413
{path: '/', component: HomePage}
1514
]
16-
});
15+
});

src/app/server-entry.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
import { createApp } from "./app";
2+
13
/**
24
* Created by zhaofeng on 2016/12/2.
35
*/
4-
import {app, router, store} from './app';
5-
66
const isDev = process.env.NODE_ENV !== 'production';
77

88
export default function (context) {
99
const s = isDev && Date.now();
1010

11+
const { app, router, store } = createApp();
1112
// set router's location
1213
router.push(context.url);
1314
const matchedComponents = router.getMatchedComponents();
1415

1516
// no matched routes
1617
if (!matchedComponents.length) {
17-
return Promise.reject({code: '404'});
18+
return Promise.reject({ code: '404' });
1819
}
1920

2021
// Call preFetch hooks on components matched by the route.
@@ -36,4 +37,4 @@ export default function (context) {
3637
context.initialState = store.state;
3738
return app
3839
})
39-
}
40+
}

src/server/controller/home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ export default class HomeController extends BaseController {
3232
keyword: keyword
3333
});
3434
}
35-
}
35+
}

src/server/render/render.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ export function init(app) {
1414
if (process.env.NODE_ENV === 'production') {
1515
//如果是生产环境,bundle是构建完成的正式文件
1616
const bundlePath = path.resolve(__dirname, '../../../dist/server-bundle.js');
17-
renderer = createBundleRenderer(fs.readFileSync(bundlePath, 'utf-8'))
17+
renderer = createBundleRenderer(fs.readFileSync(bundlePath, 'utf-8'), { runInNewContext: 'once' })
1818
} else {
1919
//如果是开发环境,bundle会在改变之后重新回调生成
2020
require('../../../build/setup-dev-server')(app, bundle => {
21-
renderer = createBundleRenderer(bundle)
21+
renderer = createBundleRenderer(bundle, { runInNewContext: 'once' })
2222
})
2323
}
2424
}
2525

2626
export function getRenderer() {
2727
return renderer;
28-
}
28+
}

0 commit comments

Comments
 (0)