File tree Expand file tree Collapse file tree 4 files changed +40
-5
lines changed Expand file tree Collapse file tree 4 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ var connectUtils = {
2020        } 
2121
2222        var  template  =  fs . readFileSync ( config . templates . scriptTag ,  "utf-8" ) ; 
23- 
2423        var  scriptPath  =  this . clientScript ( options ) ; 
24+         var  async  =  options . getIn ( [ "snippetOptions" ,  "async" ] ) ; 
2525        var  script ; 
2626        var  override  =  false ; 
2727
@@ -38,7 +38,8 @@ var connectUtils = {
3838        } 
3939
4040        template  =  template 
41-             . replace ( "%script%" ,  script ) ; 
41+             . replace ( "%script%" ,  script ) 
42+             . replace ( "%async%" ,  async  ? "async"  : "" ) ; 
4243
4344        return  template ; 
4445    } , 
Original file line number Diff line number Diff line change @@ -158,19 +158,21 @@ module.exports = {
158158    logSnippet : true , 
159159
160160    /** 
161-      * SINCE 1.7.0!  You can control how the snippet is injected 
161+      * You can control how the snippet is injected 
162162     * onto each page via a custom regex + function. 
163163     * You can also provide patterns for certain urls 
164164     * that should be ignored from the snippet injection. 
165165     * @property  snippetOptions 
166166     * @since  2.0.0 
167+      * @param  {Boolean } [async] - should the script tags have the async attribute? 
167168     * @param  {Array } [blacklist] 
168169     * @param  {Array } [whitelist] 
169170     * @param  {RegExp } [rule.match=/<body[^>]*>/i] 
170171     * @param  {Function } [rule.fn=Function] 
171172     * @type  Object 
172173     */ 
173174    snippetOptions : { 
175+         async : true , 
174176        whitelist : [ ] , 
175177        blacklist : [ ] , 
176178        rule : { 
Original file line number Diff line number Diff line change 11<script type='text/javascript' id="__bs_script__">//<![CDATA[
2-     document.write("<script async  src='%script%'><\/script>".replace("HOST", location.hostname));
3- //]]></script>
2+     document.write("<script % async% src='%script%'><\/script>".replace("HOST", location.hostname));
3+ //]]></script>
Original file line number Diff line number Diff line change 1+ "use strict" ; 
2+ 
3+ var  browserSync  =  require ( "../../../" ) ; 
4+ 
5+ var  assert  =  require ( "chai" ) . assert ; 
6+ 
7+ describe ( "E2E script with/without async attribute" ,  function  ( )  { 
8+ 
9+     it ( "serves with async" ,  function  ( done )  { 
10+         browserSync . reset ( ) ; 
11+         var  config  =  { open : false } ; 
12+         browserSync ( config ,  function  ( err ,  bs )  { 
13+             assert . include ( bs . options . get ( "snippet" ) ,  "async" ) ; 
14+             bs . cleanup ( ) ; 
15+             done ( ) ; 
16+         } ) ; 
17+     } ) ; 
18+     it ( "serves without async" ,  function  ( done )  { 
19+         browserSync . reset ( ) ; 
20+         var  config  =  { 
21+             open : false , 
22+             snippetOptions : { 
23+                 async : false 
24+             } 
25+         } ; 
26+         browserSync ( config ,  function  ( err ,  bs )  { 
27+             assert . notInclude ( bs . options . get ( "snippet" ) ,  "async" ) ; 
28+             bs . cleanup ( ) ; 
29+             done ( ) ; 
30+         } ) ; 
31+     } ) ; 
32+ } ) ; 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments