[Support request] Caching output of a rendered block

Home Forums Support [Support request] Caching output of a rendered block

Home Forums Support Caching output of a rendered block

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2348983
    Zarar

    I have a block which is quite expensive to display, so I’d like to cache the results (I can’t use W3TC Cache or any other caching plugin).

    My idea was to capture the content in a higher priority block and store it to disk, and then check the existence of the file on disk and if it does, return it instead of processing all the child blocks.

    add_filter( 'render_block', function( $block_content, $block ) {	
    	// do something to capture the HTML of that the block generates and store it in cached.html
    }, 50, 2 );
    
    add_filter( 'render_block', function( $block_content, $block ) {	
    	// check if cached.html exists, and return that. If it doesn't, return null to continue processing
    }, 1, 2 );

    Is this a feasible strategy, and if so, how do I capture the HTML the block would return? Is there a better way to do this?

    #2350153
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    It really depends on what the block is doing, exactly.

    If it were me, I would explore caching the expensive aspect of the block using transients or the options API, then simply call the cached data instead of running the expensive code.

    This way the expensive code only runs the one time and is cached, then the cached data is served from then on.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.