Help with dynamic page cache I have a page generated by a custom module that contains two sections:


Product info
Recently viewed products


The product info only changes when the product is updated.

Recently viewed products are unique to every visitor (data is stored in $_SESSION).

Each section has its own render array.

'productinfo' =&gt; [
    // ...
    '#cache' =&gt; [
        'tags' =&gt; ['product:1234']
    ]
],
'recentlyviewed' =&gt; [
    // ...
    '#lazy_builder' =&gt; array(
      // ....
    ),
    '#create_placeholder' =&gt; TRUE,
    '#cache' =&gt; [
        'max-age' =&gt; 0
    ]
]


The behaviour I expect is that the dynamic page cache can cache the productinfo section, but the recentlyviewed would never be cached. However, if a session has not yet been started (i.e. first page view), the entire html page is cached to the cache_render DB table, and cache_dynamic_page_cache is always empty.

The X-Drupal-Dynamic-Cache header is always set to UNCACHABLE.

How can I get the dynamic page cache to cache the product info section?
Your question has two different topics:


  
  How can I get the dynamic page cache to cache the product info
  section?
  


Set cache keys:

'productinfo' =&gt; [
    // ...
    '#cache' =&gt; [
        'keys' =&gt; ['mymodule', 'product', '1234'],
        'tags' =&gt; ['product:1234'],
    ],
],


Sections of a render array are only cached, if they have cache keys. The keys are used to build the unique cid for the cache render table.


  
  However, if a session has not yet been started (i.e. first page view),
  the entire html page is cached
  


Anonymous traffic is cached by the Internal Page Cache. If you start a session, the next request will be able to pass the Internal Page Cache and is handled by the Dynamic Page Cache.