I'm fetching static assets (images, pdfs, etc) from AWS S3 and displaying their download progresses to the client using the service worker api. To do this, I'm reading the "content-length" header in the response. In Chrome Canary (77.0.3821.0) this works fine, but in Firefox (version 67.0) and Chrome (version 75.0.3770.80) there is no "content-length" header in the response.
This answer on SO was helpful in setting the request.mode
to "cors" which provided me with some initial success, but so far this only seems to work in Chrome Canary.
function respondWithProgressMonitor(clientId, response) {
for (const key of response.headers.keys()) {
console.log(key); // returns only "content-type" and "last-modified" on chrome and firefox, but in Chrome Canary includes "content-length"
}
const contentLength = response.headers.get('content-length');
// ...
}
function fetchWithProgressMonitor(event) {
const request = new Request(event.request.clone(), {
mode: 'cors',
credentials: 'omit',
});
return fetch(request).then(response => respondWithProgressMonitor(event.clientId, response));
}
My S3 bucket CORS configuration rules should be exposing the header unless I'm doing something wrong.
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
<ExposeHeader>Content-Length</ExposeHeader>
</CORSRule>
</CORSConfiguration>
I'm not sure why the content-length header is exposed in the Canary response but not in the current Chrome release or Firefox. Are there some other options I need to include in my request to get back a "content-length" header in the response? Any insights or threads to follow are much appreciated.