• Resolved cuongpham259

    (@cuongpham259)


    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:

    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

    (@isaumya)

    Hi @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
      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

    (@cuongpham259)

    Hi @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

    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

    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

    (@isaumya)

    Hi @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

    (@cuongpham259)

    Hi @isaumya

    1. Yes. I enabled wp-json settings, which is weird.
      https://prnt.sc/24j3NoDhArvy
    2. Here are some pictures you need:
      Page rule only 1 setting Cache Every thing: 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/MxF1-tE0Db_X, https://prnt.sc/xbyYdqRzPOYr, 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

    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

    (@isaumya)

    Hi @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

    This should resolve your issue. Try it and let me know.

    Thread Starter cuongpham259

    (@cuongpham259)

    Hi @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

    (@isaumya)

    Hi @cuongpham259,
    Yes there is, for that you need to check this guide: 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

    (@cuongpham259)

    Hi @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.
    Plugin Contributor iSaumya

    (@isaumya)

    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

    (@cuongpham259)

    Hi @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

    (@isaumya)

    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.