Plugin Author
Acato
(@acato)
Hi @afurnisstap,
Thank you for using our plugin.
It is bad practice to edit a plugin, since your changes would be lost when the plugin is updated. Most frequently custom code is simply added to the functions.php of your theme, but keep in mind if you are not using your own theme these changes might also be lost when updating the theme. If so, another option is to create a child theme for all your custom code. See: https://developer.ww.wp.xz.cn/themes/advanced-topics/child-themes/ for instructions on how to create a child theme.
Excellent! Thank you Acato!
With your help I’ve managed to make some progress and have found functions.php in my theme. I copied you FAQ code from the link I previously provided and changes ‘posts’ the ‘pages’ for my use case.
/**
* Register the /wp-json/acf/v3/pages endpoint so it will be cached.
*/
function wprc_add_acf_pages_endpoint( $allowed_endpoints ) {
if ( ! isset( $allowed_endpoints[ 'acf/v3' ] ) || ! in_array( 'pages', $allowed_endpoints[ 'acf/v3' ] ) ) {
$allowed_endpoints[ 'acf/v3' ][] = 'pages';
}
return $allowed_endpoints;
}
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_pages_endpoint', 10, 1);
I have added this at the top of functions.php and saved the file. I’m still getting max-age=1 when sending a GET request to a page URL such as /wp-json/acf/v3/pages/1611 though (I have set the Cache timeout setting in Settings > WP REST Cache to 1 hour). Any ideas where I’m going wrong?
-
This reply was modified 7 years, 2 months ago by
afurnisstap.
Plugin Author
Acato
(@acato)
Hi @afurnisstap
We try to alter the response as little as possible, so the max-age is untouched (for now, maybe we should consider to change it). There are two ways to check if the request is using the cache:
- Check the reponse headers for
X-WP-Cached-Call, if it is present and is set to served-cache than the cache is working.
- Check if your request is in the list wp-admin > Settings > WP REST Cache > Endpoint API Caches.
Ahh I see!
OK, so I can see a few entries in the Endpoint API Caches list such as:
/wp-json/acf/v3/pages/38
/wp-json/acf/v3/pages/7
/wp-json/acf/v3/pages/285
And the response in Postman does have the X-WP-Cached-Call header for these requests so it is working to an extent but the example I gave previously (/wp-json/acf/v3/pages/1611) is nowhere to be seen and the X-WP-Cached-Call header is not present for that one. Is there some condition not being met that means some are cached but some aren’t?
One further question regarding what you’ve said about not setting the max-age – will this mean that page speed tools like https://www.webpagetest.org/ and Google PageSpeed won’t recognise that the item is being cached? That’s the main reason I’m doing all of this so I’d hope for the caching to be picked up by those tools! Currently https://www.webpagetest.org/ is telling me:
FAILED – (1 seconds) – https://domain.com/wp-json/acf/v3/pages/1611
…which I’m guessing is based off the max-age?
-
This reply was modified 7 years, 2 months ago by
afurnisstap.
I just re-published/updated the page and now it’s appearing in the Endpoint API Caches list! I will update once I know if it resolves the issues given to me by page speed tests…
Plugin Author
Acato
(@acato)
Hi @afurnisstap
Given the fact that there are already cache-entries for the /wp-json/acf/v3/pages/ endpoint, I don’t see any reason why that one specific response isn’t cached. The only reason why a response is not cached by the plugin is if the original call is returning an error by either returning a HTTP Code other than 200, or if the returned JSON isn’t valid JSON.
About the pagespeed test: It could very well be because of the max-age. When we developed this plugin it was not to score on pagespeed tests, but to speed up the WordPress REST API. I will add it to a list of possible improvements of the plugin and discuss it internally. As a quick fix I will try to (later today or at least this week) implement a filter hook so you can change the max-age value.
Great, thanks @acato. All understood.
A filter hook to set the max age would be great if possible.
Plugin Author
Acato
(@acato)
Hi @afurnisstap
I have just added two filters which allow you to change the headers for the cached response:
Add the cache control header if it is not present:
/**
* Add Cache-Control header if not present.
*/
function wprc_filter_headers( $headers, $request_uri ) {
if(!array_key_exists('Cache-Control', $headers)) {
$headers['Cache-Control'] = 'max-age=3600';
}
return $headers;
}
add_filter('wp_rest_cache/cache_headers', 'wprc_filter_headers', 10, 2);
Set the max-age to 3600:
/**
* Set max-age to 3600.
*/
function wprc_filter_header($header_value, $header_name, $request_uri) {
if($header_name == 'Cache-Control'){
return 'max-age=3600';
}
return $header_value;
}
add_filter('wp_rest_cache/cache_header', 'wprc_filter_header', 10, 3);
A new version of the plugin is now available. Please note: the headers are also cached, so you will have to clear the cache in order for the filters to work.
-
This reply was modified 7 years, 2 months ago by
Acato.
Excellent. Thank you so much for getting this out so quickly. Good job!