Auto-placeholdering in Drupal

Uncacheable metadataa

This is part 2 of https://bryanmanalo.com/how-drupal-caches-work-under-hood .

Why would you need to implement auto-placeholders?

1. Using cache-context "user" will typically invalidate Dynamic Page Cache, making your page marked as uncacheable for dynamic page cache.  As shown in photo above.

2. If a small piece of your content is marked as uncacheable, it is a poison.  It will mark the entire page as uncacheable for dynamic page cache due to cache bubbling up. 

3. This does not affect anonymous users.  This does not affect Blocks.  Blocks already have auto-placeholdering in place (including Views Blocks).  This only affects the main response, i.e. output from custom controllers or Views page.

 

How to implement auto-placeholders?

1. Implement user cache-contexts.

2. Implement '#lazy_builder'.  The lazy_builder callback should return a render array.

Lazy builder

 

Doin the the above steps will ensure that your page is still Dynamic Page Cache cacheable.  Go visit part 1 of this blog entry (https://bryanmanalo.com/how-drupal-caches-work-under-hood) to understand what is Dynamic Page Cache.

Further reading:

https://medium.com/gridonic-web/understanding-drupals-auto-placeholdering-22208b0680bf

https://www.drupal.org/docs/drupal-apis/render-api/auto-placeholdering