Getting 304 Not Modified response on URL with workers

Hey there!

One of my websites which runs workers has strange behaviour. If I refresh the browser fast enough, after 5-6th attempt CF returns 304 no-modified response. I.e. worker is being run (I see in the logs), but the return is “not modified”, even though what worker does is inserts random number in HTML. I have other website which works normally - never gets 304 response.

I understand this is probably also related to origin cache configuration and CF cache settings, but I’d expect 304 never to be returned if worker is working on certain URL.

Any ideas what might be causing 304 return, how to debug the problem and fix it?

Thanks a lot!

Hi,

We have seeing this on our website too. Our implementation has been working fine for almost 2 years. For us though, it happens immediately (not 5-6 times after reloading).

We store a cookie on the client end that we use js to change their region (AU or NZ). The worker then returns a different origin endpoint depending on the cookie, passed to the origin as a get variable: page.php?region=XX.

In the console we can see that if we load a page, the cookie is being sent :

cookie: PHPSESSID=xx; Region=NZ

Cloudflare gives us the correct NZ page. If we change region (with js), and trigger a page reload, the new cookie is being sent in the request:

cookie: PHPSESSID=xx; Region=AU

But we get a 304 response, with NZ details on the page, not the correct AU page.

Fixed our issue. IDK how/why but our origin server must have been updated to look at ‘if-modified-since’ headers, which were being passed through to it from the initial request by the worker, and then our origin sent 304 headers back to the worker, which the worker then passed back to the browser.

Adding this line in the worker before sending the request to our origin has solved the problem for now:

request.headers.set('if-modified-since', 'Mon, 01 Jan 2022 01:01:01 GMT');

@michael42- thank you so much! You saved us many hours! Thanks a lot again!

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