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?