• Resolved nookyyy

    (@nookyyy)


    Hi i got a question regarding caching / purging of rest api calls

    I would like to cache the rest api, and be able to purge a tag / url. I’ve implemented everything and tried both but when the purge is executed the whole rest api endpoint is getting purged aswell. I would like to specificly target a tag or an url and not purge the whole rest api.

    It does work, when i turn off “purge rest api” in the cache tab and manually add the endpoint /custom-api/v1/ for example, so my code should be fine?

    is this a bug or how can i make sure that only the tag / url i want to purge gets purged while keeping the cache of the rest api ?

    regards

Viewing 15 replies - 1 through 15 (of 25 total)
  • Thread Starter nookyyy

    (@nookyyy)

    And another question / bug:
    When i add the uri /wpgb/v2/filter/?action= 31536000 to Force Public Cache URIs or Force Cache URIs it seems to never cache the request even though the headers show for example:

    x-litespeed-cache-control: public,max-age=31536000

    x-litespeed-tag: b79_default,b79_URL.030bc8deef49f2f27a591f1a251b23ff,b79_REST,b79_AJAX.render,b79_

    you can check the behaviour here: https://nookyyy.com/reviews/mouse/

    the goal would be to add tags to those requests and then be able to purge them like the ones for the other rest endpoint see above

    regards

    Plugin Support qtwrk

    (@qtwrk)

    but that is an ajax URI , not the REST URI ?

    Thread Starter nookyyy

    (@nookyyy)

    the endpoint in my first post is 100% rest as its my custom endpoint ( GET call )

    the 2nd one is /wp-json/wpgb/v2/filter/?action=render&lang=de for example ( POST call )

    it should be a rest call or maybe they internally req the data via ajax within that rest call ( for whatever reason ) ?

    Plugin Support qtwrk

    (@qtwrk)

    can’t tell anything from surface , but please try grab the debug log , with debug include URI to specifically target to your REST API, see how log shows

    Thread Starter nookyyy

    (@nookyyy)

    Ok for the first issue: CACHE REST API in Cache Settings (checked) – Setting Custom TAG to REST CALL on custom REST API (works) – Caching of REST API call (works) – PURGING of that TAG (does not work – it clears FULL REST API CACHE) – IF i uncheck REST API Cache in settings it does work but the “default” REST API does not get cached anymore

    Log for Rest Call: https://pastebin.ubuntu.com/p/ZT3Z8kVvZf/

    Log for PURGE: https://pastebin.ubuntu.com/p/RSZMf8zfh3/

    Function to purge used:

     foreach ($tags_to_purge as $tag) {

    $debug_string .= 'PURGE REST TAG: ' . $tag . "\n";

    do_action('litespeed_purge', $tag); }

    • This reply was modified 10 months, 2 weeks ago by nookyyy.
    • This reply was modified 10 months, 2 weeks ago by nookyyy.
    Plugin Support qtwrk

    (@qtwrk)

    huh ?

    if you don’t see any purge log , maybe the function is not executed ?

    Thread Starter nookyyy

    (@nookyyy)

    i see the log when i removed the uri from the debug uri inclued – edited my post

    i can see that the TAG b79_REST is added – but WHY – when i dont execute my function this does not happen so its not that just by saving the post the rest data gets purged

    • This reply was modified 10 months, 2 weeks ago by nookyyy.
    Plugin Support qtwrk

    (@qtwrk)

    what if you do a standalone purge ? just for test

    like

    <?php
    requre('./wp-load.php');

    do_action('litespeed_purge', 'xxx');

    Thread Starter nookyyy

    (@nookyyy)

    That does work

    05/25/25 15:21:44.764 [=CLI= 1 1od] X-LiteSpeed-Purge: public,stale,b79_REST_ITEMSPEC_11029 => LiteSpeed\LSC->send_headers()@603 => WP_Hook->apply_filters(,ARRAY)@324 => WP_Hook->do_action(ARRAY)@348 =>
    /var/www/vhosts/nookyyy.com/httpdocs/wp-includes/load.php@517


    i did some more debuging – it seems like the purge call does actually just gets triggered when i save my post

    05/25/25 15:34:35.473 [87.145.134.193:64666 1 JIp] 💓 ------POST HTTP/1.1 (HTTPS) /wp-admin/post.php
    05/25/25 15:34:35.473 [87.145.134.193:64666 1 JIp] Query String:
    05/25/25 15:34:35.473 [87.145.134.193:64666 1 JIp] HTTP_REFERER: https://nookyyy.com/wp-admin/post.php?post=11029&acti...
    05/25/25 15:34:35.474 [87.145.134.193:64666 1 JIp] X-LiteSpeed-Purge: public,stale,b79_Po.11029,b79_URL./reviews/mouse/lamzu/inca/,b79_W.recent-posts-1,b79_FD,b79_REST => LiteSpeed\LSC->send_headers()@603 => WP_Hook->apply_filters(,ARRAY)@324 => WP_Hook->do_action(ARRAY)@348 =>
    /var/www/vhosts/nookyyy.com/httpdocs/wp-includes/load.php@517

    but i turned off

    Purge All On Upgrade, unchecked everything in Auto Purge Rules For Publish/Update and cleared Purge All Hooks field aswell.

    • This reply was modified 10 months, 2 weeks ago by nookyyy.
    Thread Starter nookyyy

    (@nookyyy)

    That does work

    05/25/25 15:21:44.764 [=CLI= 1 1od] X-LiteSpeed-Purge: public,stale,b79_REST_ITEMSPEC_11029 => LiteSpeed\LSC->send_headers()@603 => WP_Hook->apply_filters(,ARRAY)@324 => WP_Hook->do_action(ARRAY)@348 =>
    /var/www/vhosts/nookyyy.com/httpdocs/wp-includes/load.php@517


    i did some more debuging – it seems like the purge call does actually just gets triggered when i save my post

    05/25/25 15:34:35.473 [87.145.134.193:64666 1 JIp]  ------POST HTTP/1.1 (HTTPS) /wp-admin/post.php
    05/25/25 15:34:35.473 [87.145.134.193:64666 1 JIp] Query String:
    05/25/25 15:34:35.473 [87.145.134.193:64666 1 JIp] HTTP_REFERER: https://nookyyy.com/wp-admin/post.php?post=11029&acti...
    05/25/25 15:34:35.474 [87.145.134.193:64666 1 JIp] X-LiteSpeed-Purge: public,stale,b79_Po.11029,b79_URL./reviews/mouse/lamzu/inca/,b79_W.recent-posts-1,b79_FD,b79_REST => LiteSpeed\LSC->send_headers()@603 => WP_Hook->apply_filters(,ARRAY)@324 => WP_Hook->do_action(ARRAY)@348 =>
    /var/www/vhosts/nookyyy.com/httpdocs/wp-includes/load.php@517

    but i turned off

    Purge All On Upgrade, unchecked everything in Auto Purge Rules For Publish/Update and cleared Purge All Hooks field aswell.

    Update:

    so i was able to find a “hack” to make sure when my tags are added that not the whole rest endpoint gets purged – but i still wonder y a post save with my settings triggers so many purge tags

    // Modify LiteSpeed purge headers
    if (strpos($_SERVER['REQUEST_URI'], "LSCWP_NONCE") === false) {
        ob_start(function($buffer) {

            // Get the current purge header if it exists
            $current_header = '';
            foreach (headers_list() as $header) {
                if (stripos($header, 'X-LiteSpeed-Purge:') !== false) {
                    $current_header = $header;
                    break;
                }
            }

            // Extract any REST_ITEMSPEC or REST_TEMPLATE or REST_TEMPLATEtags

            $purge_tags = [];
            if (preg_match_all('/REST_(ITEMSPEC|TEMPLATE|DROPDOWN)_\d+/', $current_header, $matches)) {
                $purge_tags = $matches[0];
            }

            // If we found matching tags, build new purge header
            if (!empty($purge_tags)) {
                $tags = implode(',b79_', $purge_tags);
                @header('X-LiteSpeed-Purge: public,stale,b79_' . $tags);
            }

            return $buffer;
        });
    }
    Thread Starter nookyyy

    (@nookyyy)

    I still cant figure out why the other endpoint is not getting cached.

    when i activate cache rest api calls in the cache tab a call like this should get cached correct? because its not getting cached either

    https://nookyyy.com/wp-json/wp/v2/review/11029

    Log of all 3 Calls: https://pastebin.ubuntu.com/p/8bfGVbNJW8/

    Plugin Support qtwrk

    (@qtwrk)

    eh , sorry , one thing at the time

    9 => 'X-LiteSpeed-Cache-Control: public,max-age=31536000',
    10 => 'X-LiteSpeed-Tag: b79_default,b79_URL./wp-json/wpgb/v2/filter/,b79_REST,b79_AJAX.render,b79_',

    it is getting cached , no ? what do you see on response header?

    Thread Starter nookyyy

    (@nookyyy)

    Sure, response Heaaders for that request are ALWAYS:

    access-control-allow-headers: Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
    access-control-expose-headers: X-WP-Total, X-WP-TotalPages, Link
    cache-control: public, max-age=3600
    content-encoding: br
    content-security-policy: upgrade-insecure-requests
    content-type: application/json; charset=UTF-8
    date: Mon, 26 May 2025 12:10:14 GMT
    expect-ct: enforce, max-age=21600
    expires: Wed, 11 Jan 1984 05:00:00 GMT
    link: <https://nookyyy.com/wp-json/>; rel="https://api.w.org/"
    permissions-policy: geolocation=(), midi=(),sync-xhr=(),accelerometer=(), gyroscope=(), magnetometer=(), camera=(), fullscreen=(self)
    pragma: public
    referrer-policy: strict-origin-when-cross-origin
    server: LiteSpeed
    strict-transport-security: max-age=15768000; includeSubDomains
    vary: Accept-Encoding,User-Agent
    x-content-type-options: nosniff
    x-frame-options: sameorigin
    x-litespeed-cache-control: public,max-age=31536000
    x-litespeed-tag: b79_default,b79_URL.030bc8deef49f2f27a591f1a251b23ff,b79_REST,b79_AJAX.render,b79_
    x-powered-by: PHP/8.3.21
    x-robots-tag: noindex
    x-xss-protection: 1; mode=block

    I never get the

    x-litespeed-cache: hit or miss

    regards

    Plugin Support qtwrk

    (@qtwrk)

    I see, that is a POST request, you need to enable this

    Thread Starter nookyyy

    (@nookyyy)

    Ok thx – activated that and now i get:

    x-litespeed-cache: miss

    every time for the same POST Req. ( would the server still cache based on the req uri or based on the uri + post data ? ) ( simply the filter req on the main load: https://nookyyy.com/reviews/mouse/ )

Viewing 15 replies - 1 through 15 (of 25 total)

The topic ‘REST API / PURGE TAG’ is closed to new replies.