Cloudflare workers - possible to stop all caching?

Hello.

I have an issue with workers. I want to display a different page for visitors with different language headers, but it seems that Cloudflare cache my page and then shows an incorrect page sometimes.

I have set a page rule
Cache level: bypass

Anything else I can do to make the system never cache my page?

I got a fresh response:

  1. In Firefox, I visited that URL and got “your language: en”
  2. I edited my request’s Accept Language to es-ES
  3. New response is “your language: es”

I don’t see any Cloudflare cache headers in the responses.

1 Like

Hey. I don’t see any cache headers as well, but is it possible that Cloudflare proxy sometimes cache the response?

The worker executes fine, but the answer is sometimes cached. I sent a link to another computer and it displayed page incorrectly.

1 Like

Cloudflare doesn’t cache php pages by default. Unless you have a cache everything page rule in place. If you have a cache everything rule in place you probably want to disable that given your use case.

2 Likes

You mentioned Workers. Is there a Worker being used? Can we see the code? Chris is correct that php pages are not going to be cached by default, but perhaps the Worker is fetching an HTML page that is being cached?

1 Like
addEventListener('fetch', event => {
  
  event.respondWith(handleRequest(event))
})


  var translation = new Object();

  //var translation;
  translation.en = {title:"English text", body:"Body text", button:"I agree"};
  translation.es = {title:"Spanish text", body:"Spanish body", button:"I agree"};


async function handleRequest(event) {

  request = event.request;

  var languageHeader = event.request.headers.get('Accept-Language');
  var lang_code = languageHeader.substring(0, 2);

  var title = translation [lang_code].title;
  var body = translation [lang_code].body;
  var button = translation [lang_code].button;

  // here I replace translated text into HTML
  var re = /WEBPAGETITLE/gi;
  someHTML = someHTML.replace(re, title);

  re = /WEBPAGEBODY/gi;///
  someHTML = someHTML.replace(re, body);  


  const init = {
    headers: {
      'content-type': 'text/html;charset=UTF-8'   
    },
  }
  
 


  response = new Response(someHTML, init);
  return response;
}
addEventListener('fetch', event => {
  return event.respondWith(handleRequest(event.request))
})
var someHTML =  `

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>WEBPAGETITLE</title>
 
</head>
<body>

<h1>WEBPAGEBODY</h1>

</body>
</html>

`

This was is a small example of a worker who gave me an error.