However, the 404 response is still being cached for 4 hours. After reviewing my Cloudflare settings, I found that the only place where a 4-hour cache duration is configured is under Browser Cache TTL in the caching configuration, which applies to all domains.
Could this setting be causing the 404 JS files to be cached for 4 hours?
For files with a 200 status, caching is controlled by the origin server, where I have set a 7-day cache, and that is working as expected.
Can someone please help me understand why 404 JS files are also being cached for 4 hours, and how I can prevent them from being cached?
I can see cf-cache-status:HIT and cache-control:max-age=14400 for 4 hours in the Web browser of the user, meaning those 404s are cached since .js extension is cached by default at Cloudflare:
Have you tried clearing the cache at Cloudflare and then visiting the URL again using a different Web browser or in Incognito mode?
I’d suggest you to temporary, enable the Development mode until you sort this out, or Pause Cloudflare:
Use the “Pause Cloudflare on Site” option from the Overview tab for your domain at dash.cloudflare.com .
The link is in the lower right corner of that page.
Here’s a refined version of your message with improved clarity and grammar:
When I clear the cache in Cloudflare and access the URL from a different browser, it works fine.
However, I’m wondering why Cloudflare is caching the JS files for 4 hours, even though I have explicitly set them not to be cached on my origin server, as shown below:
location ~* \.(js|css)$ {
try_files $uri @default_js_css_fallback;
expires 7d;
if_modified_since off;
add_header Cache-Control "public, max-age=604800, no-transform";
}
location @default_js_css_fallback {
return 404 "Resource not found"; # Return a 404 status with an optional message
add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0" always;
add_header X-Frame-Options "SAMEORIGIN";
}
Can anyone figure out why Cloudflare is still caching the 404 JS files for 4 hours despite my Cache-Control settings?
Do i need to create a new Cache Rule for the status code 404 and set the Edge Cache TTL to 5 minutes (lower time frame) to overcome this ?