0

I have enable page cache for anonymous users and no cache for block. There are some random data in cache that change on every page refresh.

Once the page cached by cache_page bin block content is not changing, but it working fine with administrator.

How can I fixed this problem with page cache enable.

1

You have to think of the different levels of caching as "layers". If you cache your page, the blocks will also get cached no matter the setting for the block. If you don't cache your whole page, then the block caching config will kick in.

Ajax Blocks module exists for this purpose. Basically the page cache will only cache the HTML that gets replaced using Ajax by the actual block content (uncached).

Permits to load some blocks by additional AJAX request after loading the whole cached page when the page is viewed by anonymous user. It is suitable for sites which are mostly static, and the page caching for anonymous users is a great benefit, but there are some pieces of information that have to be dynamic.

1

For Drupal 7, there is Authcache module. In Drupal 8, you could use Dynamic Page Cache from core.

You could also use some JavaScript. If you have random data to display, you could display them all, hidden by default and use javascript to display one of them each time. Or you could use AJAX, request a URL that will return your new block content, on each page refresh. Disable cache for specific blocks has more information that can help you.

If you have multiple dynamic blocks in your website, my suggestion is to use a module, as it will take you less time to implement it. Writing JavaScript for each dynamic block will take you more time, to implement it and maintain it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.