Skip to content

feat: allow headers will be function (#749) #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -111,15 +133,15 @@ 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.
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.
Expand All @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down