Cached pages serving dynamic elements

Please help with choosing the right cache plugin while serving Cached pages with dynamic elements inside. Here is the simple case of this header:
Captura de pantalla 2024-09-15 a las 14.06.28

  • Mini-Cart Bricks element (with dynamic counter and off-canvas with cart total and products )

  • a Favourites with its counter (it doesn’t uses cookies, but storage)

Those two elements have a conditional visibility, so if cart “is empty”, or favorites as shown below:


resulting in this:
Captura de pantalla 2024-09-15 a las 14.12.14

No need to say that counters should be delivered dynamically as well, and the mini-cart off-canvas, which is not ajaxfied.

A quick answer will be to serve “non-cached” pages if visitor is-login, or has-cookies. However, the shopping process can be completed without creating an account nor accepting cookies. Favorites use “storage” instead cookies…

Solutions on my mind:

OPTION 1 Serving dynamic Bricks elements as AJAX. There is way to do it?

OPTION 2 Automattic “WP Super cache” plugin allows to serve non-cached elements inside a cache page, with function “cacheaction”. Example:

Create this snippet:

function dynamic_time() {
    echo "<div id='current-time'>Current time: " . date('H:i:s') . "</div>";
}

add_cacheaction( 'wpsc_cachedata', 'dynamic_time' );

Then in the template:

<?php if (function_exists('dynamic_time')) dynamic_time(); ?>

This will serve dynamic data on a “WP super cache” cached page.

But, how can I embed the Bricks "Mini Cart” element and other dynamic Bricks elements inside a function?

Or maybe I’m missing something? Any clue is highly appreciated.

If I, a Bricks rookie, had to solve it today, I’d create my own AJAX function. Get the data on page load then update the element.

1 Like

I got this answer from Matej at Bricks support:

my first idea was a shortcode.
You could create a template, and then create/load this shortcode with your non-cached function.

1 Like

Please let us know how it turns out. That’s a good solution to have in your notes.

I’m having a bad time attempting to print Bricks templates shortcodes though AJAX. Nothing appears and I’m not good in debugging. However simple shortcode like the one below are printed correctly through AJAX:

function simple_shortcode() {
    return 'Simple shortcode content'; 
}
add_shortcode('simple_test', 'simple_shortcode');

I know W3 Total Cache has something called Fragment Caching, but I’m never used it

https://www.boldgrid.com/support/w3-total-cache/how-to-implement-page-fragment-caching-exception-in-w3-total-cache/

1 Like

I’ve added a request on Bricks idea board: a “deliver through Ajax” option on the block element.

If true, dynamic content blocks can be delivered on fully cached pages with ease from Bricks using any cache plugin.

Meanwhile, I’ve opened a ticket to check why template shortcodes are not rendered through ajax. I can provide JS/php under request.