|
@@ -82,16 +82,20 @@ const handlerError = (err = {}, req, res, next) => { |
|
@@ -82,16 +82,20 @@ const handlerError = (err = {}, req, res, next) => { |
82
|
return next(err);
|
82
|
return next(err);
|
83
|
};
|
83
|
};
|
84
|
|
84
|
|
85
|
-const getCacheKey = (req, cacheKey = '') => {
|
|
|
86
|
- const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
85
|
+const getCacheKey = (req, route) => {
|
87
|
const urlObj = url.parse(req.url);
|
86
|
const urlObj = url.parse(req.url);
|
88
|
const isIos = req.yoho.isiOS;
|
87
|
const isIos = req.yoho.isiOS;
|
89
|
- const yohoProtocol = (req.get('User-Agent').indexOf('yoho-protocol') >= 0 && isYohoProtocol) ? 'yoho-protocol' : '';
|
88
|
+ let ck = urlObj.pathname;
|
90
|
|
89
|
|
91
|
- return md5(cacheKey
|
|
|
92
|
- .replace('$url', urlObj.pathname)
|
|
|
93
|
- .replace('$params', urlObj.query || '')
|
|
|
94
|
- .replace('$yoho-protocol', yohoProtocol) + (isIos ? 'ios' : 'android'));
|
90
|
+ if (route.query) {
|
|
|
91
|
+ const qks = Object.keys(route.query);
|
|
|
92
|
+
|
|
|
93
|
+ ck += `?${qks.map(qk => `${qk}=${req.query && req.query[qk] || ''}`).join('&')}`;
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ ck += `|${isIos ? 'ios' : 'android'}`;
|
|
|
97
|
+
|
|
|
98
|
+ return md5(ck);
|
95
|
};
|
99
|
};
|
96
|
|
100
|
|
97
|
const render = (route) => {
|
101
|
const render = (route) => {
|
|
@@ -99,12 +103,11 @@ const render = (route) => { |
|
@@ -99,12 +103,11 @@ const render = (route) => { |
99
|
try {
|
103
|
try {
|
100
|
res.setHeader('X-YOHO-Version', pkg.version);
|
104
|
res.setHeader('X-YOHO-Version', pkg.version);
|
101
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
105
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
102
|
- const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
|
|
103
|
|
106
|
|
104
|
if (isDegrade) {
|
107
|
if (isDegrade) {
|
105
|
return res.send(degradeHtml);
|
108
|
return res.send(degradeHtml);
|
106
|
}
|
109
|
}
|
107
|
- const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
110
|
+ const ck = getCacheKey(req, route);
|
108
|
|
111
|
|
109
|
if (config.useCache && route.cache && ck) {
|
112
|
if (config.useCache && route.cache && ck) {
|
110
|
const html = await redis.getAsync(ck);
|
113
|
const html = await redis.getAsync(ck);
|
|
@@ -136,23 +139,17 @@ const render = (route) => { |
|
@@ -136,23 +139,17 @@ const render = (route) => { |
136
|
let resources = context.renderResourceHints();
|
139
|
let resources = context.renderResourceHints();
|
137
|
const states = context.renderState();
|
140
|
const states = context.renderState();
|
138
|
let asyncScripts;
|
141
|
let asyncScripts;
|
139
|
- let zk = {};
|
142
|
+ let zk = {
|
|
|
143
|
+ asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true)
|
|
|
144
|
+ };
|
140
|
|
145
|
|
141
|
if (process.env.NODE_ENV === 'production') {
|
146
|
if (process.env.NODE_ENV === 'production') {
|
142
|
zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
|
147
|
zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
|
143
|
- zk.asyncJs = _.get(req.app.locals.wap, 'webapp.ios-async-js', true);
|
|
|
144
|
}
|
148
|
}
|
145
|
|
149
|
|
146
|
if (req.yoho.isiOS && zk.asyncJs) {
|
150
|
if (req.yoho.isiOS && zk.asyncJs) {
|
147
|
asyncScripts = asyncLoadScripts(scripts);
|
151
|
asyncScripts = asyncLoadScripts(scripts);
|
148
|
}
|
152
|
}
|
149
|
- if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
150
|
- route.cacheKey &&
|
|
|
151
|
- route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
|
|
|
152
|
- isYohoProtocol) {
|
|
|
153
|
- styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
154
|
- resources = resources.replace(/<link rel="preload" href="[^"]+" as="style">/g, '');
|
|
|
155
|
- }
|
|
|
156
|
|
153
|
|
157
|
const result = template({
|
154
|
const result = template({
|
158
|
html,
|
155
|
html,
|
|
@@ -179,7 +176,7 @@ const devRender = (route) => { |
|
@@ -179,7 +176,7 @@ const devRender = (route) => { |
179
|
return async(req, res, next) => {
|
176
|
return async(req, res, next) => {
|
180
|
try {
|
177
|
try {
|
181
|
res.setHeader('X-YOHO-Version', pkg.version);
|
178
|
res.setHeader('X-YOHO-Version', pkg.version);
|
182
|
- const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
179
|
+ const ck = getCacheKey(req, route);
|
183
|
|
180
|
|
184
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
181
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
185
|
|
182
|
|
|
@@ -223,11 +220,6 @@ const devRender = (route) => { |
|
@@ -223,11 +220,6 @@ const devRender = (route) => { |
223
|
}
|
220
|
}
|
224
|
let {styles, scripts, resources, states, html} = msg;
|
221
|
let {styles, scripts, resources, states, html} = msg;
|
225
|
|
222
|
|
226
|
- if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
227
|
- route.cacheKey &&
|
|
|
228
|
- route.cacheKey.indexOf('$yoho-protocol') >= 0) {
|
|
|
229
|
- styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
230
|
- }
|
|
|
231
|
const result = template({
|
223
|
const result = template({
|
232
|
html,
|
224
|
html,
|
233
|
styles,
|
225
|
styles,
|