How to cache a file in CloudFlare, but not in browsers? I tried “Cache Everything”. I tried “Cloudflare-CDN-Cache-Control”

huangapple go评论59阅读模式
英文:

How to cache a file in CloudFlare, but not in browsers? I tried “Cache Everything”. I tried “Cloudflare-CDN-Cache-Control”

问题

我有一个SPA,域名下的所有文件都是静态的。我使用CloudFlare来缓存这些文件,以减轻服务器负担。

我的目标是让CloudFlare缓存所有文件,直到我的部署脚本告诉它文件已更改。客户端浏览器应该看到我从服务器设置的任何cache-control标头,这可能是对于我生成的具有URL中哈希的文件(如JS/CSS/图像文件)的cache-control: public,max-age=31536000,以及对于我的index.html文件的cache-control: no-cache(由于这是SPA,由于此原因从许多URL提供,不能在其中包含哈希)。

所有这些都完美地运作,除了index.html。对于index.html,似乎Cloudflare看到no-cache标头并决定无法缓存我的index.html文件,所以我在我的GET请求标头中看到“cf-cache-status: DYNAMIC”,它会为每个请求命中我的服务器。

文档说“使用页面规则来实现自定义缓存选项。” 阅读这方面的信息,显然我可以使用页面规则向所有文件添加Cache Everything设置。我已经这样做了,但现在我在index.html的标头中看到cf-cache-status: EXPIRED,这意味着每个请求仍然会命中我的服务器,我猜测是因为它仍然看到我的cache-control: no-cache并决定无法提供缓存的页面。所以“Cache Everything”没有帮助。

进一步阅读,我看到了Cloudflare-CDN-Cache-Control标头,这似乎正是我想要的。我可以为CloudFlare和浏览器设置不同的cache-control值。所以我设置了Cloudflare-CDN-Cache-Control: public,max-age=86400,希望CloudFlare将开始缓存我的index.html文件。但似乎并不重要 - 在我的浏览器中,我仍然看到cf-cache-status: DYNAMIC。而我不知道如何进一步调试这个问题。

到目前为止,我觉得我可能只是在根本上做错了什么。感觉像我想要的是几乎任何SPA都想要的东西,所以可能有一种简单而可靠的方法来实现它。但是从搜索答案来看,我没有找到任何清晰的信息。我漏掉了什么?我只是希望服务器从我的服务器发送cache-control值到浏览器,并且Cloudflare本身缓存所有文件,直到我的部署脚本告诉它清除缓存。

英文:

I have an SPA where all the files on my domain are static. I'm using CloudFlare to cache them to reduce load on my server.

My goal would be to have CloudFlare cache all my files until my deploy script tells it a file changed. And client browsers should see whatever cache-control header I set from my server, which would be something like cache-control: public,max-age=31536000 for files I'm generating with hashes in URLs (like JS/CSS/image files) and cache-control: no-cache for my index.html file (which is served from many URLs due to this being a SPA, and cannot have a hash in it).

All of this works perfectly, except index.html. With index.html, it seems that Cloudflare sees the no-cache header and decides it cannot cache my index.html file, so I see "cf-cache-status: DYNAMIC" in my GET request header and it hits my server for every request.

The documentation says "Use Page Rules to implement custom caching options." Reading up on that, apparently I can use Page Rules to add the Cache Everything setting to all files. I did that, but now I see cf-cache-status: EXPIRED in the headers for index.html, which means it's still hitting my server every request, I guess because it still sees my cache-control: no-cache and decides it can't serve the cached page. So "Cache Everything" is not helping.

Reading further, I see the Cloudflare-CDN-Cache-Control header, which seems to be exactly what I want. I can set different cache-control values for CloudFlare and for browsers. So I set Cloudflare-CDN-Cache-Control: public,max-age=86400 in the hopes that CloudFlare will start caching my index.html file. But it seems to not matter - in my browser I still see cf-cache-status: DYNAMIC. And I don't know how I can debug this further.

At this point, I feel like I'm probably just doing something fundamentally wrong. Feels like what I want is the same thing nearly any SPA would want, so there is probably an easy and reliable way to achieve it. But from Googling for answers, I don't find anything clear. What am I missing? I just want the cache-control values form my server to be sent to the browser, and for Cloudflare itself to cache all my files until my deploy script tells it to clear the cache.

答案1

得分: 0

当然,在发布后的5分钟内,我弄清楚了!关键是将“Cache Everything”和“Cloudflare-CDN-Cache-Control”一起设置,并确保将“Browser Cache TTL”设置为“Respect Existing Headers”。

英文:

Of course 5 minutes after posting, I figured it out! The key is to set "Cache Everything" and Cloudflare-CDN-Cache-Control together, and also make sure the "Browser Cache TTL" setting is set to "Respect Existing Headers".

huangapple
  • 本文由 发表于 2023年2月24日 03:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定