|
| 1 | +--- |
| 2 | +pcx-content-type: how-to |
| 3 | +--- |
| 4 | + |
| 5 | +# Add custom HTTP headers |
| 6 | + |
| 7 | +Cloudflare provides HTTP header customization for Pages projects through Cloudflare Workers [serverless functions](https://www.cloudflare.com/learning/serverless/what-is-serverless/). You may want to add, remove, or alter HTTP headers for security or performance reasons. |
| 8 | + |
| 9 | +If you have not deployed a Worker before, get started with our [tutorial](https://developers.cloudflare.com/workers/get-started/guide). For the purpose of this tutorial, accomplish steps one ("Sign up for a Workers account") through four ("Generate a new project") before returning to this page. |
| 10 | + |
| 11 | +Before continuing, ensure that your Cloudflare Pages project is connected to a [custom domain](https://developers.cloudflare.com/pages/getting-started#adding-a-custom-domain). |
| 12 | + |
| 13 | +## Writing a Workers function |
| 14 | + |
| 15 | +Workers functions are written in [JavaScript](https://www.cloudflare.com/learning/serverless/serverless-javascript/). When a Worker makes a request to a Cloudflare Pages application, it will receive a response. The response a Worker receives is immutable, meaning it cannot be changed. In order to add, delete, or alter headers, clone the response and modify the headers on a new `Response` instance. Return the new response to the browser with your desired header changes. An example of this is shown below: |
| 16 | + |
| 17 | +```js |
| 18 | +--- |
| 19 | +header: Setting custom headers with a Workers function |
| 20 | +--- |
| 21 | +addEventListener('fetch', event => { |
| 22 | + event.respondWith(handleRequest(event.request)) |
| 23 | +}) |
| 24 | + |
| 25 | +async function handleRequest(request) { |
| 26 | + const response = await fetch(request) |
| 27 | + |
| 28 | + // Clone the response so that it's no longer immutable |
| 29 | + const newResponse = new Response(response.body, response) |
| 30 | + |
| 31 | + // Add a custom header with a value |
| 32 | + newResponse.headers.append("x-workers-hello", "Hello from Cloudflare Workers") |
| 33 | + |
| 34 | + // Delete headers |
| 35 | + newResponse.headers.delete("x-header-to-delete") |
| 36 | + newResponse.headers.delete("x-header2-to-delete") |
| 37 | + |
| 38 | + // Adjust the value for an existing header |
| 39 | + newResponse.headers.set("x-header-to-change", "NewValue") |
| 40 | + |
| 41 | + return newResponse |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +<Aside> |
| 46 | + |
| 47 | +If you would like to skip writing this file yourself, you can use our `custom-headers-example` [template](https://github.com/signalnerve/custom-headers-example) to generate a new Workers function with [wrangler](https://developers.cloudflare.com/workers/cli-wrangler/install-update), the Workers CLI tool. |
| 48 | + |
| 49 | +```sh |
| 50 | +--- |
| 51 | +header: Generating a serverless function with wrangler |
| 52 | +--- |
| 53 | +$ wrangler generate projectname https://github.com/cloudflare/custom-headers-example |
| 54 | +``` |
| 55 | + |
| 56 | +</Aside> |
| 57 | + |
| 58 | +## Deploying a Workers function |
| 59 | + |
| 60 | +To operate your Workers function alongside your Pages application, deploy it to the same custom domain as your Pages application. To do this, update the `wrangler.toml` file in your project with your account and zone details: |
| 61 | + |
| 62 | +```toml |
| 63 | +--- |
| 64 | +filename: wrangler.toml |
| 65 | +highlight: [4,6,7] |
| 66 | +--- |
| 67 | +name = "custom-headers-example" |
| 68 | +type = "javascript" |
| 69 | + |
| 70 | +account_id = "FILL-IN-YOUR-ACCOUNT-ID" |
| 71 | +workers_dev = false |
| 72 | +route = "FILL-IN-YOUR-WEBSITE.com/*" |
| 73 | +zone_id = "FILL-IN-YOUR-ZONE-ID" |
| 74 | +``` |
| 75 | + |
| 76 | +If you do not know how to find your Account ID and Zone ID, refer to [our guide](https://developers.cloudflare.com/workers/get-started/guide#7-configure-your-project-for-deployment). |
| 77 | + |
| 78 | +Once you have configured your `wrangler.toml`, run `wrangler publish` in your terminal to deploy your Worker: |
| 79 | + |
| 80 | +```sh |
| 81 | +$ wrangler publish |
| 82 | +``` |
| 83 | + |
| 84 | +After you have deployed your Worker, your desired HTTP header adjustments will take effect. While the Worker is deployed, you should continue to see the content from your Pages application as normal. |
0 commit comments