How to get the content-length of the response from a request with fetch()

The server should expose the header using Access-Control-Expose-Headers on the server side:

Access-Control-Expose-Headers: Content-Length

@sideshowbarker comment explains why you can see the header in the network panel of the browser, but receive null when you do response.headers.get("Content-Length"):

Just because your browser receives the header and you can see the header in devtools doesn’t mean your frontend JavaScript code can see it. If the response from a cross-origin request doesn’t have an Access-Control-Expose-Headers: Content-Length response — as indicated in this answer — then it’s your browser itself that will block your code from being able to access the header. For a cross-origin request, your browser will only expose a particular response header to your frontend JavaScript code if the Access-Control-Expose-Headers value contains the name of that particular header.

You can see it working by copy/pasting this to the console:

fetch("//stackoverflow.com").then(response => console.log(response.headers.get("content-length")))

Note that the return value of Headers.get will be a bytestring, so you will have to cast it to a number to use it in a numerical expression:

Number(response.headers.get("content-length")) > 0