Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/live-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ function staticServer(root, onTagMissedCallback) {
var reqpath = isFile ? "" : url.parse(req.url).pathname;
var hasNoOrigin = !req.headers.origin;
var injectCandidates = [
new RegExp("</body>", "i"),
new RegExp("</svg>"),
new RegExp("</head>", "i")
{ regex: new RegExp("</body>", "i"), lastOccurrence: true },
{ regex: new RegExp("</svg>"), lastOccurrence: true },
{ regex: new RegExp("</head>", "i"), lastOccurrence: false }
];

// extraInjectCandidates = extraInjectCandidates || [];
Expand All @@ -88,11 +88,18 @@ function staticServer(root, onTagMissedCallback) {
if (hasNoOrigin && (possibleExtensions.indexOf(x) > -1)) {
// TODO: Sync file read here is not nice, but we need to determine if the html should be injected or not
var contents = fs.readFileSync(filepath, "utf8");
var lastIndex = -1;
for (var i = 0; i < injectCandidates.length; ++i) {
match = injectCandidates[i].exec(contents);
if (match) {
injectTag = match[0];
break;
var allMatches = [...contents.matchAll(injectCandidates[i].regex)];
if (allMatches.length > 0) {
var selectedMatch = injectCandidates[i].lastOccurrence
? allMatches[allMatches.length - 1]
: allMatches[0];
var matchIndex = selectedMatch.index;
if (matchIndex > lastIndex) {
lastIndex = matchIndex;
injectTag = selectedMatch[0];
}
}
}

Expand Down