Skip to content

Commit 1014aad

Browse files
authored
Fix multiple <svg> and missing content issue (#2722)
live-server injects code into every <svg> occurrence but only increases the Content-Length value once. This commit fixes this issue by counting the number of <svg> and increasing the Content-Length accordingly, ensuring that all content is properly sent.
1 parent 9c33eb4 commit 1014aad

File tree

1 file changed

+9
-6
lines changed
  • packages/cli/src/lib/live-server

1 file changed

+9
-6
lines changed

packages/cli/src/lib/live-server/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ function staticServer(root) {
6868
if (req.method !== 'GET' && req.method !== 'HEAD') return next();
6969
const reqpath = isFile ? "" : parse(req.url).pathname;
7070
var hasNoOrigin = !req.headers.origin;
71-
var injectCandidates = [ new RegExp("</body>", "i"), new RegExp("</svg>"), new RegExp("</head>", "i")];
71+
var injectCandidates = [ new RegExp("</body>", "i"), new RegExp("</svg>", "gi"), new RegExp("</head>", "i")];
7272
var injectTag = null;
73+
var injectCount = 0;
7374

7475
function directory() {
7576
var pathname = parse(req.url).pathname;
@@ -79,15 +80,16 @@ function staticServer(root) {
7980
}
8081

8182
function file(filepath /*, stat*/) {
82-
var x = path.extname(filepath).toLocaleLowerCase(), match,
83+
var x = path.extname(filepath).toLocaleLowerCase(), matches,
8384
possibleExtensions = [ "", ".html", ".htm", ".xhtml", ".php", ".svg" ];
8485
if (hasNoOrigin && (possibleExtensions.indexOf(x) > -1)) {
8586
// TODO: Sync file read here is not nice, but we need to determine if the html should be injected or not
8687
var contents = fs.readFileSync(filepath, "utf8");
8788
for (var i = 0; i < injectCandidates.length; ++i) {
88-
match = injectCandidates[i].exec(contents);
89-
if (match) {
90-
injectTag = match[0];
89+
matches = contents.match(injectCandidates[i]);
90+
injectCount = matches && matches.length || 0;
91+
if (injectCount > 0) {
92+
injectTag = matches[0];
9193
break;
9294
}
9395
}
@@ -107,7 +109,8 @@ function staticServer(root) {
107109
function inject(stream) {
108110
if (injectTag) {
109111
// We need to modify the length given to browser
110-
var len = INJECTED_CODE.length + res.getHeader('Content-Length');
112+
var contentLength = Number(res.getHeader('Content-Length')) || 0;
113+
var len = INJECTED_CODE.length * injectCount + contentLength;
111114
res.setHeader('Content-Length', len);
112115
var originalPipe = stream.pipe;
113116
stream.pipe = function(resp) {

0 commit comments

Comments
 (0)