diff --git a/History.md b/History.md index 689c08a9..0c5334c1 100644 --- a/History.md +++ b/History.md @@ -2,7 +2,11 @@ ## 🚀 Improvements -- Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding ``, ``, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167) +* Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding `<!DOCTYPE html>`, `<title>`, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167) + +## ⚡ Performance + +* Avoid duplicate Content-Type header processing in `res.send()` when sending string responses without an explicit Content-Type header - by [@bjohansebas](https://github.com/bjohansebas) in [#6991](https://github.com/expressjs/express/pull/6991) 5.2.1 / 2025-12-01 ======================= diff --git a/lib/response.js b/lib/response.js index 731afb78..f965e539 100644 --- a/lib/response.js +++ b/lib/response.js @@ -126,7 +126,6 @@ res.send = function send(body) { var chunk = body; var encoding; var req = this.req; - var type; // settings var app = this.app; @@ -134,7 +133,12 @@ res.send = function send(body) { switch (typeof chunk) { // string defaulting to html case 'string': - if (!this.get('Content-Type')) { + encoding = 'utf8'; + const type = this.get('Content-Type'); + + if (typeof type === 'string') { + this.set('Content-Type', setCharset(type, 'utf-8')); + } else { this.type('html'); } break; @@ -153,17 +157,6 @@ res.send = function send(body) { break; } - // write strings in utf-8 - if (typeof chunk === 'string') { - encoding = 'utf8'; - type = this.get('Content-Type'); - - // reflect this in content-type - if (typeof type === 'string') { - this.set('Content-Type', setCharset(type, 'utf-8')); - } - } - // determine if ETag should be generated var etagFn = app.get('etag fn') var generateETag = !this.get('ETag') && typeof etagFn === 'function'