Skip to content

docs: add headers option example #3809

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

Merged
merged 1 commit into from
Sep 8, 2021
Merged
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
29 changes: 29 additions & 0 deletions examples/headers/function/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# headers option as a function

Adds headers to all responses.

**webpack.config.js**

```js
module.exports = {
// ...
devServer: {
headers: () => {
return { "X-Custom-Header": ["key1=value1", "key2=value2"] };
},
},
};
```

To run this example use the following command:

```console
npx webpack serve --open
```

## What should happen

1. The script should open `http://localhost:8080/`.
2. You should see the text on the page itself change to read `Success!`.
3. Open the console in your browser's devtools and select the _Network_ tab.
4. Find `main.js`. The response headers should contain `X-Custom-Header: key1=value1` and `X-Custom-Header: key2=value2`.
6 changes: 6 additions & 0 deletions examples/headers/function/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

const target = document.querySelector("#target");

target.classList.add("pass");
target.innerHTML = "Success!";
15 changes: 15 additions & 0 deletions examples/headers/function/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

// our setup function adds behind-the-scenes bits to the config that all of our
// examples need
const { setup } = require("../../util");

module.exports = setup({
context: __dirname,
entry: "./app.js",
devServer: {
headers: () => {
return { "X-Custom-Header": ["key1=value1", "key2=value2"] };
},
},
});
29 changes: 29 additions & 0 deletions examples/headers/object/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# headers option as an object

Adds headers to all responses.

**webpack.config.js**

```js
module.exports = {
// ...
devServer: {
headers: {
"X-Custom-Header": "yes",
},
},
};
```

To run this example use the following command:

```console
npx webpack serve --open
```

## What should happen

1. The script should open `http://localhost:8080/`.
2. You should see the text on the page itself change to read `Success!`.
3. Open the console in your browser's devtools and select the _Network_ tab.
4. Find `main.js`. The response headers should contain `X-Custom-Header: yes`.
6 changes: 6 additions & 0 deletions examples/headers/object/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

const target = document.querySelector("#target");

target.classList.add("pass");
target.innerHTML = "Success!";
15 changes: 15 additions & 0 deletions examples/headers/object/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

// our setup function adds behind-the-scenes bits to the config that all of our
// examples need
const { setup } = require("../../util");

module.exports = setup({
context: __dirname,
entry: "./app.js",
devServer: {
headers: {
"X-Custom-Header": "yes",
},
},
});