|
@@ -62,7 +62,7 @@ const versionToNumber = (str, len = 3) => { |
|
@@ -62,7 +62,7 @@ const versionToNumber = (str, len = 3) => { |
62
|
}
|
62
|
}
|
63
|
|
63
|
|
64
|
return verNum;
|
64
|
return verNum;
|
65
|
-}
|
65
|
+};
|
66
|
|
66
|
|
67
|
const getImmersedStatus = (req) => {
|
67
|
const getImmersedStatus = (req) => {
|
68
|
let status = false;
|
68
|
let status = false;
|
|
@@ -76,7 +76,7 @@ const getImmersedStatus = (req) => { |
|
@@ -76,7 +76,7 @@ const getImmersedStatus = (req) => { |
76
|
}
|
76
|
}
|
77
|
|
77
|
|
78
|
return status;
|
78
|
return status;
|
79
|
-}
|
79
|
+};
|
80
|
|
80
|
|
81
|
const getContext = (req) => {
|
81
|
const getContext = (req) => {
|
82
|
return {
|
82
|
return {
|
|
@@ -107,16 +107,20 @@ const handlerError = (err = {}, req, res, next) => { |
|
@@ -107,16 +107,20 @@ const handlerError = (err = {}, req, res, next) => { |
107
|
return next(err);
|
107
|
return next(err);
|
108
|
};
|
108
|
};
|
109
|
|
109
|
|
110
|
-const getCacheKey = (req, cacheKey = '') => {
|
|
|
111
|
- const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
110
|
+const getCacheKey = (req, route) => {
|
112
|
const urlObj = url.parse(req.url);
|
111
|
const urlObj = url.parse(req.url);
|
113
|
const isIos = req.yoho.isiOS;
|
112
|
const isIos = req.yoho.isiOS;
|
114
|
- const yohoProtocol = (req.get('User-Agent').indexOf('yoho-protocol') >= 0 && isYohoProtocol) ? 'yoho-protocol' : '';
|
113
|
+ let ck = urlObj.pathname;
|
|
|
114
|
+
|
|
|
115
|
+ if (route.query) {
|
|
|
116
|
+ const qks = Object.keys(route.query);
|
115
|
|
117
|
|
116
|
- return md5(cacheKey
|
|
|
117
|
- .replace('$url', urlObj.pathname)
|
|
|
118
|
- .replace('$params', urlObj.query || '')
|
|
|
119
|
- .replace('$yoho-protocol', yohoProtocol) + (isIos ? 'ios' : 'android'));
|
118
|
+ ck += `?${qks.map(qk => `${qk}=${req.query && req.query[qk] || ''}`).join('&')}`;
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ ck += `|${isIos ? 'ios' : 'android'}`;
|
|
|
122
|
+
|
|
|
123
|
+ return md5(ck);
|
120
|
};
|
124
|
};
|
121
|
|
125
|
|
122
|
const render = (route) => {
|
126
|
const render = (route) => {
|
|
@@ -124,12 +128,11 @@ const render = (route) => { |
|
@@ -124,12 +128,11 @@ const render = (route) => { |
124
|
try {
|
128
|
try {
|
125
|
res.setHeader('X-YOHO-Version', pkg.version);
|
129
|
res.setHeader('X-YOHO-Version', pkg.version);
|
126
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
130
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
127
|
- const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
|
|
128
|
|
131
|
|
129
|
if (isDegrade) {
|
132
|
if (isDegrade) {
|
130
|
return res.send(degradeHtml);
|
133
|
return res.send(degradeHtml);
|
131
|
}
|
134
|
}
|
132
|
- const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
135
|
+ const ck = getCacheKey(req, route);
|
133
|
|
136
|
|
134
|
if (config.useCache && route.cache && ck) {
|
137
|
if (config.useCache && route.cache && ck) {
|
135
|
const html = await redis.getAsync(ck);
|
138
|
const html = await redis.getAsync(ck);
|
|
@@ -161,23 +164,17 @@ const render = (route) => { |
|
@@ -161,23 +164,17 @@ const render = (route) => { |
161
|
let resources = context.renderResourceHints();
|
164
|
let resources = context.renderResourceHints();
|
162
|
const states = context.renderState();
|
165
|
const states = context.renderState();
|
163
|
let asyncScripts;
|
166
|
let asyncScripts;
|
164
|
- let zk = {};
|
167
|
+ let zk = {
|
|
|
168
|
+ asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true)
|
|
|
169
|
+ };
|
165
|
|
170
|
|
166
|
if (process.env.NODE_ENV === 'production') {
|
171
|
if (process.env.NODE_ENV === 'production') {
|
167
|
zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
|
172
|
zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
|
168
|
- zk.asyncJs = _.get(req.app.locals.wap, 'webapp.ios-async-js', true);
|
|
|
169
|
}
|
173
|
}
|
170
|
|
174
|
|
171
|
if (req.yoho.isiOS && zk.asyncJs) {
|
175
|
if (req.yoho.isiOS && zk.asyncJs) {
|
172
|
asyncScripts = asyncLoadScripts(scripts);
|
176
|
asyncScripts = asyncLoadScripts(scripts);
|
173
|
}
|
177
|
}
|
174
|
- if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
175
|
- route.cacheKey &&
|
|
|
176
|
- route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
|
|
|
177
|
- isYohoProtocol) {
|
|
|
178
|
- styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
179
|
- resources = resources.replace(/<link rel="preload" href="[^"]+" as="style">/g, '');
|
|
|
180
|
- }
|
|
|
181
|
|
178
|
|
182
|
const result = template({
|
179
|
const result = template({
|
183
|
html,
|
180
|
html,
|
|
@@ -205,7 +202,7 @@ const devRender = (route) => { |
|
@@ -205,7 +202,7 @@ const devRender = (route) => { |
205
|
return async(req, res, next) => {
|
202
|
return async(req, res, next) => {
|
206
|
try {
|
203
|
try {
|
207
|
res.setHeader('X-YOHO-Version', pkg.version);
|
204
|
res.setHeader('X-YOHO-Version', pkg.version);
|
208
|
- const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
205
|
+ const ck = getCacheKey(req, route);
|
209
|
|
206
|
|
210
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
207
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
211
|
|
208
|
|
|
@@ -249,11 +246,6 @@ const devRender = (route) => { |
|
@@ -249,11 +246,6 @@ const devRender = (route) => { |
249
|
}
|
246
|
}
|
250
|
let {styles, scripts, resources, states, html} = msg;
|
247
|
let {styles, scripts, resources, states, html} = msg;
|
251
|
|
248
|
|
252
|
- if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
253
|
- route.cacheKey &&
|
|
|
254
|
- route.cacheKey.indexOf('$yoho-protocol') >= 0) {
|
|
|
255
|
- styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
256
|
- }
|
|
|
257
|
const result = template({
|
249
|
const result = template({
|
258
|
html,
|
250
|
html,
|
259
|
styles,
|
251
|
styles,
|