Don't inject the autoreload tag when headers have already been sent

master
Sven Slootweg 3 years ago
parent 1d8a34e86d
commit dc3749565c

@ -18,17 +18,21 @@ function monkeyPatchEnd(res) {
let prefix; let prefix;
if (originalChunk == null || typeof originalChunk === "string" || Buffer.isBuffer(originalChunk)) { if (originalChunk == null || typeof originalChunk === "string" || Buffer.isBuffer(originalChunk)) {
let typeHeader = res.getHeader("content-type"); if (!res.headersSent) {
// If headers have already been sent, we'll just have to hope that the browser will still look at our appended tag, as we can't change the response size anymore...
// TODO: Make this more robust in the future
let typeHeader = res.getHeader("content-type");
if (typeHeader != null && typeHeader.startsWith("text/html")) { if (typeHeader != null && typeHeader.startsWith("text/html")) {
let contentLength = res.getHeader("content-length"); let contentLength = res.getHeader("content-length");
if (contentLength != null) { if (contentLength != null) {
// Compensate for the additional bytes introduced by our injected script tag // Compensate for the additional bytes introduced by our injected script tag
res.setHeader("content-length", parseInt(contentLength) + reloadClientTag.length); res.setHeader("content-length", parseInt(contentLength) + reloadClientTag.length);
}
prefix = reloadClientTag;
} }
prefix = reloadClientTag;
} }
// Reset the `end` method back to the original method; we don't need to get in the way anymore // Reset the `end` method back to the original method; we don't need to get in the way anymore

Loading…
Cancel
Save