Title: Problem with Cache plugin &amp; REST API
Last modified: July 20, 2023

---

# Problem with Cache plugin & REST API

 *  Resolved [cuongpham259](https://wordpress.org/support/users/cuongpham259/)
 * (@cuongpham259)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/)
 * Hello.
 * Thanks for creating a great plugin…
 * I’m having a problem with the Cache Plugin, have been trying for hours but still
   don’t understand what’s going on.
 * I switch from **WP Rocket** to **Super Page Cache for Cloudflare Plugin**.
 * I use **rest_api** to update the “**View**” Meta for the post every time a User
   visits. The code is as follows:
 *     ```wp-block-code
       function updatePostViewApi(WP_REST_Request $request)
       {
           $postID = $request['id'];
           $postType = get_post_type($postID);
   
           if (is_null($postType)) {
               return new WP_REST_Response(null, 404);
           }
   
           setPostView($postID, $postType);
           return new WP_REST_Response(null, 200);
       }
   
   
   
       add_action('rest_api_init', function () {
           register_rest_route('catalog', '/post_view_data1/(?P<id>\d+)', array(
               'methods' => 'GET',
               'callback'=> 'updatePostViewApi',
               'args' => array(
                   'id' => array(
                       'validate_callback' => function ($param, $request, $key) {
                           return is_numeric($param);
                       },
                   ),
               ),
           ));
   
           register_rest_route('catalog', '/post_view_data2/(?P<id>\d+)', array(
               'methods' => 'GET',
               'callback' => 'updatePostViewApi',
               'args' => array(
                   'id' => array(
                       'validate_callback' => function ($param, $request, $key) {
                           return is_numeric($param);
                       },
                   ),
               ),
           ));
   
           register_rest_route('catalog', '/post_view_data3/(?P<id>\d+)', array(
               'methods' => 'GET',
               'callback' => 'updatePostViewApi',
               'args' => array(
                   'id' => array(
                       'validate_callback' => function ($param, $request, $key) {
                           return is_numeric($param);
                       },
                   ),
               ),
           ));
       });
       ```
   
 * Then I use extra code to generate Shortcode and use Plugin: **Content No Cache**
   to load Meta via **Ajax **it works perfectly with **WP Rocket**.
 * However with the** Super Page Cache for Cloudflare Plugin**. Code only updates
   1 time if CACHE Status is not **HIT**.
 * Looks like **Cloudflare **is caching rest_api and preventing it from working 
   once Cache Status is **HIT**. Or am I missing an installation?
 * Thanks if you have any advice on how to fix this problem.

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Contributor [iSaumya](https://wordpress.org/support/users/isaumya/)
 * (@isaumya)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16911549)
 * Hi [@cuongpham259](https://wordpress.org/support/users/cuongpham259/),
    1. If you would like not to cache the REST API GET requests, you can enable this
       option inside the plugin settings: [https://i.imgur.com/WED1Wgk.jpeg](https://i.imgur.com/WED1Wgk.jpeg)
       
       This will ensure `/wp-json/` requests are not cached.
    2. When Cloudflare caches a webpage HTML at the CDN level, the request never goes
       to your origin server, instead when a request is intercepted by Cloudflare, 
       it checks if the page content is cached, it served the page content from the
       CDN cache without ever needed to reach out to the origin server. So, considering
       how CDN level page cache works, you might need to modify your code. The best
       approach is the create the API endpoints in PHP and then in JS level fetch the
       data, send POST requests to your endpoint to update those data. So that the 
       entire process will happen asynchronously and will not be hampered by cache.
 *  Thread Starter [cuongpham259](https://wordpress.org/support/users/cuongpham259/)
 * (@cuongpham259)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16914158)
 * Hi [@isaumya](https://wordpress.org/support/users/isaumya/)
 * Thanks for the reply.
 * I have installed wp-json. But the API error only updates the data once. Despite
   the announcement “Post view updated successfully” is displayed. But the value
   will not be + added to the Meta “View”
 * [https://prnt.sc/l2ut_xkaqssv](https://prnt.sc/l2ut_xkaqssv)
 * Of course, when logged in, this function works normally.
 * **There is one more strange problem.**
 * I have created some CPTs with urls appended .html at the end. Although logged
   in, these posts are still saved in cloudflare, still valid HIT.
 * [https://prnt.sc/H3jEcS_y-kfe](https://prnt.sc/H3jEcS_y-kfe)
 * For normal pages without the .html value, Dynamic will be displayed. this parameter
   issue is not removed on login.
 * How to configure ignore it?
 * Thanks a lot!
 *  Plugin Contributor [iSaumya](https://wordpress.org/support/users/isaumya/)
 * (@isaumya)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16917115)
 * Hi [@cuongpham259](https://wordpress.org/support/users/cuongpham259/),
    1. Have you enabled the option to not cache the WP JSON endpoint? If you have enabled
       that then CF won’t be caching the Endpoint.
    2. For the `.html` page I can see the plugin is setting the cache-control header
       as `nocache` but due to some of your Cloudflare settings/rules CF is still caching
       it.
    3. Can you please share a screenshot of your Cloudflare account’s Page Rule section,
       Cache Rule Section, if you have any cache rule share detailed screenshot of 
       those cache rule, screenshot of the workers section.
    4. Also please share the screenshot of the Cloudflare Dashbord > Caching > Browser
       Cace TTL section – make sure it is set to Respect Existing Headers.
 *  Thread Starter [cuongpham259](https://wordpress.org/support/users/cuongpham259/)
 * (@cuongpham259)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16917183)
 * Hi [@isaumya](https://wordpress.org/support/users/isaumya/)
    1. Yes. I enabled wp-json settings, which is weird.
       [https://prnt.sc/24j3NoDhArvy](https://prnt.sc/24j3NoDhArvy)
    2. Here are some pictures you need:
       Page rule only 1 setting Cache Every thing:
       [https://prnt.sc/iq_dCLOYG07m](https://prnt.sc/iq_dCLOYG07m)
 * Cache Rule: I imported from your tutorial. And create 3 more Roles to make sure
   the cache is not cached [https://prnt.sc/VLrjP6GQ_HUj](https://prnt.sc/VLrjP6GQ_HUj)
 * [https://prnt.sc/MxF1-tE0Db_X](https://prnt.sc/MxF1-tE0Db_X), [https://prnt.sc/xbyYdqRzPOYr](https://prnt.sc/xbyYdqRzPOYr),
   [https://prnt.sc/OT0crU1PjHsk](https://prnt.sc/OT0crU1PjHsk).
 * **SPCFC Cache Rule ➜ Cache Eligible Requests** same as the settings from the 
   tutorial, so I won’t upload pictures here.
 * **Also Browser Cache TTL** = **Respect Existing Headers**. This setting has been
   around for a long time. [https://prnt.sc/S4BPRg6gaQ7Q](https://prnt.sc/S4BPRg6gaQ7Q)
 * I don’t understand why the page** .html** is being cached, all my posts add .
   html at the end of the url 🙁
 *  Plugin Contributor [iSaumya](https://wordpress.org/support/users/isaumya/)
 * (@isaumya)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16917193)
 * Hi [@cuongpham259](https://wordpress.org/support/users/cuongpham259/),
   Thanks
   for sharing this. The Page rule is causing all the problems. Please disable or
   delete the page rule.
 * Then also delete all the cache rules that you have added and check this newly
   updated implementation guide and add just 1 Cache rule as mentioned here: [https://gist.github.com/isaumya/af10e4855ac83156cc210b7148135fa2](https://gist.github.com/isaumya/af10e4855ac83156cc210b7148135fa2)
 * This should resolve your issue. Try it and let me know.
 *  Thread Starter [cuongpham259](https://wordpress.org/support/users/cuongpham259/)
 * (@cuongpham259)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16917254)
 * Hi [@isaumya](https://wordpress.org/support/users/isaumya/)
 * You are correct, it looks like the Everything Page Rules Cache is corrupting 
   the .html cache. The site is now working as expected.
 * Thank you!
 * I have 1 question. Is it possible to combine **Super Page Cache caching for Cloudflare**
   and **WP Rocket**?
 * Currently the speed for **non-logged **in users is super **fast**. But for **
   logged in** users it is quite **slow**.
 * I want to leverage **WP Rocket **storage for **logged **in users, **disable caching
   for non-logged in users in both plugins** to avoid conflicts. Website distributes
   cache from cloudflare CDN and static html file from WP Rocket to logged in users.
 * This is possible?
 *  Plugin Contributor [iSaumya](https://wordpress.org/support/users/isaumya/)
 * (@isaumya)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16917265)
 * Hi [@cuongpham259](https://wordpress.org/support/users/cuongpham259/),
   Yes there
   is, for that you need to check this guide: [https://gist.github.com/isaumya/d5990b036e0ed2ac55631995f862f4b8](https://gist.github.com/isaumya/d5990b036e0ed2ac55631995f862f4b8)
 * So, basically you can only use WP Rocket for static file optimization and no 
   caching feature. I personally prefer using Perfmatters over WP Rocket as it not
   a page cahcing plugin at the core and has no page caching feature. But if you
   would like to use WP Rocket, check the guide.
 * For logged-in users your origin server’s speed matters a lot. For logged-in users
   pages will never be cached as for them content needs to be generated dynamically
   based on many logics, so PHP needs to generate those pages – they can’t be served
   from cache.
 *  Thread Starter [cuongpham259](https://wordpress.org/support/users/cuongpham259/)
 * (@cuongpham259)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16927225)
 * Hi [@isaumya](https://wordpress.org/support/users/isaumya/)
 * Sorry for the late reply, I saw the doctor for 2 days.
 * You are correct, the speed for the logged in person depends on the actual server.
   But WP Rocket has a function to create a cache for logged in users, I have used
   it in the past and it is quite good.
 * Currently, I know that Super Page Cache for Cloudflare only supports parallel
   use with WP Rocket by banning WP Rocket from using Cache to avoid conflicts. 
   However I had a couple of ideas and tested it out.
 * After adding some customization. The good news is that Super Page Cache for Cloudflare
   can now be used with non-logged in users and HTML cache with logged in users 
   via the WP Rocket plugin. This is just the initial result, more time is needed
   to monitor and evaluate the results.
 * As for the “wp-json” problem. Because I use Security Plugin change wp-json = 
   api-json. So will get an error like the case I described. So is there a way to
   customize the output of the json?… Hope there is an easy way to do this.
 * Thank you for supporting!
    -  This reply was modified 2 years, 10 months ago by [cuongpham259](https://wordpress.org/support/users/cuongpham259/).
 *  Plugin Contributor [iSaumya](https://wordpress.org/support/users/isaumya/)
 * (@isaumya)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16927264)
 * > After adding some customization. The good news is that Super Page Cache for
   > Cloudflare can now be used with non-logged in users and HTML cache with logged
   > in users via the WP Rocket plugin. This is just the initial result, more time
   > is needed to monitor and evaluate the results.
 * OK. Let me know what you have implemented and how? I mean if this works for you,
   please consider sharing a guide of what you have done so that it can also help
   others. Looking forward to seeing your feedback after you test this thoroughly.
 * > As for the “wp-json” problem. Because I use Security Plugin change wp-json 
   > = api-json. So will get an error like the case I described. So is there a way
   > to customize the output of the json?… Hope there is an easy way to do this.
 * This plugin doesn’t do anything special for the REST API endpoints. It either
   send headers asking Cloudflare to cache the GET requests or if you have enabled
   the option inside the plugin then it will not cache the wp-json endpoint. Beside
   that this plugin doesn’t do anything special with the rest API endpoints.
 *  Thread Starter [cuongpham259](https://wordpress.org/support/users/cuongpham259/)
 * (@cuongpham259)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16927285)
 * Hi [@isaumya](https://wordpress.org/support/users/isaumya/)
 * Okay, it’s pretty simple. If everything works as expected. I will share again
   with the community. Just need more time to know that there is no implicit conflict
   here.
 * As a temporary solution, I would reset the default wp-json. It works fine now
   for both login and non-login.
 * The temporary problem has passed. Thanks for helping
 * Have a good day!
 *  Plugin Contributor [iSaumya](https://wordpress.org/support/users/isaumya/)
 * (@isaumya)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16927290)
 * Happy to hear your problem has been fixed now. Also, if everything works properly,
   looking forward to the guide for using WP Rocket only for logged-in users.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Problem with Cache plugin & REST API’ is closed to new replies.

 * ![](https://ps.w.org/wp-cloudflare-page-cache/assets/icon-256x256.gif?rev=3234997)
 * [Super Page Cache – Cloudflare Cache, Page Speed & Core Web Vitals](https://wordpress.org/plugins/wp-cloudflare-page-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-cloudflare-page-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-cloudflare-page-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-cloudflare-page-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-cloudflare-page-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-cloudflare-page-cache/reviews/)

 * 11 replies
 * 2 participants
 * Last reply from: [iSaumya](https://wordpress.org/support/users/isaumya/)
 * Last activity: [2 years, 10 months ago](https://wordpress.org/support/topic/problem-with-cache-plugin-rest-api/#post-16927290)
 * Status: resolved