Skip to content

Commit 255b2f0

Browse files
authored
docs: add devMiddleware example (#3806)
1 parent 0535c41 commit 255b2f0

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

examples/dev-middleware/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# devMiddleware option
2+
3+
Provide options to [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) which handles webpack assets.
4+
5+
**webpack.config.js**
6+
7+
```js
8+
module.exports = {
9+
// ...
10+
devServer: {
11+
devMiddleware: {
12+
index: true,
13+
headers: {
14+
"X-Custom-Header": "yes",
15+
},
16+
},
17+
},
18+
};
19+
```
20+
21+
To run this example use the following command:
22+
23+
```console
24+
npx webpack serve --open
25+
```
26+
27+
## What should happen
28+
29+
1. The script should open `http://localhost:8080/`.
30+
2. You should see the text on the page itself change to read `Success!`.
31+
3. Open the console in your browser's devtools and select the _Network_ tab.
32+
4. Find `main.js`. The response headers should contain `X-Custom-Header: yes`.
33+
34+
Now update `webpack.config.js` with [`index: false`](https://github.com/webpack/webpack-dev-middleware#index), this will tell the server to not respond to requests to the root URL.
35+
36+
Now close and restart the server with:
37+
38+
```console
39+
npx webpack serve --open
40+
```
41+
42+
## What should happen
43+
44+
1. The script should open `http://localhost:8080/`.
45+
2. You should see the `Cannot GET /` text on the page.

examples/dev-middleware/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const { setup } = require("../util");
6+
7+
module.exports = setup({
8+
context: __dirname,
9+
entry: "./app.js",
10+
devServer: {
11+
devMiddleware: {
12+
index: false,
13+
headers: {
14+
"X-Custom-Header": "yes",
15+
},
16+
},
17+
},
18+
});

0 commit comments

Comments
 (0)