This topic provides sample code that shows how to modify headers.

Sample code

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const response = await fetch(request)

  // Copy the body of the response.
  const newResponse = new Response(response.body, response)

  // Add headers.
  newResponse.headers.append("custom-ER-add", "ER header")

  // Remove headers.
  newResponse.headers.delete("custom-ER-delete")
  newResponse.headers.delete("custom2-ER-delete")

  // Modify headers.
  newResponse.headers.set("custom-ER-reset", "ER header")

  return newResponse
}

Result

After EdgeRoutine copies the body of the response, adds headers, removes headers, or modifies headers, new responses are returned to the client.

Example 8