Skip to content

Commit

Permalink
fix(snippet): Allow async attribute to be removed from snippet with s…
Browse files Browse the repository at this point in the history
…nippetOptions.async = false - fixes #670
  • Loading branch information
Shane Osbourne committed Jun 17, 2015
1 parent 3503a54 commit c32bec6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/connect-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var connectUtils = {
}

var template = fs.readFileSync(config.templates.scriptTag, "utf-8");

var scriptPath = this.clientScript(options);
var async = options.getIn(["snippetOptions", "async"]);
var script;
var override = false;

Expand All @@ -38,7 +38,8 @@ var connectUtils = {
}

template = template
.replace("%script%", script);
.replace("%script%", script)
.replace("%async%", async ? "async" : "");

return template;
},
Expand Down
4 changes: 3 additions & 1 deletion lib/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,21 @@ module.exports = {
logSnippet: true,

/**
* SINCE 1.7.0! You can control how the snippet is injected
* You can control how the snippet is injected
* onto each page via a custom regex + function.
* You can also provide patterns for certain urls
* that should be ignored from the snippet injection.
* @property snippetOptions
* @since 2.0.0
* @param {Boolean} [async] - should the script tags have the async attribute?
* @param {Array} [blacklist]
* @param {Array} [whitelist]
* @param {RegExp} [rule.match=/<body[^>]*>/i]
* @param {Function} [rule.fn=Function]
* @type Object
*/
snippetOptions: {
async: true,
whitelist: [],
blacklist: [],
rule: {
Expand Down
4 changes: 2 additions & 2 deletions lib/templates/script-tags.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script type='text/javascript' id="__bs_script__">//<![CDATA[
document.write("<script async src='%script%'><\/script>".replace("HOST", location.hostname));
//]]></script>
document.write("<script %async%src='%script%'><\/script>".replace("HOST", location.hostname));
//]]></script>
32 changes: 32 additions & 0 deletions test/specs/e2e/e2e.options.script.async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

var browserSync = require("../../../");

var assert = require("chai").assert;

describe("E2E script with/without async attribute", function () {

it("serves with async", function (done) {
browserSync.reset();
var config = {open: false};
browserSync(config, function (err, bs) {
assert.include(bs.options.get("snippet"), "async");
bs.cleanup();
done();
});
});
it("serves without async", function (done) {
browserSync.reset();
var config = {
open: false,
snippetOptions: {
async: false
}
};
browserSync(config, function (err, bs) {
assert.notInclude(bs.options.get("snippet"), "async");
bs.cleanup();
done();
});
});
});

0 comments on commit c32bec6

Please sign in to comment.