Skip to content

Commit bdec0da

Browse files
authored
Merge pull request #1012 from mdmccarley89/fix-1011
Fix issues with multiple inline svgs in live-server injection
2 parents 8c205ee + 9772b3a commit bdec0da

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/live-server/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ function staticServer(root) {
4848
var hasNoOrigin = !req.headers.origin;
4949
var injectCandidates = [
5050
new RegExp('</body>', 'i'),
51-
new RegExp('</svg>'),
51+
new RegExp('</svg>', 'g'),
5252
new RegExp('</head>', 'i'),
5353
];
5454
var injectTag = null;
55+
var injectCount = 0;
5556

5657
function directory() {
5758
var pathname = url.parse(req.originalUrl).pathname;
@@ -62,15 +63,16 @@ function staticServer(root) {
6263

6364
function file(filepath /*, stat*/) {
6465
var x = path.extname(filepath).toLocaleLowerCase(),
65-
match,
66+
matches,
6667
possibleExtensions = ['', '.html', '.htm', '.xhtml', '.php', '.svg'];
6768
if (hasNoOrigin && possibleExtensions.indexOf(x) > -1) {
6869
// TODO: Sync file read here is not nice, but we need to determine if the html should be injected or not
6970
var contents = fs.readFileSync(filepath, 'utf8');
7071
for (var i = 0; i < injectCandidates.length; ++i) {
71-
match = injectCandidates[i].exec(contents);
72-
if (match) {
73-
injectTag = match[0];
72+
matches = contents.match(injectCandidates[i]);
73+
injectCount = matches && matches.length || 0;
74+
if (injectCount) {
75+
injectTag = matches[0];
7476
break;
7577
}
7678
}
@@ -94,7 +96,7 @@ function staticServer(root) {
9496
function inject(stream) {
9597
if (injectTag) {
9698
// We need to modify the length given to browser
97-
var len = INJECTED_CODE.length + res.getHeader('Content-Length');
99+
var len = INJECTED_CODE.length * injectCount + res.getHeader('Content-Length');
98100
res.setHeader('Content-Length', len);
99101
var originalPipe = stream.pipe;
100102
stream.pipe = function(resp) {

0 commit comments

Comments
 (0)