|
@@ -83,12 +83,14 @@ const handlerError = (err = {}, req, res, next) => { |
|
@@ -83,12 +83,14 @@ const handlerError = (err = {}, req, res, next) => { |
83
|
return next(err);
|
83
|
return next(err);
|
84
|
};
|
84
|
};
|
85
|
|
85
|
|
86
|
-const getCacheKey = (urlPath, cacheKey = '') => {
|
|
|
87
|
- const urlObj = url.parse(urlPath);
|
86
|
+const getCacheKey = (req, cacheKey = '') => {
|
|
|
87
|
+ const urlObj = url.parse(req.url);
|
|
|
88
|
+ const yohoProtocol = req.get('User-Agent').indexOf('yoho-protocol') >= 0 ? 'yoho-protocol' : '';
|
88
|
|
89
|
|
89
|
return md5(cacheKey
|
90
|
return md5(cacheKey
|
90
|
.replace('$url', urlObj.pathname)
|
91
|
.replace('$url', urlObj.pathname)
|
91
|
- .replace('$params', urlObj.query));
|
92
|
+ .replace('$params', urlObj.query || '')
|
|
|
93
|
+ .replace('$yoho-protocol', yohoProtocol));
|
92
|
};
|
94
|
};
|
93
|
|
95
|
|
94
|
const render = (route) => {
|
96
|
const render = (route) => {
|
|
@@ -96,11 +98,12 @@ const render = (route) => { |
|
@@ -96,11 +98,12 @@ const render = (route) => { |
96
|
try {
|
98
|
try {
|
97
|
res.setHeader('X-YOHO-Version', pkg.version);
|
99
|
res.setHeader('X-YOHO-Version', pkg.version);
|
98
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
100
|
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
|
|
101
|
+ const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
|
99
|
|
102
|
|
100
|
if (isDegrade) {
|
103
|
if (isDegrade) {
|
101
|
return res.send(degradeHtml);
|
104
|
return res.send(degradeHtml);
|
102
|
}
|
105
|
}
|
103
|
- const ck = route.cacheKey ? getCacheKey(req.url, route.cacheKey) : void 0;
|
106
|
+ const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
104
|
|
107
|
|
105
|
if (config.useCache && route.cache && ck) {
|
108
|
if (config.useCache && route.cache && ck) {
|
106
|
const html = await redis.getAsync(ck);
|
109
|
const html = await redis.getAsync(ck);
|
|
@@ -124,11 +127,10 @@ const render = (route) => { |
|
@@ -124,11 +127,10 @@ const render = (route) => { |
124
|
let context = getContext(req);
|
127
|
let context = getContext(req);
|
125
|
|
128
|
|
126
|
renderer.renderToString(context, (err, html) => {
|
129
|
renderer.renderToString(context, (err, html) => {
|
127
|
-
|
|
|
128
|
if (err) {
|
130
|
if (err) {
|
129
|
return handlerError(err, req, res, next);
|
131
|
return handlerError(err, req, res, next);
|
130
|
}
|
132
|
}
|
131
|
- const styles = context.renderStyles();
|
133
|
+ let styles = context.renderStyles();
|
132
|
let scripts = context.renderScripts();
|
134
|
let scripts = context.renderScripts();
|
133
|
const resources = context.renderResourceHints();
|
135
|
const resources = context.renderResourceHints();
|
134
|
const states = context.renderState();
|
136
|
const states = context.renderState();
|
|
@@ -137,6 +139,11 @@ const render = (route) => { |
|
@@ -137,6 +139,11 @@ const render = (route) => { |
137
|
if (req.yoho.isiOS) {
|
139
|
if (req.yoho.isiOS) {
|
138
|
asyncScripts = asyncLoadScripts(scripts);
|
140
|
asyncScripts = asyncLoadScripts(scripts);
|
139
|
}
|
141
|
}
|
|
|
142
|
+ if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
143
|
+ route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
|
|
|
144
|
+ isYohoProtocol) {
|
|
|
145
|
+ styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
146
|
+ }
|
140
|
|
147
|
|
141
|
const result = template({
|
148
|
const result = template({
|
142
|
html,
|
149
|
html,
|
|
@@ -161,11 +168,15 @@ const devRender = (route) => { |
|
@@ -161,11 +168,15 @@ const devRender = (route) => { |
161
|
return async(req, res, next) => {
|
168
|
return async(req, res, next) => {
|
162
|
try {
|
169
|
try {
|
163
|
res.setHeader('X-YOHO-Version', pkg.version);
|
170
|
res.setHeader('X-YOHO-Version', pkg.version);
|
164
|
- const ck = route.cacheKey ? getCacheKey(req.url, route.cacheKey) : void 0;
|
171
|
+ const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
|
|
|
172
|
+
|
|
|
173
|
+ const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
|
165
|
|
174
|
|
166
|
- // return require('request-promise')({
|
|
|
167
|
- // url: 'http://m.yohobuy.com:6005/degrade.html'
|
|
|
168
|
- // }).pipe(res);
|
175
|
+ if (isDegrade) {
|
|
|
176
|
+ return require('request-promise')({
|
|
|
177
|
+ url: 'http://m.yohobuy.com:6005/degrade.html'
|
|
|
178
|
+ }).pipe(res);
|
|
|
179
|
+ }
|
169
|
if (config.useCache && route.cache && ck) {
|
180
|
if (config.useCache && route.cache && ck) {
|
170
|
const html = await redis.getAsync(ck);
|
181
|
const html = await redis.getAsync(ck);
|
171
|
|
182
|
|
|
@@ -201,6 +212,10 @@ const devRender = (route) => { |
|
@@ -201,6 +212,10 @@ const devRender = (route) => { |
201
|
}
|
212
|
}
|
202
|
let {styles, scripts, resources, states, html} = msg;
|
213
|
let {styles, scripts, resources, states, html} = msg;
|
203
|
|
214
|
|
|
|
215
|
+ if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
|
|
|
216
|
+ route.cacheKey.indexOf('$yoho-protocol') >= 0) {
|
|
|
217
|
+ styles = styles.replace(/"\/\//g, '"yoho-protocol://');
|
|
|
218
|
+ }
|
204
|
const result = template({
|
219
|
const result = template({
|
205
|
html,
|
220
|
html,
|
206
|
styles,
|
221
|
styles,
|
|
@@ -209,7 +224,6 @@ const devRender = (route) => { |
|
@@ -209,7 +224,6 @@ const devRender = (route) => { |
209
|
states
|
224
|
states
|
210
|
});
|
225
|
});
|
211
|
|
226
|
|
212
|
-
|
|
|
213
|
if (config.useCache && route.cache && ck) {
|
227
|
if (config.useCache && route.cache && ck) {
|
214
|
redis.setex(ck, route.cacheTime || 60, result);
|
228
|
redis.setex(ck, route.cacheTime || 60, result);
|
215
|
}
|
229
|
}
|