Set Response Cache-Control on Staled Content

#1
I want all requests to use both cache (application cache-control 1 hour TTL) and staled cache (1 year). This ensures all the requests get served with cache/staled content while fetching for a new copy in the backend. I also have CloudFront in front of LiteSpeed servers. I want to set the response cache-control to no-cache when staled content is served so CloudFront can get a copy of the staled content and serving it without caching. This ensures the second request to the same URL is coming back to the litespeed server to get the freshed content. Is there anyway to detect if the response is a staled copy and set/change the response header conditionally?
 
#2
You seem to have a few fundamental misunderstandings. `Cache-Control` is intended for the browser cache to handle static assets (CSS, JS, images, etc.); the main document must never be cached there. Caching dynamic assets in the browser is arguably the biggest mistake you could possibly make! Ideally, you should set the `Cache-Control` TTL to one year, or 31,536,000 seconds.

Using both LSCache and a CDN cache for the main document at the same time is the next biggest mistake you could make. One cache plus another cache does not equal two caches! Therefore, use CloudFront only for static assets, never for dynamic ones.

cdn_2.png

Is there anyway to detect if the response is a staled copy and set/change the response header conditionally?
NO!
 
#3
Thank you for the response. You are right, ideally the cache should be on the CDN and never hit the original server.

We're running Drupal sites on multiple servers and some pages could take a few seconds to generate and load. There's a module to purge pages on the CDN. When the cache is purged on the CDN, there will be a lot requests coming to the origin and temporary freeze the origin servers. My idea is caching all public requests on the origin for a few seconds on the regular cache and set staled cache to one year. This set up will allow cached pages to be served right away in milliseconds and kick off a backend request to refresh the staled cache.

The problem right now is the CDN will cache the staled content for another year because it did not know the staled content was served. This is why I need change the response cache control to tell the CDN to use the staled content for that single request only and don't cache it (set s-maxage or maxage to no cache). On the second request, it should come back to the origin again to get the refreshed content. Since there's no way to notify staled content served and turning on both origin cache and CDN will cache the pages for another year even after CDN purge.
 
Top