[Pages] How to prevent caching 404s when using _headers

Answer these questions to help the Community help you get started.

What is the domain name?
maxroll.gg

Have you searched for an answer?
yes

When you tested your domain using the Cloudflare Diagnostic Center, what were the results?

Describe the issue you are having:
We are using the _headers file to cache build assets:

/build/*
Cache-Control: public, max-age=31536000, s-maxage=31536000

/assets/*
Cache-Control: public, max-age=31536000, s-maxage=31536000

However, it seems that even 404s are being cached, resulting in a broken website for many users.

What error message or number are you receiving?
Cached 404

What steps have you taken to resolve the issue?
Not solved

Cache rules, page rules etc. Because this is on the “origin” we cannot respect the origin headers.

Was the site working with SSL prior to adding it to Cloudflare?
Yes

What are the steps to reproduce the error:

  1. Add rules in _headers to add caching to static build assets
  2. Try to access a non-existent resource

Have you tried from another browser and/or incognito mode?

Yes, does this even in cURL

2 Likes

I am experiencing a similar issue with Cloudflare pages configured with a custom domain where 404 are cached for days. Note this is using Cloudflare pages, the origin is Cloudflare servers. Sad this post got no answer since June.

I am in contact with support. It seems that a global domain cache purge is the only way to solve the issue for now. That’s really not serious.

1 Like

Not really helpful but we solved it by migrating away from Cloudflare pages and are now just using Kubernetes. All problems went away. To be fair we ran into many issues but especially “worker ran out of resources” issues.