From c32bec6d464e7b1b52534233bfbf8ba885c4f295 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Wed, 17 Jun 2015 11:10:58 +0100 Subject: [PATCH] fix(snippet): Allow async attribute to be removed from snippet with snippetOptions.async = false - fixes #670 --- lib/connect-utils.js | 5 ++-- lib/default-config.js | 4 ++- lib/templates/script-tags.tmpl | 4 +-- test/specs/e2e/e2e.options.script.async.js | 32 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/specs/e2e/e2e.options.script.async.js diff --git a/lib/connect-utils.js b/lib/connect-utils.js index 662758498..1ff623ca1 100644 --- a/lib/connect-utils.js +++ b/lib/connect-utils.js @@ -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; @@ -38,7 +38,8 @@ var connectUtils = { } template = template - .replace("%script%", script); + .replace("%script%", script) + .replace("%async%", async ? "async" : ""); return template; }, diff --git a/lib/default-config.js b/lib/default-config.js index e8695fbb4..02e7c0368 100644 --- a/lib/default-config.js +++ b/lib/default-config.js @@ -158,12 +158,13 @@ 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] @@ -171,6 +172,7 @@ module.exports = { * @type Object */ snippetOptions: { + async: true, whitelist: [], blacklist: [], rule: { diff --git a/lib/templates/script-tags.tmpl b/lib/templates/script-tags.tmpl index ec1bdfd8f..0c3c945ac 100644 --- a/lib/templates/script-tags.tmpl +++ b/lib/templates/script-tags.tmpl @@ -1,3 +1,3 @@ \ No newline at end of file + document.write(" diff --git a/test/specs/e2e/e2e.options.script.async.js b/test/specs/e2e/e2e.options.script.async.js new file mode 100644 index 000000000..d992b6e93 --- /dev/null +++ b/test/specs/e2e/e2e.options.script.async.js @@ -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(); + }); + }); +});