From 62d906e6abde0c251550a598e159c67d53e287a4 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Wed, 17 May 2023 20:40:06 +0200 Subject: [PATCH] fix: append to head if body not present yet - fixes #2031 (#2041) Co-authored-by: Shane Osbourne --- examples/options.snippetOptions.js | 30 +++++++++++++++++++ .../browser-sync/templates/script-tags.html | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 examples/options.snippetOptions.js diff --git a/examples/options.snippetOptions.js b/examples/options.snippetOptions.js new file mode 100644 index 000000000..06d856c1e --- /dev/null +++ b/examples/options.snippetOptions.js @@ -0,0 +1,30 @@ +/** + * + * Install: + * npm install browser-sync + * + * Run: + * node + * + * 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; + }, + }, + }, +}); diff --git a/packages/browser-sync/templates/script-tags.html b/packages/browser-sync/templates/script-tags.html index 310c804b2..4cc7d9d5d 100644 --- a/packages/browser-sync/templates/script-tags.html +++ b/packages/browser-sync/templates/script-tags.html @@ -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);