Authored by 毕凯

优化 JS 记载

@@ -116,10 +116,63 @@ @@ -116,10 +116,63 @@
116 {{/wechatShare}} 116 {{/wechatShare}}
117 <script>var STATIC_RESOURCE_HASH = '{{startTime}}';</script> 117 <script>var STATIC_RESOURCE_HASH = '{{startTime}}';</script>
118 {{#if devEnv}} 118 {{#if devEnv}}
119 - <script>var STATIC_RESOURCE_PATH = '//{{devHost}}:{{port}}';</script> 119 + <script>
  120 + var STATIC_RESOURCE_PATH = '//{{devHost}}:{{port}}';
  121 + window.jsFiles = ["//{{devHost}}:{{port}}/libs.js?t={{startTime}}", "//{{devHost}}:{{port}}/{{module}}.{{page}}.js?t={{startTime}}"];
  122 + </script>
  123 +
  124 + <script>
  125 + (function() {
  126 + window.jsFiles.forEach(function(file) {
  127 + var preloadLink = document.createElement('link');
  128 +
  129 + preloadLink.href = file;
  130 + preloadLink.rel = 'preload';
  131 + preloadLink.as = 'script';
  132 + document.head.appendChild(preloadLink);
  133 + });
  134 + window.loadJS = function loadJS(src, retryMode) {
  135 + var ref = window.document.getElementsByTagName('script')[ 0 ];
  136 + var script = window.document.createElement('script');
  137 + var scriptSrc;
  138 +
  139 + if (retryMode) {
  140 + scriptSrc = src;
  141 + } else {
  142 + src && window.jsFiles.push(src);
  143 + scriptSrc = window.jsFiles.shift();
  144 + }
  145 +
  146 + if (!scriptSrc) {
  147 + return;
  148 + }
120 149
121 - <script src="//{{devHost}}:{{port}}/libs.js?t={{startTime}}"></script>  
122 - <script src="//{{devHost}}:{{port}}/{{module}}.{{page}}.js?t={{startTime}}"></script> 150 + script.src = scriptSrc;
  151 + script.crossorigin = 'anonymous';
  152 +
  153 + if (!retryMode) {
  154 + script.onerror = function() {
  155 + if (script.parentNode) {
  156 + script.parentNode.removeChild(script);
  157 + }
  158 +
  159 + loadJS(script.src, true);
  160 + };
  161 + }
  162 +
  163 + script.onload = function() {
  164 + loadJS();
  165 + };
  166 +
  167 + ref.parentNode.insertBefore(script, ref.nextSibling);
  168 + return script;
  169 + };
  170 +
  171 + loadJS();
  172 + }());
  173 + </script>
  174 + {{!-- <script src="//{{devHost}}:{{port}}/libs.js?t={{startTime}}"></script>
  175 + <script src="//{{devHost}}:{{port}}/{{module}}.{{page}}.js?t={{startTime}}"></script> --}}
123 {{^}} 176 {{^}}
124 <script>var STATIC_RESOURCE_PATH = '//cdn.yoho.cn/yohobuywap-node/{{version}}';</script> 177 <script>var STATIC_RESOURCE_PATH = '//cdn.yoho.cn/yohobuywap-node/{{version}}';</script>
125 <script src="//cdn.yoho.cn/yohobuywap-node/{{version}}/libs.js?t={{startTime}}" crossorigin="anonymous"></script> 178 <script src="//cdn.yoho.cn/yohobuywap-node/{{version}}/libs.js?t={{startTime}}" crossorigin="anonymous"></script>