Skip to content

Commit e77a41b

Browse files
authored
docs: add headers option example (#3809)
1 parent ac74bca commit e77a41b

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

examples/headers/function/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# headers option as a function
2+
3+
Adds headers to all responses.
4+
5+
**webpack.config.js**
6+
7+
```js
8+
module.exports = {
9+
// ...
10+
devServer: {
11+
headers: () => {
12+
return { "X-Custom-Header": ["key1=value1", "key2=value2"] };
13+
},
14+
},
15+
};
16+
```
17+
18+
To run this example use the following command:
19+
20+
```console
21+
npx webpack serve --open
22+
```
23+
24+
## What should happen
25+
26+
1. The script should open `http://localhost:8080/`.
27+
2. You should see the text on the page itself change to read `Success!`.
28+
3. Open the console in your browser's devtools and select the _Network_ tab.
29+
4. Find `main.js`. The response headers should contain `X-Custom-Header: key1=value1` and `X-Custom-Header: key2=value2`.

examples/headers/function/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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
headers: () => {
12+
return { "X-Custom-Header": ["key1=value1", "key2=value2"] };
13+
},
14+
},
15+
});

examples/headers/object/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# headers option as an object
2+
3+
Adds headers to all responses.
4+
5+
**webpack.config.js**
6+
7+
```js
8+
module.exports = {
9+
// ...
10+
devServer: {
11+
headers: {
12+
"X-Custom-Header": "yes",
13+
},
14+
},
15+
};
16+
```
17+
18+
To run this example use the following command:
19+
20+
```console
21+
npx webpack serve --open
22+
```
23+
24+
## What should happen
25+
26+
1. The script should open `http://localhost:8080/`.
27+
2. You should see the text on the page itself change to read `Success!`.
28+
3. Open the console in your browser's devtools and select the _Network_ tab.
29+
4. Find `main.js`. The response headers should contain `X-Custom-Header: yes`.

examples/headers/object/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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
headers: {
12+
"X-Custom-Header": "yes",
13+
},
14+
},
15+
});

0 commit comments

Comments
 (0)