ssr.js
4.29 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('fs');
const path = require('path');
const rp = require('request-promise');
const LRU = require('lru-cache');
const _ = require('lodash');
const pkg = require('../../package.json');
const logger = global.yoho.logger;
const {createBundleRenderer} = require('vue-server-renderer');
const routes = [
{
route: /product\/\d+/,
cache: true,
disable: true
},
{
route: '/channel',
cache: true
},
{
route: '/channel/search',
cache: true
},
{
route: '/channel/men',
cache: true
},
{
route: '/channel/women',
cache: true
},
{
route: '/about',
cache: true
},
];
const isDev = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV;
let renderer;
let template = fs.readFileSync(path.join(__dirname, '../../src/index.html'), 'utf-8');
const microCache = LRU({ // eslint-disable-line
max: 10000,
maxAge: 2000
});
const getContext = (req) => {
return {
url: req.url,
title: 'BLK!',
user: req.user,
env: {
isApp: req.yoho.isApp,
isiOS: req.yoho.isiOS,
isAndroid: req.yoho.isAndroid,
isYohoBuy: req.yoho.isYohoBuy,
channel: req.yoho.channel
}
};
};
const render = (options) => {
return (req, res, next) => {
if (options.cache) {
const html = microCache.get(req.url);
if (html) {
console.log('cache', req.url);
return res.send(html);
}
}
let context = getContext(req);
renderer.renderToString(context, (err, html) => {
if (err) {
// TODO 处理错误类型
return next(err);
}
if (options.cache) {
microCache.set(req.url, html);
}
return res.send(html);
});
};
};
const devRender = () => {
return (req, res, next) => {
let context = getContext(req);
process.send({action: 'ssr_request', context});
let event = msg => {
process.removeListener('message', event);
if (msg.action === 'ssr_request') {
if (msg.err) {
try {
const err = JSON.parse(msg.err);
if (err.code === 404) {
return next();
}
return next(err);
} catch (e) {
return next({
code: 500,
message: msg.err
});
}
}
return res.end(msg.html);
}
};
process.on('message', event);
};
};
const loadBundle = async (errorCount = 0) => {
if (!isDev) {
if (errorCount > 5) {
throw {
code: 9999,
message: 'ssr bundle download faild 5!'
};
}
await Promise.all([
rp(`http://cdn.yoho.cn/yohoblk-wap/bundle/yoho-ssr-server-${pkg.version}.json`, {json: true}),
rp(`http://cdn.yoho.cn/yohoblk-wap/bundle/yoho-ssr-client-${pkg.version}.json`, {json: true}),
]).then(results => {
logger.warn('ssr file is loaded');
renderer = createBundleRenderer(results[0], {
runInNewContext: false,
template,
clientManifest: results[1]
});
}).catch(() => {
return loadBundle(errorCount + 1);
});
// const serverBundle = require(`../../public/dist/yohoblk-wap/bundle/yoho-ssr-server-${pkg.version}.json`);
// const clientManifest = require(`../../public/dist/yohoblk-wap/bundle/yoho-ssr-client-${pkg.version}.json`);
// renderer = createBundleRenderer(serverBundle, {
// runInNewContext: false,
// template,
// clientManifest
// });
}
};
const ssrRender = options => isDev ? devRender(options) : render(options);
module.exports = async (app) => {
await loadBundle();
_.each(routes, r => {
if (!r.disable) {
app.get(r.route, ssrRender({
cache: r.cache
}));
}
});
};