diff --git a/src/content/docs/cache/interaction-cloudflare-products/r2.mdx b/src/content/docs/cache/interaction-cloudflare-products/r2.mdx index 6dfcad8527c1d5..4d0df2c3687e73 100644 --- a/src/content/docs/cache/interaction-cloudflare-products/r2.mdx +++ b/src/content/docs/cache/interaction-cloudflare-products/r2.mdx @@ -2,7 +2,7 @@ pcx_content_type: how-to title: Enable cache in an R2 bucket sidebar: - order: 2 + order: 3 head: - tag: title content: Enable cache in an R2 bucket diff --git a/src/content/docs/cache/interaction-cloudflare-products/waf-snippets.mdx b/src/content/docs/cache/interaction-cloudflare-products/waf-snippets.mdx index b015804c624db1..300cdaf147b631 100644 --- a/src/content/docs/cache/interaction-cloudflare-products/waf-snippets.mdx +++ b/src/content/docs/cache/interaction-cloudflare-products/waf-snippets.mdx @@ -2,7 +2,7 @@ pcx_content_type: how-to title: Control cache access with WAF and Snippets sidebar: - order: 3 + order: 4 head: - tag: title content: Control cache access with WAF and Snippets diff --git a/src/content/docs/cache/interaction-cloudflare-products/workers-cache-rules.mdx b/src/content/docs/cache/interaction-cloudflare-products/workers-cache-rules.mdx new file mode 100644 index 00000000000000..428228eb6e2a2f --- /dev/null +++ b/src/content/docs/cache/interaction-cloudflare-products/workers-cache-rules.mdx @@ -0,0 +1,39 @@ +--- +pcx_content_type: how-to +title: How Workers interact with Cache Rules +sidebar: + order: 2 +head: + - tag: title + content: How Workers interact with Cache Rules + +--- + +Your Workers script can override [cache rule](/cache/how-to/cache-rules/) behavior, whether it is applied to a zone using Cloudflare or a zone that is not proxied through Cloudflare. + +For example, if there is a cache rule configured to bypass cache for `example.com/foo`, but your Workers script sets `cacheEverything: true`, the script's setting will take precedence, and the request will be cached. The same applies if the request is made to a non-Cloudflare zone — the Worker's `cacheEverything` setting will still override. + +## Precedence order + +Cache behavior is determined by the following order of precedence: + +1. Workers script settings +2. Cache rules +3. Page rules + +Cache rules override page rule settings, and Workers scripts override cache rules. Among rules at the same level, the one with the highest specificity takes priority. + +## Compatibility flags + +This override behavior is controlled by [compatibility flags](/workers/configuration/compatibility-flags/): + +- For the Fetch API: `request_cf_overrides_cache_rules` +- For the Cache API: `cache_api_request_cf_overrides_cache_rules` + +These flags must be enabled to allow Workers scripts to override cache rules. + +If your Worker has a compatibility date of 2025-04-02 or earlier, these flags are not enabled by default. In that case, cache settings defined in the Worker will not override existing cache rules. + +### Example (Older compatibility date) + +If a cache rule is configured to bypass cache for `example.com/foo`, and a Worker with a compatibility date of `2025-04-02` or earlier tries to set `cacheEverything: true`, the cache rule will take effect — and the response will not be cached. \ No newline at end of file