Skip to content

Commit 2e138e6

Browse files
arturovtAndrewKushnir
authored andcommitted
refactor(common): prevent duplicating Content-Type header (angular#59518)
Drops some bytes by moving `Content-Type` into a variable, which is then minified to something like `var b="Content-Type"` and reused in all the places. PR Close angular#59518
1 parent af99c6c commit 2e138e6

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

packages/common/http/src/fetch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {Observable, Observer} from 'rxjs';
1111

1212
import {HttpBackend} from './backend';
1313
import {HttpHeaders} from './headers';
14-
import {ACCEPT_HEADER, HttpRequest, X_REQUEST_URL_HEADER} from './request';
14+
import {ACCEPT_HEADER, CONTENT_TYPE_HEADER, HttpRequest, X_REQUEST_URL_HEADER} from './request';
1515
import {
1616
HTTP_STATUS_CODE_OK,
1717
HttpDownloadProgressEvent,
@@ -174,7 +174,7 @@ export class FetchBackend implements HttpBackend {
174174
// Combine all chunks.
175175
const chunksAll = this.concatChunks(chunks, receivedLength);
176176
try {
177-
const contentType = response.headers.get('Content-Type') ?? '';
177+
const contentType = response.headers.get(CONTENT_TYPE_HEADER) ?? '';
178178
body = this.parseBody(request, chunksAll, contentType);
179179
} catch (error) {
180180
// Body loading or parsing failed
@@ -263,11 +263,11 @@ export class FetchBackend implements HttpBackend {
263263
}
264264

265265
// Auto-detect the Content-Type header if one isn't present already.
266-
if (!req.headers.has('Content-Type')) {
266+
if (!req.headers.has(CONTENT_TYPE_HEADER)) {
267267
const detectedType = req.detectContentTypeHeader();
268268
// Sometimes Content-Type detection fails.
269269
if (detectedType !== null) {
270-
headers['Content-Type'] = detectedType;
270+
headers[CONTENT_TYPE_HEADER] = detectedType;
271271
}
272272
}
273273

packages/common/http/src/request.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ function isUrlSearchParams(value: any): value is URLSearchParams {
7777
return typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams;
7878
}
7979

80+
/**
81+
* `Content-Type` is an HTTP header used to indicate the media type
82+
* (also known as MIME type) of the resource being sent to the client
83+
* or received from the server.
84+
*/
85+
export const CONTENT_TYPE_HEADER = 'Content-Type';
86+
8087
/**
8188
* `X-Request-URL` is a custom HTTP header used in older browser versions,
8289
* including Firefox (< 32), Chrome (< 37), Safari (< 8), and Internet Explorer,

packages/common/http/src/xhr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {switchMap} from 'rxjs/operators';
1414
import {HttpBackend} from './backend';
1515
import {RuntimeErrorCode} from './errors';
1616
import {HttpHeaders} from './headers';
17-
import {ACCEPT_HEADER, HttpRequest, X_REQUEST_URL_HEADER} from './request';
17+
import {ACCEPT_HEADER, CONTENT_TYPE_HEADER, HttpRequest, X_REQUEST_URL_HEADER} from './request';
1818
import {
1919
HTTP_STATUS_CODE_NO_CONTENT,
2020
HTTP_STATUS_CODE_OK,
@@ -102,11 +102,11 @@ export class HttpXhrBackend implements HttpBackend {
102102
}
103103

104104
// Auto-detect the Content-Type header if one isn't present already.
105-
if (!req.headers.has('Content-Type')) {
105+
if (!req.headers.has(CONTENT_TYPE_HEADER)) {
106106
const detectedType = req.detectContentTypeHeader();
107107
// Sometimes Content-Type detection fails.
108108
if (detectedType !== null) {
109-
xhr.setRequestHeader('Content-Type', detectedType);
109+
xhr.setRequestHeader(CONTENT_TYPE_HEADER, detectedType);
110110
}
111111
}
112112

0 commit comments

Comments
 (0)