...
|
...
|
@@ -83,12 +83,14 @@ const handlerError = (err = {}, req, res, next) => { |
|
|
return next(err);
|
|
|
};
|
|
|
|
|
|
const getCacheKey = (urlPath, cacheKey = '') => {
|
|
|
const urlObj = url.parse(urlPath);
|
|
|
const getCacheKey = (req, cacheKey = '') => {
|
|
|
const urlObj = url.parse(req.url);
|
|
|
const yohoProtocol = req.get('User-Agent').indexOf('yoho-protocol') >= 0 ? 'yoho-protocol' : '';
|
|
|
|
|
|
return md5(cacheKey
|
|
|
.replace('$url', urlObj.pathname)
|
|
|
.replace('$params', urlObj.query));
|
|
|
.replace('$params', urlObj.query || '')
|
|
|
.replace('$yoho-protocol', yohoProtocol));
|
|
|
};
|
|
|
|
|
|
const render = (route) => {
|
...
|
...
|
@@ -96,11 +98,12 @@ const render = (route) => { |
|
|
try {
|
|
|
res.setHeader('X-YOHO-Version', pkg.version);
|
|
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
|
|
const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
|
|
|
|
|
if (isDegrade) {
|
|
|
return res.send(degradeHtml);
|
|
|
}
|
|
|
const ck = route.cacheKey ? getCacheKey(req.url, route.cacheKey) : void 0;
|
|
|
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
|
|
|
|
|
if (config.useCache && route.cache && ck) {
|
|
|
const html = await redis.getAsync(ck);
|
...
|
...
|
@@ -124,11 +127,10 @@ const render = (route) => { |
|
|
let context = getContext(req);
|
|
|
|
|
|
renderer.renderToString(context, (err, html) => {
|
|
|
|
|
|
if (err) {
|
|
|
return handlerError(err, req, res, next);
|
|
|
}
|
|
|
const styles = context.renderStyles();
|
|
|
let styles = context.renderStyles();
|
|
|
let scripts = context.renderScripts();
|
|
|
const resources = context.renderResourceHints();
|
|
|
const states = context.renderState();
|
...
|
...
|
@@ -137,6 +139,11 @@ const render = (route) => { |
|
|
if (req.yoho.isiOS) {
|
|
|
asyncScripts = asyncLoadScripts(scripts);
|
|
|
}
|
|
|
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
|
|
|
isYohoProtocol) {
|
|
|
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
}
|
|
|
|
|
|
const result = template({
|
|
|
html,
|
...
|
...
|
@@ -161,11 +168,15 @@ const devRender = (route) => { |
|
|
return async(req, res, next) => {
|
|
|
try {
|
|
|
res.setHeader('X-YOHO-Version', pkg.version);
|
|
|
const ck = route.cacheKey ? getCacheKey(req.url, route.cacheKey) : void 0;
|
|
|
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
|
|
|
|
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
|
|
|
|
|
// return require('request-promise')({
|
|
|
// url: 'http://m.yohobuy.com:6005/degrade.html'
|
|
|
// }).pipe(res);
|
|
|
if (isDegrade) {
|
|
|
return require('request-promise')({
|
|
|
url: 'http://m.yohobuy.com:6005/degrade.html'
|
|
|
}).pipe(res);
|
|
|
}
|
|
|
if (config.useCache && route.cache && ck) {
|
|
|
const html = await redis.getAsync(ck);
|
|
|
|
...
|
...
|
@@ -201,6 +212,10 @@ const devRender = (route) => { |
|
|
}
|
|
|
let {styles, scripts, resources, states, html} = msg;
|
|
|
|
|
|
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
route.cacheKey.indexOf('$yoho-protocol') >= 0) {
|
|
|
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
}
|
|
|
const result = template({
|
|
|
html,
|
|
|
styles,
|
...
|
...
|
@@ -209,7 +224,6 @@ const devRender = (route) => { |
|
|
states
|
|
|
});
|
|
|
|
|
|
|
|
|
if (config.useCache && route.cache && ck) {
|
|
|
redis.setex(ck, route.cacheTime || 60, result);
|
|
|
}
|
...
|
...
|
|