Expires and Cache-Control headers overwritten or not sent
-
I’m creating a plugin with a custom post type for an event. The event starts and ends at specific times and the page content for the event changes when the event starts or ends. For example, when the event starts the content will change from “The event has not started” to “The event is on now!”.
The cached event page on the server (or on the browser) needs to expire when the event starts or ends. To do that I have added expires and cache-control headers to the page.
Question 1:
Just to make sure I’m on the right path, is this the correct approach to avoid showing stale content to the end user or should I be doing something different?Sending expires and cache-control headers appears to work mostly but fails under certain circumstances, and that’s where I really need help.
To add the headers I have hooked into the send_headers action and added Expires and Cache-Control like this:
header( “Cache-Control: max-age=$max_age, must-revalidate, proxy-revalidate, public, no-cache”, TRUE );
header( “Expires: $expires”, TRUE );To make that work with W3TC I must turn OFF the Browser Cache settings for “Set expires header” and “Set cache control header”. If those settings are turned ON then my headers are simply overwritten by W3TC.
I would like to turn those settings ON in W3TC so that default expires and cache-control headers are sent for other pages but I would like to be able to replace those headers for my event pages.
Question 2:
Is there any way for my code to replace the default expires and cache control headers sent by W3TC when those settings are ON?I have tried using different priorities (1 and 1000000) for the send_headers action hook but the result is always the same. Is there another hook I should be using or do I need to do this in some other way?
Again, sending the expires and cache-control headers with the page works mostly when those settings are off. But a second problem occurs if I refresh the page in the browser. When the page is refreshed the result sent from the server never includes the original expires or cache control headers that I added. The server may return a 304 when “Set Last-Modifier header” is turned ON, or it may return a 200 when that setting is OFF but in either case the response headers never contain the expires or cache-control headers that I originally added. The behaviour on the browser is that the page does not expire and the stale content is shown.
Strangely, when I turn on debug for W3TC at the bottom of the page I see the debug content like “<|– Performance optimized by W3 …” and it includes the original cache-control header “Cache-Control: max-age=900, must-revalidate, proxy-revalidate, public, no-cache”. However, when I inspect the headers (Firefox Web Developer > Network) the cache-control and expires headers are not there. I assume that is because I am receiving a cached copy of the page which contained the information about the headers sent when the page was originally created but those same headers are not sent with the cached response. Note that this makes the debug information unreliable.
Question 3:
Is there some setting or anything else I can do to cause W3TC to re-send the original headers including expires and cache-control with the response sent from the cache whether it’s 304 or 200?I have been trying to work through these problems for days now and I’m not sure what else I can try. I really need some advice and guidance, and I appreciate your help with this.
Thank you,
-Dave.
- The topic ‘Expires and Cache-Control headers overwritten or not sent’ is closed to new replies.