html-injector
html-injector copied to clipboard
Only reload when url contains path of changed file
I was seeing the injection, but also getting a reload because multiple html files are changing, so adding a check seems to take care of this problem.
I only figured out how to make this work for this config style though:
bs.use(htmlInjector, {
files: ['./**/*.html']
});
The logs below have my own debugging messages:
Before:
[BS] [HTML Injector] Responding to file change event {"event":"change","path":"docs/index.html","namespace":"HTML Injector"}
[BS] [HTML Injector] Cache items: 1
[BS] [HTML Injector] requesting http://localhost:3000/docs/maintenance/index.html
[BS] [HTML Injector] Responding to file change event {"event":"change","path":"docs/maintenance/index.html","namespace":"HTML Injector"}
[BS] [HTML Injector] Cache items: 1
[BS] [HTML Injector] requesting http://localhost:3000/docs/maintenance/index.html
[BS] [HTML Injector] 1 tasks returned
[BS] [HTML Injector] Task: TAG: DIV, INDEX: 25
[BS] [HTML Injector] 0 tasks returned, reloading instead
After:
[BS] [HTML Injector] Responding to file change event {"event":"change","path":"docs/index.html","namespace":"HTML Injector"}
[BS] [HTML Injector] Cache items: 1
[BS] [HTML Injector] requesting http://localhost:3000/docs/maintenance/index.html
[BS] [HTML Injector] Responding to file change event {"event":"change","path":"docs/maintenance/index.html","namespace":"HTML Injector"}
[BS] [HTML Injector] Cache items: 1
[BS] [HTML Injector] requesting http://localhost:3000/docs/maintenance/index.html
[BS] [HTML Injector] 1 tasks returned
[BS] [HTML Injector] Task: TAG: DIV, INDEX: 25
Perhaps this could be an option. Thoughts?
In my case I had to do something like this:
diff --git a/node_modules/bs-html-injector/index.js b/node_modules/bs-html-injector/index.js
index 8d27a1e..9617f17 100644
--- a/node_modules/bs-html-injector/index.js
+++ b/node_modules/bs-html-injector/index.js
@@ -14,6 +14,7 @@ var createDom = require("./lib/injector").createDom;
var HtmlInjector = require("./lib/html-injector");
var config = require("./lib/config");
var _ = require("./lodash.custom");
+var urlParser = require("url");
/**
* ON/OFF flag
@@ -187,7 +188,7 @@ module.exports["plugin"] = function (opts, bs) {
debug('Responding to file change event', data.namespace);
- requestNew(opts);
+ requestNew(opts, data);
}
function pluginEvent () {
@@ -214,7 +215,7 @@ module.exports["plugin"] = function (opts, bs) {
* @param {String} url
* @param {Object} opts - plugin options
*/
- function requestNew (opts) {
+ function requestNew (opts, data) {
// Remove any
var sockets = bs.io.of(bs.options.getIn(["socket", "namespace"])).sockets;
@@ -231,6 +232,13 @@ module.exports["plugin"] = function (opts, bs) {
return;
}
+ if (data) {
+ var baseDir = Array.from(bs.options.get('server').get('baseDir'))[0];
+ var pathname = urlParser.parse(url).pathname.replace('index.html', '');
+ var filePath = data.path.replace('index.html', '');
+ if (pathname !== filePath.slice(baseDir.length)) return;
+ }
+
debug("requesting %s", url);
request(getRequestOptions(url), function (error, response, body) {
@@ -268,4 +276,3 @@ module.exports.hooks = {
* @type {string}
*/
module.exports["plugin:name"] = config.PLUGIN_NAME;
-
I'm using patch-package
to avoid having to create a fork (at least for now). Thanks for pointing me in the right direction @webxl! ❤️