...
|
...
|
@@ -82,16 +82,20 @@ const handlerError = (err = {}, req, res, next) => { |
|
|
return next(err);
|
|
|
};
|
|
|
|
|
|
const getCacheKey = (req, cacheKey = '') => {
|
|
|
const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
|
|
const getCacheKey = (req, route) => {
|
|
|
const urlObj = url.parse(req.url);
|
|
|
const isIos = req.yoho.isiOS;
|
|
|
const yohoProtocol = (req.get('User-Agent').indexOf('yoho-protocol') >= 0 && isYohoProtocol) ? 'yoho-protocol' : '';
|
|
|
let ck = urlObj.pathname;
|
|
|
|
|
|
return md5(cacheKey
|
|
|
.replace('$url', urlObj.pathname)
|
|
|
.replace('$params', urlObj.query || '')
|
|
|
.replace('$yoho-protocol', yohoProtocol) + (isIos ? 'ios' : 'android'));
|
|
|
if (route.query) {
|
|
|
const qks = Object.keys(route.query);
|
|
|
|
|
|
ck += `?${qks.map(qk => `${qk}=${req.query && req.query[qk] || ''}`).join('&')}`;
|
|
|
}
|
|
|
|
|
|
ck += `|${isIos ? 'ios' : 'android'}`;
|
|
|
|
|
|
return md5(ck);
|
|
|
};
|
|
|
|
|
|
const render = (route) => {
|
...
|
...
|
@@ -99,12 +103,11 @@ 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, route.cacheKey) : void 0;
|
|
|
const ck = getCacheKey(req, route);
|
|
|
|
|
|
if (config.useCache && route.cache && ck) {
|
|
|
const html = await redis.getAsync(ck);
|
...
|
...
|
@@ -136,23 +139,17 @@ const render = (route) => { |
|
|
let resources = context.renderResourceHints();
|
|
|
const states = context.renderState();
|
|
|
let asyncScripts;
|
|
|
let zk = {};
|
|
|
let zk = {
|
|
|
asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true)
|
|
|
};
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
|
|
|
zk.asyncJs = _.get(req.app.locals.wap, 'webapp.ios-async-js', true);
|
|
|
}
|
|
|
|
|
|
if (req.yoho.isiOS && zk.asyncJs) {
|
|
|
asyncScripts = asyncLoadScripts(scripts);
|
|
|
}
|
|
|
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
route.cacheKey &&
|
|
|
route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
|
|
|
isYohoProtocol) {
|
|
|
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
resources = resources.replace(/<link rel="preload" href="[^"]+" as="style">/g, '');
|
|
|
}
|
|
|
|
|
|
const result = template({
|
|
|
html,
|
...
|
...
|
@@ -179,7 +176,7 @@ const devRender = (route) => { |
|
|
return async(req, res, next) => {
|
|
|
try {
|
|
|
res.setHeader('X-YOHO-Version', pkg.version);
|
|
|
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
|
|
const ck = getCacheKey(req, route);
|
|
|
|
|
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
|
|
|
...
|
...
|
@@ -223,11 +220,6 @@ const devRender = (route) => { |
|
|
}
|
|
|
let {styles, scripts, resources, states, html} = msg;
|
|
|
|
|
|
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
route.cacheKey &&
|
|
|
route.cacheKey.indexOf('$yoho-protocol') >= 0) {
|
|
|
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
}
|
|
|
const result = template({
|
|
|
html,
|
|
|
styles,
|
...
|
...
|
|