Skip to content

Commit c32bec6

Browse files
author
Shane Osbourne
committed
fix(snippet): Allow async attribute to be removed from snippet with snippetOptions.async = false - fixes #670
1 parent 3503a54 commit c32bec6

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

lib/connect-utils.js

+3-2
Original file line numberDiff line numberDiff 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
},

lib/default-config.js

+3-1
Original file line numberDiff line numberDiff 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: {

lib/templates/script-tags.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
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>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
});

0 commit comments

Comments
 (0)