feat: do not modify the Content-Type twice when sending strings (#6991)

* fix: improve content-type handling in res.send method

* fix: ensure content-type is a string before setting charset in res.send

* fix: refactor content-type handling in res.send to use const and improve clarity

* Apply suggestion from @bjohansebas

* docs: update History.md
This commit is contained in:
Sebastian Beltran
2026-01-19 09:56:53 -05:00
committed by GitHub
parent 5a4568abfe
commit a479419b16
2 changed files with 11 additions and 14 deletions

View File

@@ -2,7 +2,11 @@
## 🚀 Improvements
- 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)
* 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
=======================

View File

@@ -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'