Authored by 陈峰

wkwebview css preload

@@ -39,7 +39,7 @@ exports.devServer = (app, cb) => { @@ -39,7 +39,7 @@ exports.devServer = (app, cb) => {
39 }; 39 };
40 40
41 clientConfig.entry.app = ['./build/client-hot.js', clientConfig.entry.app]; 41 clientConfig.entry.app = ['./build/client-hot.js', clientConfig.entry.app];
42 - clientConfig.output.publicPath = 'http://m.yohobuy.com:6005/'; 42 + clientConfig.output.publicPath = '//m.yohobuy.com:6005/';
43 clientConfig.output.filename = '[name].js'; 43 clientConfig.output.filename = '[name].js';
44 clientConfig.plugins.push( 44 clientConfig.plugins.push(
45 new webpack.HotModuleReplacementPlugin(), 45 new webpack.HotModuleReplacementPlugin(),
@@ -46,7 +46,7 @@ module.exports = { @@ -46,7 +46,7 @@ module.exports = {
46 host: 'badjs.yoho.cn', 46 host: 'badjs.yoho.cn',
47 port: 80, 47 port: 80,
48 db: 'web-apm', 48 db: 'web-apm',
49 - immediate: true 49 + duration: 1000
50 }, 50 },
51 subDomains: { 51 subDomains: {
52 host: '.m.yohobuy.com', 52 host: '.m.yohobuy.com',
@@ -175,7 +175,7 @@ if (isProduction) { @@ -175,7 +175,7 @@ if (isProduction) {
175 host: 'badjs.yohoops.org', 175 host: 'badjs.yohoops.org',
176 port: 80, 176 port: 80,
177 db: 'web-apm', 177 db: 'web-apm',
178 - immediate: true 178 + duration: 1000
179 }, 179 },
180 monitorReport: { 180 monitorReport: {
181 host: '10.66.4.25', 181 host: '10.66.4.25',
1 module.exports = [ 1 module.exports = [
2 { 2 {
3 route: /grass\/article\/\d+$/, 3 route: /grass\/article\/\d+$/,
4 - cacheKey: '$url$params', 4 + cacheKey: '$url$params$yoho-protocol',
5 cacheTime: 900, 5 cacheTime: 900,
6 cache: true 6 cache: true
7 }, 7 },
8 { 8 {
9 route: /grass\/article\/\d+\/user/, 9 route: /grass\/article\/\d+\/user/,
10 - cacheKey: '$url$params', 10 + cacheKey: '$url$params$yoho-protocol',
11 cacheTime: 900, 11 cacheTime: 900,
12 cache: true 12 cache: true
13 }, 13 },
14 { 14 {
15 route: /grass\/author\/\d+\/\d+/, 15 route: /grass\/author\/\d+\/\d+/,
16 - cacheKey: '$url$params', 16 + cacheKey: '$url$params$yoho-protocol',
17 cacheTime: 900, 17 cacheTime: 900,
18 cache: true 18 cache: true
19 } 19 }
@@ -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 }
@@ -26,7 +26,6 @@ @@ -26,7 +26,6 @@
26 {{#if asyncScripts}} 26 {{#if asyncScripts}}
27 <script> 27 <script>
28 document.addEventListener('DOMContentLoaded', function() { 28 document.addEventListener('DOMContentLoaded', function() {
29 - console.log('DOMContentLoaded')  
30 setTimeout(function() { 29 setTimeout(function() {
31 var s = document.getElementsByTagName("script")[0]; 30 var s = document.getElementsByTagName("script")[0];
32 {{# asyncScripts}} 31 {{# asyncScripts}}