Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 0c13253

Browse files
committed
perf: remove headers option
1 parent d065b3e commit 0c13253

File tree

5 files changed

+0
-345
lines changed

5 files changed

+0
-345
lines changed

README.md

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ See [below](#other-servers) for an example of use with fastify.
6363
| Name | Type | Default | Description |
6464
| :---------------------------------------------: | :---------------------------: | :-------------------------------------------: | :------------------------------------------------------------------------------------------------------------------- |
6565
| **[`methods`](#methods)** | `Array` | `[ 'GET', 'HEAD' ]` | Allows to pass the list of HTTP request methods accepted by the middleware |
66-
| **[`headers`](#headers)** | `Array\|Object\|Function` | `undefined` | Allows to pass custom HTTP headers on each request. |
6766
| **[`index`](#index)** | `Boolean\|String` | `index.html` | If `false` (but not `undefined`), the server will not respond to requests to the root URL. |
6867
| **[`etag`](#tag)** | `boolean\| "weak"\| "strong"` | `undefined` | Enable or disable etag generation. |
6968
| **[`publicPath`](#publicpath)** | `String` | `output.publicPath` (from a configuration) | The public path that the middleware is bound to. |
@@ -81,70 +80,6 @@ Default: `[ 'GET', 'HEAD' ]`
8180

8281
This property allows a user to pass the list of HTTP request methods accepted by the middleware\*\*.
8382

84-
### headers
85-
86-
Type: `Array|Object|Function`
87-
Default: `undefined`
88-
89-
This property allows a user to pass custom HTTP headers on each request.
90-
eg. `{ "X-Custom-Header": "yes" }`
91-
92-
or
93-
94-
```js
95-
webpackDevMiddleware(compiler, {
96-
headers: () => {
97-
return {
98-
"Last-Modified": new Date(),
99-
};
100-
},
101-
});
102-
```
103-
104-
or
105-
106-
```js
107-
webpackDevMiddleware(compiler, {
108-
headers: (req, res, context) => {
109-
res.setHeader("Last-Modified", new Date());
110-
},
111-
});
112-
```
113-
114-
or
115-
116-
```js
117-
webpackDevMiddleware(compiler, {
118-
headers: [
119-
{
120-
key: "X-custom-header",
121-
value: "foo",
122-
},
123-
{
124-
key: "Y-custom-header",
125-
value: "bar",
126-
},
127-
],
128-
});
129-
```
130-
131-
or
132-
133-
```js
134-
webpackDevMiddleware(compiler, {
135-
headers: () => [
136-
{
137-
key: "X-custom-header",
138-
value: "foo",
139-
},
140-
{
141-
key: "Y-custom-header",
142-
value: "bar",
143-
},
144-
],
145-
});
146-
```
147-
14883
### index
14984

15085
Type: `Boolean|String`

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ const noop = () => {};
102102
* @typedef {Object} Options
103103
* @property {boolean | ((targetPath: string) => boolean)} [writeToDisk]
104104
* @property {string[]} [methods]
105-
* @property {Headers<RequestInternal, ResponseInternal>} [headers]
106105
* @property {NonNullable<Configuration["output"]>["publicPath"]} [publicPath]
107106
* @property {Configuration["stats"]} [stats]
108107
* @property {boolean} [serverSideRender]

src/middleware.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -447,33 +447,6 @@ function wrapper(context) {
447447
let len = size;
448448
let offset = 0;
449449

450-
// Send logic
451-
let { headers } = context.options;
452-
453-
if (typeof headers === "function") {
454-
headers = /** @type {NormalizedHeaders} */ (headers(req, res, context));
455-
}
456-
457-
/**
458-
* @type {{key: string, value: string | number}[]}
459-
*/
460-
const allHeaders = [];
461-
462-
if (typeof headers !== "undefined") {
463-
if (!Array.isArray(headers)) {
464-
// eslint-disable-next-line guard-for-in
465-
for (const name in headers) {
466-
allHeaders.push({ key: name, value: headers[name] });
467-
}
468-
469-
headers = allHeaders;
470-
}
471-
472-
headers.forEach((header) => {
473-
res.setHeader(header.key, header.value);
474-
});
475-
}
476-
477450
if (!res.getHeader("Content-Type")) {
478451
// content-type name(like application/javascript; charset=utf-8) or false
479452
const contentType = mime.contentType(path.extname(filename));

test/middleware.test.js

Lines changed: 0 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,6 @@ function get404ContentTypeHeader(name) {
153153
}
154154
}
155155

156-
function applyTestMiddleware(name, middlewares) {
157-
middlewares.push({
158-
route: "/file.jpg",
159-
fn: (req, res) => {
160-
// Express API
161-
if (res.send) {
162-
res.send("welcome");
163-
}
164-
// Connect API
165-
else {
166-
res.setHeader("Content-Type", "text/html");
167-
res.end("welcome");
168-
}
169-
},
170-
});
171-
172-
return middlewares;
173-
}
174-
175156
function parseHttpDate(date) {
176157
const timestamp = date && Date.parse(date);
177158

@@ -3519,237 +3500,6 @@ describe.each([
35193500
});
35203501
});
35213502

3522-
describe("headers option", () => {
3523-
describe("works with object", () => {
3524-
beforeEach(async () => {
3525-
const compiler = getCompiler(webpackConfig);
3526-
3527-
[server, req, instance] = await frameworkFactory(
3528-
name,
3529-
framework,
3530-
compiler,
3531-
{
3532-
headers: { "X-nonsense-1": "yes", "X-nonsense-2": "no" },
3533-
},
3534-
{
3535-
setupMiddlewares: (middlewares) => {
3536-
applyTestMiddleware(name, middlewares);
3537-
3538-
return middlewares;
3539-
},
3540-
},
3541-
);
3542-
});
3543-
3544-
afterEach(async () => {
3545-
await close(server, instance);
3546-
});
3547-
3548-
it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
3549-
const response = await req.get(`/bundle.js`);
3550-
3551-
expect(response.statusCode).toEqual(200);
3552-
expect(response.headers["x-nonsense-1"]).toEqual("yes");
3553-
expect(response.headers["x-nonsense-2"]).toEqual("no");
3554-
});
3555-
3556-
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
3557-
const res = await req.get("/file.jpg");
3558-
expect(res.statusCode).toEqual(200);
3559-
expect(res.headers["X-nonsense-1"]).toBeUndefined();
3560-
expect(res.headers["X-nonsense-2"]).toBeUndefined();
3561-
});
3562-
});
3563-
3564-
describe("works with array of objects", () => {
3565-
beforeEach(async () => {
3566-
const compiler = getCompiler(webpackConfig);
3567-
3568-
[server, req, instance] = await frameworkFactory(
3569-
name,
3570-
framework,
3571-
compiler,
3572-
{
3573-
headers: [
3574-
{
3575-
key: "X-Foo",
3576-
value: "value1",
3577-
},
3578-
{
3579-
key: "X-Bar",
3580-
value: "value2",
3581-
},
3582-
],
3583-
},
3584-
{
3585-
setupMiddlewares: (middlewares) => {
3586-
applyTestMiddleware(name, middlewares);
3587-
3588-
return middlewares;
3589-
},
3590-
},
3591-
);
3592-
});
3593-
3594-
afterEach(async () => {
3595-
await close(server, instance);
3596-
});
3597-
3598-
it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
3599-
const response = await req.get(`/bundle.js`);
3600-
3601-
expect(response.statusCode).toEqual(200);
3602-
expect(response.headers["x-foo"]).toEqual("value1");
3603-
expect(response.headers["x-bar"]).toEqual("value2");
3604-
});
3605-
3606-
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
3607-
const res = await req.get("/file.jpg");
3608-
expect(res.statusCode).toEqual(200);
3609-
expect(res.headers["x-foo"]).toBeUndefined();
3610-
expect(res.headers["x-bar"]).toBeUndefined();
3611-
});
3612-
});
3613-
3614-
describe("works with function", () => {
3615-
beforeEach(async () => {
3616-
const compiler = getCompiler(webpackConfig);
3617-
3618-
[server, req, instance] = await frameworkFactory(
3619-
name,
3620-
framework,
3621-
compiler,
3622-
{
3623-
headers: () => {
3624-
return { "X-nonsense-1": "yes", "X-nonsense-2": "no" };
3625-
},
3626-
},
3627-
{
3628-
setupMiddlewares: (middlewares) => {
3629-
applyTestMiddleware(name, middlewares);
3630-
3631-
return middlewares;
3632-
},
3633-
},
3634-
);
3635-
});
3636-
3637-
afterEach(async () => {
3638-
await close(server, instance);
3639-
});
3640-
3641-
it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
3642-
const response = await req.get(`/bundle.js`);
3643-
3644-
expect(response.statusCode).toEqual(200);
3645-
expect(response.headers["x-nonsense-1"]).toEqual("yes");
3646-
expect(response.headers["x-nonsense-2"]).toEqual("no");
3647-
});
3648-
3649-
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
3650-
const res = await req.get("/file.jpg");
3651-
expect(res.statusCode).toEqual(200);
3652-
expect(res.headers["X-nonsense-1"]).toBeUndefined();
3653-
expect(res.headers["X-nonsense-2"]).toBeUndefined();
3654-
});
3655-
});
3656-
3657-
describe("works with function returning an array", () => {
3658-
beforeEach(async () => {
3659-
const compiler = getCompiler(webpackConfig);
3660-
3661-
[server, req, instance] = await frameworkFactory(
3662-
name,
3663-
framework,
3664-
compiler,
3665-
{
3666-
headers: () => [
3667-
{
3668-
key: "X-Foo",
3669-
value: "value1",
3670-
},
3671-
{
3672-
key: "X-Bar",
3673-
value: "value2",
3674-
},
3675-
],
3676-
},
3677-
{
3678-
setupMiddlewares: (middlewares) => {
3679-
applyTestMiddleware(name, middlewares);
3680-
3681-
return middlewares;
3682-
},
3683-
},
3684-
);
3685-
});
3686-
3687-
afterEach(async () => {
3688-
await close(server, instance);
3689-
});
3690-
3691-
it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
3692-
const response = await req.get(`/bundle.js`);
3693-
3694-
expect(response.statusCode).toEqual(200);
3695-
expect(response.headers["x-foo"]).toEqual("value1");
3696-
expect(response.headers["x-bar"]).toEqual("value2");
3697-
});
3698-
3699-
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
3700-
const res = await req.get("/file.jpg");
3701-
expect(res.statusCode).toEqual(200);
3702-
expect(res.headers["x-foo"]).toBeUndefined();
3703-
expect(res.headers["x-bar"]).toBeUndefined();
3704-
});
3705-
});
3706-
3707-
describe("works with headers function with params", () => {
3708-
beforeEach(async () => {
3709-
const compiler = getCompiler(webpackConfig);
3710-
3711-
[server, req, instance] = await frameworkFactory(
3712-
name,
3713-
framework,
3714-
compiler,
3715-
{
3716-
// eslint-disable-next-line no-unused-vars, no-shadow
3717-
headers: (req, res, context) => {
3718-
res.setHeader("X-nonsense-1", "yes");
3719-
res.setHeader("X-nonsense-2", "no");
3720-
},
3721-
},
3722-
{
3723-
setupMiddlewares: (middlewares) => {
3724-
applyTestMiddleware(name, middlewares);
3725-
3726-
return middlewares;
3727-
},
3728-
},
3729-
);
3730-
});
3731-
3732-
afterEach(async () => {
3733-
await close(server, instance);
3734-
});
3735-
3736-
it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
3737-
const response = await req.get(`/bundle.js`);
3738-
3739-
expect(response.statusCode).toEqual(200);
3740-
expect(response.headers["x-nonsense-1"]).toEqual("yes");
3741-
expect(response.headers["x-nonsense-2"]).toEqual("no");
3742-
});
3743-
3744-
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
3745-
const res = await req.get("/file.jpg");
3746-
expect(res.statusCode).toEqual(200);
3747-
expect(res.headers["X-nonsense-1"]).toBeUndefined();
3748-
expect(res.headers["X-nonsense-2"]).toBeUndefined();
3749-
});
3750-
});
3751-
});
3752-
37533503
describe("publicPath option", () => {
37543504
describe('should work with "string" value', () => {
37553505
beforeAll(async () => {

0 commit comments

Comments
 (0)