• Hello guys,

    first of all: Great plugin! Many thanks for your effort.

    Unfortunately, I encountered a problem that prevents me from using the plugin. I went through the entire FAQs and also searched the support forum for a solution, but I couldn’t find a solution. I hope you can help me.

    I have an endpoint (hmm/v1/overview) that I would like to have cached. I took your sample code from the FAQ and modified it accordingly:

    function hmm_add_caching( $allowed_endpoints ) {
    	$api_namespace = 'hmm/v1';
    
    	if ( ! isset( $allowed_endpoints[ $api_namespace ] ) || ! in_array( 'overview', $allowed_endpoints[ $api_namespace ] ) ) {
    		$allowed_endpoints[ $api_namespace ][] = 'overview';
    	}
    
    	return $allowed_endpoints;
    }
    
    add_filter( 'wp_rest_cache/allowed_endpoints', 'hmm_add_caching', 10, 1);

    This works wonderfully as soon as you call the endpoint without parameters. ~250ms becomes only 20ms, what a dream.

    I’m building a small React app for the frontend. For this I use the “apiFetch” function from “@wordpress/api-fetch”. However, this function packs a parameter at the end every time. So it turns my request into this:
    https://example.com/wp-json/hmm/v1/overview?_locale=user” instead of “https://example.com/wp-json/hmm/v1/overview”.

    Caching no longer works with this parameter. Did I miss something or is it a bug?

    Many thanks for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @omeglin

    Thank you for using our plugin!

    The _locale parameter shouldn’t prevent our plugin from caching the response. What I am suspecting is that the “@wordpress/api-fetch” is using a nonce to make the call to the REST API. By default all calls containing a nonce are not cached. You can have our plugin also cache calls with a nonce by using the following code:

    /**
    * Always allow caching of requests with a nonce.
    *
    * @param bool             $skip_nonce_caching False if cache should not be skipped when nonce is present.
    * @param \WP_REST_Request $request The current REST Request.
    * @param string           $request_uri The REST URI that is being requested.
    *
    * @return boolean
    */
    function wprc_skip_nonce_caching( $skip_nonce_caching, $request, $request_uri ) {
    $skip_nonce_caching = false;
    
    return $skip_nonce_caching;
    }
    
    add_filter( 'wp_rest_cache/skip_nonce_caching', 'wprc_skip_nonce_caching', 10, 3 );
    Thread Starter Oleg Meglin

    (@omeglin)

    Hello @rockfire ,

    Thanks for the tip. You’re right, it was indeed the nonce header. Unfortunately, the hook in the sample code didn’t work. The value remained unchanged on true and could not be overwritten. I tried to debug it, but without success. Since I was running out of time on this project, I gave up.

    I’ve made life easier for myself and now use axios because I don’t need the nonce header. The endpoints are public.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @omeglin

    You were returning true in the wp_rest_cache/skip_nonce_caching filter? In that case it is expected that there is still no caching. If you return true there will be no caching when a nonce is present, you have to return false in order for request containing a nonce to be cached.

    Thread Starter Oleg Meglin

    (@omeglin)

    Hi @rockfire,

    I used your code and your code returns false. That was the goal. I haven’t changed anything about that. But the actual filter value remained unchanged on true.

    I changed the source code (includes/api/class-endpoint-api.php from line 307) a bit to understand what happens.

    From this query here:

    if ( apply_filters( 'wp_rest_cache/skip_nonce_caching', true, $this->request, $this->request_uri ) && ! is_null( $wp_nonce ) ) {
        return true;
    }

    I did this here:

    $skip_nonce_caching = apply_filters( 'wp_rest_cache/skip_nonce_caching', true, $this->request, $this->request_uri );
    error_log( $skip_nonce_caching );
    if ( $skip_nonce_caching && ! is_null( $wp_nonce ) ) {
        return true;
    }

    The logged value was true, although the filter was overridden to false.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘“_locale” parmater prevents caching’ is closed to new replies.