diff --git a/operations/development-server.js b/operations/development-server.js index 3c733cc..38a0bcf 100644 --- a/operations/development-server.js +++ b/operations/development-server.js @@ -18,17 +18,21 @@ function monkeyPatchEnd(res) { let prefix; 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")) { - let contentLength = res.getHeader("content-length"); - - if (contentLength != null) { - // Compensate for the additional bytes introduced by our injected script tag - res.setHeader("content-length", parseInt(contentLength) + reloadClientTag.length); + if (typeHeader != null && typeHeader.startsWith("text/html")) { + let contentLength = res.getHeader("content-length"); + + if (contentLength != null) { + // Compensate for the additional bytes introduced by our injected script tag + 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