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
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"
},