Support » Plugin: LiteSpeed Cache » Exclude rest api route programmatically

  • Resolved Adil Soomro


    I’m developing a plugin, that need to expose some rest api endpoints. I want to programmatically exclude some routes from caching.

    I’m using JWT Auth Plugin for auth management.

    I’m registering following route:

    register_rest_route( 'something/v1', '/saved-searches', array(
        'methods' => 'GET',
        'callback' => 'listSavedSearches',

    This is how I exclude the request path/page from cache.

    function listSavedSearches() {
        do_action( 'litespeed_control_set_nocache', 'nocache due to logged in' );
        ... //some code
        wp_send_json(['sucess' => true, 'results' => $saved_searches],200);

    So far so good, I get the non-cached results fine.

    However, if I send the wrong auth token in the header, causing jwt_auth_invalid_token the callback listSavedSearches() never called, so it doesn’t set_nocache, and goes to back as 403 unauthorised, but also its cached for future calls.

    All subsequent calls, even with a valid token are responded with 403.
    How can I avoid that?

    PS: If I write the path to plugin setting by going into LightSpeed Plugin > Cache > Exclude > Do Not Cache URIs It works, but I’ve more than few endpoints that I want to exclude from cache, so I’m looking for programmatic solution.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support qtwrk



    it sounds like the hook was fired too late

    have you tried this approach ?

    function lscwp_hook(){
        do_action( 'litespeed_control_set_nocache', 'no-cache for rest api' );
    add_action( 'litespeed_init', 'lscwp_hook');

    you can add some if check for REST API

    Best regards,

    Thread Starter Adil Soomro


    Thank you for you reply. I’ve tried the code you suggested. It works but with a problem. It does not let me specify a page / url to ‘set_nocache’.

    Here’s how I’ve tried:

    function lscwp_init_hook(){
        global $wp;
        $request = $wp->request;
        if (endsWith($request, "saved-searches")) {
            do_action( 'litespeed_control_set_nocache', 'no-cache for rest api' );
    add_action( 'litespeed_init', 'lscwp_init_hook');

    endsWith() is utility method to search the string. So if I take do_action() out from the condition, all rest api are disabled. So it works.

    But I cannot seem to apply it on specific page. Any pointers?

    Plugin Support qtwrk


    maybe something like this ?

    $exclude_url_list = array( "test", "some", "thing" );
    foreach ($exclude_url_list as $exclude_url) {
      if (strpos($_SERVER['REQUEST_URI'], $exclude_url) !== FALSE) {
    do_action( 'litespeed_control_set_nocache', 'no-cache for rest api' );

    if you have multiple URI , otherwise just one if check against REQUEST_URI ?

    it’s not completely a “wordpress” way , but I guess it should work.

    • This reply was modified 11 months, 2 weeks ago by qtwrk.
    Thread Starter Adil Soomro


    Thank you so much, this seem to work. Awesome.

    Now one last thing, when a cached endpoint is returned with 403 due to token expired, how can I get the correct result after token renew. For example:

    I get a token from JWT Auth Plugin, (login sequence) and then hit the webservice with auth bearer token. When token expires, it returns 403, I renew after login do request on same endpoint, but I always get the cached result, even for public pages.

    How can I tell the server to send me non-cached result or purge the existing response? or don’t cache 403 responses?

    Plugin Support qtwrk


    you can randomly attach some string , for example unix timestamp, after your request

    like /?rest_route=/some/thing&random



    this will force the server to generate new cache

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exclude rest api route programmatically’ is closed to new replies.