Problem/Motivation
Layout builder will discard cacheability metadata from blocks if they are visited and produce no content.
Digging into how core blocks function, it looks like this is explicitly captured:
\Drupal\block\BlockViewBuilder::preRender
// If $content is not empty, then it contains cacheability metadata, and
// we must merge it with the existing cacheability metadata. This allows
// blocks to be empty, yet still bubble cacheability metadata, to indicate
// why they are empty.
With the offending code in LB discarding the cacheability metadata in the same circumstance:
\Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray::onBuildRender
$content = $block->build();
$is_content_empty = Element::isEmpty($content);
$is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface;
// If the content is empty and no placeholder is available, return.
if ($is_content_empty && !$is_placeholder_ready) {
return;
}
Proposed resolution
Make it consistent, capture the metadata from empty blocks and bubble it appropriately.