From 7c08f0ec1feb594de19221c11fb93731cc518a57 Mon Sep 17 00:00:00 2001 From: Nick Aversano Date: Fri, 6 Oct 2017 11:58:12 -0400 Subject: [PATCH 1/2] memory leak fix, only reload css files --- hotModuleReplacement.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hotModuleReplacement.js b/hotModuleReplacement.js index cab2579..122a2eb 100644 --- a/hotModuleReplacement.js +++ b/hotModuleReplacement.js @@ -27,6 +27,8 @@ var getCurrentScriptUrl = function(moduleId) { } function updateCss(el, url) { + if (!el.href || !el.href.endsWith('css')) return; + var newEl = el.cloneNode(); if (!url) { url = el.href.split('?')[0]; @@ -34,6 +36,9 @@ function updateCss(el, url) { newEl.addEventListener('load', function () { el.remove(); }); + newEl.addEventListener('error', function () { + el.remove(); + }); newEl.href = url + '?' + Date.now(); el.parentNode.insertBefore(newEl, el.nextSibling); } @@ -41,8 +46,8 @@ function updateCss(el, url) { function reloadStyle(src) { var elements = document.querySelectorAll('link'); var loaded = false; - for (var i = 0, el = null; el = elements[i]; i++) { - var url = getReloadUrl(el.href, src); + for (let i = 0, el = null; el = elements[i]; i++) { + let url = getReloadUrl(el.href, src); if (url) { updateCss(el, url); loaded = true; @@ -64,7 +69,7 @@ function getReloadUrl(href, src) { function reloadAll() { var elements = document.querySelectorAll('link'); - for (var i = 0, el = null; el = elements[i]; i++) { + for (let i = 0, el = null; el = elements[i]; i++) { updateCss(el); } } From a0cef3fc4f77354f2d4c3182a27655794302bbc9 Mon Sep 17 00:00:00 2001 From: Nick Aversano Date: Fri, 6 Oct 2017 12:53:13 -0400 Subject: [PATCH 2/2] small bug fix --- hotModuleReplacement.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hotModuleReplacement.js b/hotModuleReplacement.js index 122a2eb..1aeb7ca 100644 --- a/hotModuleReplacement.js +++ b/hotModuleReplacement.js @@ -27,12 +27,13 @@ var getCurrentScriptUrl = function(moduleId) { } function updateCss(el, url) { - if (!el.href || !el.href.endsWith('css')) return; - - var newEl = el.cloneNode(); if (!url) { url = el.href.split('?')[0]; } + + if (!url || !url.endsWith('.css')) return; + + var newEl = el.cloneNode(); newEl.addEventListener('load', function () { el.remove(); });