Skip to content

Commit

Permalink
fix: append to head if body not present yet - fixes #2031 (#2041)
Browse files Browse the repository at this point in the history
Co-authored-by: Shane Osbourne <sosbourne@duckduckgo.com>
  • Loading branch information
shakyShane and Shane Osbourne authored May 17, 2023
1 parent f91440e commit 62d906e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/options.snippetOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
*
* Install:
* npm install browser-sync
*
* Run:
* node <yourfile.js>
*
* This example shows how you can place the snippet anywhere.
*/

"use strict";

var path = require("path");
var browserSync = require("../packages/browser-sync").create();
var cwd = path.join(__dirname, "..");
var fixtures_dir = path.join(cwd, "packages/browser-sync/test/fixtures");

browserSync.init({
files: [path.join(fixtures_dir, "css/*.css")],
server: fixtures_dir,
snippetOptions: {
rule: {
match: /<\/head>/i,
fn: function (snippet, match) {
return snippet + match;
},
},
},
});
2 changes: 2 additions & 0 deletions packages/browser-sync/templates/script-tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
script.src = '%script%'.replace("HOST", location.hostname);
if (document.body) {
document.body.appendChild(script);
} else if (document.head) {
document.head.appendChild(script);
}
} catch (e) {
console.error("Browsersync: could not append script tag", e);
Expand Down

0 comments on commit 62d906e

Please # to comment.