diff --git a/README.md b/README.md index a403ea19e..bccdf7842 100644 --- a/README.md +++ b/README.md @@ -63,29 +63,51 @@ The middleware accepts an `options` Object. The following is a property referenc ### methods -Type: `Array` +Type: `Array` Default: `[ 'GET', 'HEAD' ]` This property allows a user to pass the list of HTTP request methods accepted by the middleware\*\*. ### headers -Type: `Object` +Type: `Object` Default: `undefined` This property allows a user to pass custom HTTP headers on each request. eg. `{ "X-Custom-Header": "yes" }` +or + +```js +webpackDevMiddleware(compiler, { + headers: () => { + return { + 'Last-Modified': new Date(), + } + } +}) +``` + +or + +```js +webpackDevMiddleware(compiler, { + headers: (req, res, context) => { + res.setHeader('Last-Modified', new Date()) + } +}) +``` + ### index -Type: `Boolean|String` +Type: `Boolean|String` Default: `index.html` If `false` (but not `undefined`), the server will not respond to requests to the root URL. ### mimeTypes -Type: `Object` +Type: `Object` Default: `undefined` This property allows a user to register custom mime types or extension mappings. @@ -111,7 +133,7 @@ Stats options object or preset name. ### serverSideRender -Type: `Boolean` +Type: `Boolean` Default: `undefined` Instructs the module to enable or disable the server-side rendering mode. @@ -119,7 +141,7 @@ Please see [Server-Side Rendering](#server-side-rendering) for more information. ### writeToDisk -Type: `Boolean|Function` +Type: `Boolean|Function` Default: `false` If `true`, the option will instruct the module to write files to the configured location on disk as specified in your `webpack` config file. @@ -145,7 +167,7 @@ middleware(compiler, { ### outputFileSystem -Type: `Object` +Type: `Object` Default: [memfs](https://github.com/streamich/memfs) Set the default file system which will be used by webpack as primary destination of generated files. diff --git a/src/middleware.js b/src/middleware.js index e22393db1..d24583715 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -42,7 +42,16 @@ export default function wrapper(context) { async function processRequest() { const filename = getFilenameFromUrl(context, req.url); - const { headers } = context.options; + let { headers } = context.options; + + if (typeof headers === 'function') { + headers = headers(req, res, context) + if (headers && typeof headers !== 'object') { + headers = null; + console.warn('webpack-dev-middleware >> ', 'The heasers return must be the object'); + } + } + let content; if (!filename) { diff --git a/src/options.json b/src/options.json index 0969a0154..d22559626 100644 --- a/src/options.json +++ b/src/options.json @@ -25,7 +25,15 @@ } }, "headers": { - "type": "object" + "description": "Allows a user to pass custom HTTP headers on each request.", + "anyOf": [ + { + "type": "object" + }, + { + "instanceof": "Function" + } + ] }, "publicPath": { "description": "The `publicPath` specifies the public URL address of the output files when referenced in a browser.",