Authored by yyq

statics preload

@@ -10,6 +10,17 @@ module.exports = (app) => { @@ -10,6 +10,17 @@ module.exports = (app) => {
10 function getStatic(path, def) { 10 function getStatic(path, def) {
11 return _.get(manifest, path, `${config.assetUrl}${def}`); 11 return _.get(manifest, path, `${config.assetUrl}${def}`);
12 } 12 }
  13 +
  14 + function getPreloads(list, type) {
  15 + let _list = [];
  16 +
  17 + _.forEach(list, url => {
  18 + _list.push({url, type});
  19 + });
  20 +
  21 + return _list;
  22 + }
  23 +
13 return (req, res, next) => { 24 return (req, res, next) => {
14 res.on('beforeRender', (params) => { 25 res.on('beforeRender', (params) => {
15 if (params) { 26 if (params) {
@@ -35,7 +46,10 @@ module.exports = (app) => { @@ -35,7 +46,10 @@ module.exports = (app) => {
35 } else { 46 } else {
36 statics.styles.push(getStatic('common.index.css', 'css/index.css')); 47 statics.styles.push(getStatic('common.index.css', 'css/index.css'));
37 } 48 }
  49 +
38 res.locals.statics = statics; 50 res.locals.statics = statics;
  51 + res.locals.preloads = _.concat(getPreloads(statics.styles, 'style'),
  52 + getPreloads(statics.javascripts, 'script'));
39 } 53 }
40 } 54 }
41 }); 55 });
@@ -24,6 +24,10 @@ @@ -24,6 +24,10 @@
24 <link rel="dns-prefetch" href="{{this}}"> 24 <link rel="dns-prefetch" href="{{this}}">
25 {{/dnsPrefetch.hosts}} 25 {{/dnsPrefetch.hosts}}
26 26
  27 + {{#preloads}}
  28 + <link rel="preload" href="{{url}}" as="{{type}}">
  29 + {{/preloads}}
  30 +
27 <script> 31 <script>
28 {{#ifand isProduction wap.open.bughd}} 32 {{#ifand isProduction wap.open.bughd}}
29 window._timeStart = new Date().getTime(); 33 window._timeStart = new Date().getTime();