Basic compression (GZIP) to JSON asset

I’ve set a page rule to “cache everything” under this URL path, and I have this asset URL:

https://go.tallyfy.com/resources/i18n/en/global.json?cacheBuster=1594682181904

I can always remove the query string, but remember the rule is set to “Cache Everything”.

It’s not using gzip to send back a compressed response. Why is this?

To show the state of this - see:

In addition, how do I also compress other file formats and send them back zipped beyond JSON like .woff2, etc?

The origin is Cloudfront and the bucket for S3 (all static assets) is set to compress.

I don’t see a content-type header when I request that resource. I see it in your screenshot, but not when I ‘curl’ it or request it from my browser.

As for fonts, the support seems rather limited for some reason. Maybe they’re already compressed, like images (also not on list).

Got it - thank you!

How would I detect for a “.json?” in the path of the request via a Cloudflare Worker, and then add the content type manually?

I’m not 100% certain/untested, but something along the lines of;

const url = require("url");
const path = require("path");

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

async function handleRequest(request) {
  let response = await fetch(request);
  response = new Response(response.body, response);

  const { pathname } = url.parse(request.url);

  if (path.parse(pathname).ext === ".json") {
    response.headers.set("content-type", "application/json");
  }

  return response;
}

Although, if this is being served from S3, has the content-type been set there on the object?

1 Like

@sdayman @rsclarke I’ve now used a CF Worker to modify and set the content-type - thank you.

I’m still not seeing Gzip or Brotli compression - are you? On the same asset?

https://go.tallyfy.com/resources/i18n/en/global.json?cacheBuster=1594682181904


I used:

// If cachable - return straight away and cache response
            if (isCacheable === true) {               
                let cachedResponse = await fetch(request, {
                cf: {
                      // Tell Cloudflare's CDN to always cache 
                      cacheTtl: CACHING_TTL,
                      cacheEverything: true,
                      polish: 'lossy' 
                    },
                });
                // Due to a bug with JSON content type from S3 - we set it manually if JSON
                cachedResponse = new Response(response.body, response);
                if (u.href.toString().includes(".json?")) {
                    cachedResponse.headers.set("content-type", "application/json");
                }
                return cachedResponse;
            }

I’m seeing Brotli:

1 Like

That’s awesome - thank you! Might have been a temporary snafu

2 Likes