From dc3749565c62a105761d46501ae6478ead26840e Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 7 Dec 2020 15:54:57 +0100 Subject: [PATCH] Don't inject the autoreload tag when headers have already been sent --- operations/development-server.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) 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