Caching static HTML failes while using webpagetest.org

Hey together,

we are using a cloudflare worker to cache static html requests via the Worker Cache API.
We are using a smilar script like this one: https://github.com/stephan13360/cloudflare-worker/blob/master/cache/index.js

I have added some console logs to better understand whats going on. You see that in the requests below.

So our origin request is altered to have 7 days of max-age and gets “put” into the cache.
But: If we check the url via webpagetest.org I can see that the cache API is returning that no cached version was found and the script therefore returns the origin response and after that creates a new version of the request in cache API.

I was wondering why this is happening. WebPageTest sends a “HEAD” Request beforehand of their 3 GET requests. Could this be the reason why the cache is cleared for that particular URL?

Hope you can help me with that :slight_smile:

Thanks a lot. By the way: The community here is amazing!

HEAD Request:

{
  "outcome": "ok",
  "scriptName": "entrypagecacheworker",
  "diagnosticsChannelEvents": [],
  "exceptions": [
    {
      "name": "TypeError",
      "message": "Cannot cache response to non-GET request.",
      "timestamp": 1692995987570
    }
  ],
  "logs": [
    {
      "message": [
        "Cache Headers before altering: \"no-cache, max-age=0\""
      ],
      "level": "log",
      "timestamp": 1692995987570
    },
    {
      "message": [
        "Cache Headers after altering: \"public, s-maxage=604800, max-age=0\""
      ],
      "level": "log",
      "timestamp": 1692995987570
    },
    {
      "message": [
        "Request was not cached in Worker Cache API! I grabbed the origin response."
      ],
      "level": "log",
      "timestamp": 1692995987570
    }
  ],
  "eventTimestamp": 1692995987179,
  "event": {
    "request": {
      "url": "https://www.handytick.de/vertragsverlaengerung",
      "method": "HEAD",
      "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip",
        "cf-connecting-ip": "3.131.207.60",
        "cf-ipcountry": "US",
        "cf-ray": "7fc6a577cc222d6d",
        "cf-visitor": "{\"scheme\":\"https\"}",
        "connection": "Keep-Alive",
        "host": "www.handytick.de",
        "user-agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; PTST 2.295)",
        "x-forwarded-proto": "https",
        "x-real-ip": "3.131.207.60"
      },

First GET Request from WebPageTest:

{
  "outcome": "ok",
  "scriptName": "entrypagecacheworker",
  "diagnosticsChannelEvents": [],
  "exceptions": [],
  "logs": [
    {
      "message": [
        "Cache Headers before altering: \"no-cache, max-age=0\""
      ],
      "level": "log",
      "timestamp": 1692995993278
    },
    {
      "message": [
        "Cache Headers after altering: \"public, s-maxage=604800, max-age=0\""
      ],
      "level": "log",
      "timestamp": 1692995993278
    },
    {
      "message": [
        "Request was not cached in Worker Cache API! I grabbed the origin response."
      ],
      "level": "log",
      "timestamp": 1692995993278
    }
  ],
  "eventTimestamp": 1692995992908,
  "event": {
    "request": {
      "url": "https://www.handytick.de/vertragsverlaengerung",
      "method": "GET",
      "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip",
        "accept-language": "en-US,en;q=0.9",
        "cf-connecting-ip": "54.163.76.76",
        "cf-ipcountry": "US",
        "cf-ray": "7fc6a59b9c832030",
        "cf-visitor": "{\"scheme\":\"https\"}",
        "connection": "Keep-Alive",
        "host": "www.handytick.de",
        "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"116\", \"Google Chrome\";v=\"116\"",
        "sec-ch-ua-mobile": "?1",
        "sec-ch-ua-platform": "\"Android\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "cross-site",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Linux; Android 8.1.0; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36 PTST/230405.214311",
        "x-forwarded-proto": "https",
        "x-real-ip": "54.163.76.76"
      },

Second GET request from WebPageTest:

{
  "outcome": "ok",
  "scriptName": "entrypagecacheworker",
  "diagnosticsChannelEvents": [],
  "exceptions": [],
  "logs": [
    {
      "message": [
        "Cache Headers before altering: \"no-cache, max-age=0\""
      ],
      "level": "log",
      "timestamp": 1692995994492
    },
    {
      "message": [
        "Cache Headers after altering: \"public, s-maxage=604800, max-age=0\""
      ],
      "level": "log",
      "timestamp": 1692995994492
    }
  ],
  "eventTimestamp": 1692995994108,
  "event": {
    "request": {
      "url": "https://www.handytick.de/vertragsverlaengerung",
      "method": "GET",
      "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip",
        "accept-language": "en-US,en;q=0.9",
        "cf-connecting-ip": "54.163.76.76",
        "cf-ipcountry": "US",
        "cf-ray": "7fc6a5a31cd620a8",
        "cf-visitor": "{\"scheme\":\"https\"}",
        "connection": "Keep-Alive",
        "host": "www.handytick.de",
        "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"116\", \"Google Chrome\";v=\"116\"",
        "sec-ch-ua-mobile": "?1",
        "sec-ch-ua-platform": "\"Android\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "cross-site",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Linux; Android 8.1.0; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36 PTST/230405.214311",
        "x-forwarded-proto": "https",
        "x-real-ip": "54.163.76.76"
      },

I now have excluded “HEAD” requests in the worker but still the first request is not cached even if my request 1 minute before the webpagetest.org request got response from cache.

There isn’t just one cache; it’s per-datacenter, as noted in the docs. The testing site you’re using is probably hitting a different POP from your original request, so that POP doesn’t yet have the file in its cache.

You can see what POP you’re hitting by requesting /cdn-cgi/trace on any Cloudflare-proxied host. Also, you should be able to see what POP your requests are hitting by looking at the HTTP response headers, for something like:

cf-ray: 7fc6f23ecb3c2051-IAD

In that example, IAD is the POP the response came from.

1 Like

awww man I have overseen that. And also WebPagetest was requesting from different locations in Virginia USA. So I got confused by that too.

Thanks!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.