I have a website that is hosted on GitHub Pages, fully static HTML that loads reasonably fast. I have been trying to cut the TTFB Time (as well as FCP, LCP and Fully Loaded).
Trying to focus on TTFB now, I made several attempts.
- Using Cloudflare in DNS Only mode, I'm getting a consistent TTFB that's 99% below 30ms.
- Using Cloudflare in Proxy mode with page rules to "Cache Everything", the TTFB is always above 100ms.
Then I tried using Cloudflare workers, the idea was simple:
- a. Fetch the current site content into a worker.
- b. cache the content
- c. Serve the cached content
Surprisingly, I could get nothing better than 80ms, though I was expecting a much less TTFB than my 1st trial (DNS-Only mode), on average, the TTFB is somewhere between 100ms and 150ms; I'm testing the Worker's url (random-name.organization-name.workers.dev). I suspect there is something not right with my worker code, Here is what I tried so far (Mostly from the docs)
const url = "https://domain.extension"
async function gatherResponse(response) {
const { headers } = response
return await response.text()
}
async function handleRequest() {
const init = {
headers: {
"content-type": "text/html;charset=UTF-8",
},
cf: {
cacheTtl: 50000,
cacheEverything: true,
},
}
const response = await fetch(url, init)
const results = await gatherResponse(response)
return new Response(results, init)
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest())
})
Could there be a way to cache everything and let the worker serve that faster than the DNS-Only Option? I tried upgrading my Cloudflare plan and also got a paid worker hoping to get few more milliseconds saved hopelessly.