Is it possible to alter the headers of the Request
object that is received by the fetch
event?
Two attempts:
Modify existing headers:
self.addEventListener('fetch', function (event) { event.request.headers.set("foo", "bar"); event.respondWith(fetch(event.request)); });
Fails with
Failed to execute 'set' on 'Headers': Headers are immutable
.Create new
Request
object:self.addEventListener('fetch', function (event) { var req = new Request(event.request, { headers: { "foo": "bar" } }); event.respondWith(fetch(req)); });
Fails with
Failed to construct 'Request': Cannot construct a Request with a Request whose mode is 'navigate' and a non-empty RequestInit.
(See also How to alter the headers of a Response?)