Skip to content

Commit 503d290

Browse files
feat: added mimeTypeDefault option (#1527)
1 parent f5f033b commit 503d290

File tree

6 files changed

+79
-11
lines changed

6 files changed

+79
-11
lines changed

README.md

+19-11
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,18 @@ See [below](#other-servers) for an example of use with fastify.
6060

6161
## Options
6262

63-
| Name | Type | Default | Description |
64-
| :-----------------------------------------: | :-----------------------: | :-------------------------------------------: | :------------------------------------------------------------------------------------------------------------------- |
65-
| **[`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. |
67-
| **[`index`](#index)** | `Boolean\|String` | `index.html` | If `false` (but not `undefined`), the server will not respond to requests to the root URL. |
68-
| **[`mimeTypes`](#mimetypes)** | `Object` | `undefined` | Allows to register custom mime types or extension mappings. |
69-
| **[`publicPath`](#publicpath)** | `String` | `output.publicPath` (from a configuration) | The public path that the middleware is bound to. |
70-
| **[`stats`](#stats)** | `Boolean\|String\|Object` | `stats` (from a configuration) | Stats options object or preset name. |
71-
| **[`serverSideRender`](#serversiderender)** | `Boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. |
72-
| **[`writeToDisk`](#writetodisk)** | `Boolean\|Function` | `false` | Instructs the module to write files to the configured location on disk as specified in your `webpack` configuration. |
73-
| **[`outputFileSystem`](#outputfilesystem)** | `Object` | [`memfs`](https://github.com/streamich/memfs) | Set the default file system which will be used by webpack as primary destination of generated files. |
63+
| Name | Type | Default | Description |
64+
| :-----------------------------------------: | :--------: | :-------------------------------------------: | :--------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
65+
| **[`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. |
67+
| **[`index`](#index)** | `Boolean\ | String` | `index.html` | If `false` (but not `undefined`), the server will not respond to requests to the root URL. |
68+
| **[`mimeTypes`](#mimetypes)** | `Object` | `undefined` | Allows to register custom mime types or extension mappings. |
69+
| **[`mimeTypeDefault`](#mimetypedefault)** | `String` | `undefined` | Allows to register a default mime type when we can't determine the content type. |
70+
| **[`publicPath`](#publicpath)** | `String` | `output.publicPath` (from a configuration) | The public path that the middleware is bound to. |
71+
| **[`stats`](#stats)** | `Boolean\ | String\| Object` | `stats` (from a configuration) | Stats options object or preset name. |
72+
| **[`serverSideRender`](#serversiderender)** | `Boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. |
73+
| **[`writeToDisk`](#writetodisk)** | `Boolean\ | Function` | `false` | Instructs the module to write files to the configured location on disk as specified in your `webpack` configuration. |
74+
| **[`outputFileSystem`](#outputfilesystem)** | `Object` | [`memfs`](https://github.com/streamich/memfs) | Set the default file system which will be used by webpack as primary destination of generated files. |
7475

7576
The middleware accepts an `options` Object. The following is a property reference for the Object.
7677

@@ -164,6 +165,13 @@ eg. `mimeTypes: { phtml: 'text/html' }`.
164165

165166
Please see the documentation for [`mime-types`](https://github.com/jshttp/mime-types) for more information.
166167

168+
### mimeTypeDefault
169+
170+
Type: `String`
171+
Default: `undefined`
172+
173+
This property allows a user to register a default mime type when we can't determine the content type.
174+
167175
### publicPath
168176

169177
Type: `String`

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const noop = () => {};
8080
* @template {ServerResponse} ResponseInternal
8181
* @typedef {Object} Options
8282
* @property {{[key: string]: string}} [mimeTypes]
83+
* @property {string | undefined} [mimeTypeDefault]
8384
* @property {boolean | ((targetPath: string) => boolean)} [writeToDisk]
8485
* @property {string} [methods]
8586
* @property {Headers<RequestInternal, ResponseInternal>} [headers]

src/middleware.js

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ function wrapper(context) {
146146
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
147147
if (contentType) {
148148
setHeaderForResponse(res, "Content-Type", contentType);
149+
} else if (context.options.mimeTypeDefault) {
150+
setHeaderForResponse(
151+
res,
152+
"Content-Type",
153+
context.options.mimeTypeDefault
154+
);
149155
}
150156
}
151157

src/options.json

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
"link": "https://github.com/webpack/webpack-dev-middleware#mimetypes",
77
"type": "object"
88
},
9+
"mimeTypeDefault": {
10+
"description": "Allows a user to register a default mime type when we can't determine the content type.",
11+
"link": "https://github.com/webpack/webpack-dev-middleware#mimetypedefault",
12+
"type": "string"
13+
},
914
"writeToDisk": {
1015
"description": "Allows to write generated files on disk.",
1116
"link": "https://github.com/webpack/webpack-dev-middleware#writetodisk",

test/middleware.test.js

+46
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,52 @@ describe.each([
21632163
});
21642164
});
21652165

2166+
describe("mimeTypeDefault option", () => {
2167+
describe('should set the correct value for "Content-Type" header to unknown MIME type', () => {
2168+
beforeAll((done) => {
2169+
const outputPath = path.resolve(__dirname, "./outputs/basic");
2170+
const compiler = getCompiler({
2171+
...webpackConfig,
2172+
output: {
2173+
filename: "bundle.js",
2174+
path: outputPath,
2175+
},
2176+
});
2177+
2178+
instance = middleware(compiler, {
2179+
mimeTypeDefault: "text/plain",
2180+
});
2181+
2182+
app = framework();
2183+
app.use(instance);
2184+
2185+
listen = listenShorthand(done);
2186+
2187+
req = request(app);
2188+
2189+
instance.context.outputFileSystem.mkdirSync(outputPath, {
2190+
recursive: true,
2191+
});
2192+
instance.context.outputFileSystem.writeFileSync(
2193+
path.resolve(outputPath, "file.unknown"),
2194+
"welcome"
2195+
);
2196+
});
2197+
2198+
afterAll(close);
2199+
2200+
it('should return the "200" code for the "GET" request to "file.html"', async () => {
2201+
const response = await req.get("/file.unknown");
2202+
2203+
expect(response.statusCode).toEqual(200);
2204+
expect(
2205+
response.headers["content-type"].startsWith("text/plain")
2206+
).toBe(true);
2207+
expect(response.text).toEqual("welcome");
2208+
});
2209+
});
2210+
});
2211+
21662212
describe("watchOptions option", () => {
21672213
describe("should work without value", () => {
21682214
let compiler;

types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export = wdm;
5858
* @template {ServerResponse} ResponseInternal
5959
* @typedef {Object} Options
6060
* @property {{[key: string]: string}} [mimeTypes]
61+
* @property {string | undefined} [mimeTypeDefault]
6162
* @property {boolean | ((targetPath: string) => boolean)} [writeToDisk]
6263
* @property {string} [methods]
6364
* @property {Headers<RequestInternal, ResponseInternal>} [headers]
@@ -164,6 +165,7 @@ type Options<
164165
[key: string]: string;
165166
}
166167
| undefined;
168+
mimeTypeDefault?: string | undefined;
167169
writeToDisk?: boolean | ((targetPath: string) => boolean) | undefined;
168170
methods?: string | undefined;
169171
headers?: Headers<RequestInternal, ResponseInternal>;

0 commit comments

Comments
 (0)