Cloudflare 503 Status Maintenance Page worker compressed HTML response?

I am playing with CF worker code to create a standalone HTML maintenance page which returns 503 HTTP status code. But looks like gzip compression response only happens with 200 HTTP status code. With 503 HTTP status code, the response returned by the CF worker does not support gzip compression and returns uncompressed size HTML page.

@harris so curious if there’s a way to allow 503 HTTP status code responses to support gzip compression ?

with 200 HTTP status code

curl -4I https://domain.com/
HTTP/2 200 
date: Tue, 10 Mar 2020 01:06:58 GMT
content-type: text/html;charset=UTF-8
content-length: 18431
set-cookie: __cfduid=d82cafd6a021f5f7e7398aa344f2a407d1583802418; expires=Thu, 09-Apr-20 01:06:58 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 571922dd1c14749f-IAD

curl gzip compressed response returns 12.20KB size

curltest gzip https://domain.com/
URI: https://domain.com/ (gzip)
Uncompressed size : 17.99 KiB
Compressed size   : 12.20 KiB

But with 503 HTTP status response

curl -4I https://domain.com/
HTTP/2 503 
date: Tue, 10 Mar 2020 01:07:09 GMT
content-type: text/html;charset=UTF-8
content-length: 18431
set-cookie: __cfduid=d7765c3903050caf0c31a27cf3c6177b61583802429; expires=Thu, 09-Apr-20 01:07:09 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 571923225b41e758-EWR

curl gzip compressed request will return uncompressed response size = 17.99KB

curltest gzip https://domain.com/
URI: https://domain.com/ (gzip)
Uncompressed size : 17.99 KiB
Compressed size   : 17.99 KiB

Hi @eva2000, it should be possible to gzip any response by adding a Content-Encoding: gzip header in the worker. If that doesn’t work, could you share the script you’re using?

I am using modified version of Send Raw HTML template example at https://developers.cloudflare.com/workers/templates

Tried adding accept-encoding header but still uncompressed only for 503 HTTP status code responses though

async function handleRequest(request) {
  const init = {
    headers: {
      'content-type': 'text/html;charset=UTF-8',
      'accept-encoding': 'gzip',
    },
    status: 503,
  }
  const ip = request.headers.get("cf-connecting-ip")
  // trusted IP 1.2.3.4
  if (ip !== '1.2.3.4') {
    return new Response(someHTML, init)
  }
  else // allow trusted IP onto site
  {
    return fetch(request)
  }
}
addEventListener('fetch', event => {
  return event.respondWith(handleRequest(event.request))
})
const someHTML =  `<!DOCTYPE html>
<html lang="en">
<head>
...
</body>
</html>
`
curl -4I https://domain.com/
HTTP/2 503 
date: Tue, 10 Mar 2020 03:20:15 GMT
content-type: text/html;charset=UTF-8
content-length: 18431
set-cookie: __cfduid=dd279f6ad396ea6865bbddd23fde2b7371583810415; expires=Thu, 09-Apr-20 03:20:15 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax
accept-encoding: gzip
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 5719e619ab3de851-EWR
curltest gzip https://domain.com/
URI: https://domain.com/ (gzip)
Uncompressed size : 17.99 KiB
Compressed size   : 17.99 KiB

I think you want Content-Encoding, not Accept-Encoding.

Accept-Encoding is a request header specifying which encodings an HTTP client can understand. Content-Encoding specifies what encoding the message body actually uses, which is what you need here.

1 Like

whoops, Content-Encoding fixed the issue. Thanks !

  const init = {
    headers: {
      'content-type': 'text/html;charset=UTF-8',
      'content-encoding': 'gzip',
      'vary': 'accept-encoding',
    },
    status: 503,
  }
curltest gzip https://domain.com/
URI: https://domain.com/ (gzip)
Uncompressed size : 18.06 KiB
Compressed size   : 12.26 KiB