Placeholder lazy_builder cache max-age ignored?

Created on 22 December 2023, 6 months ago

I'm trying to figure out lazy loading and placeholders, so I can have a page that has dynamically loaded components, while leveraging the cache to build and store 95% of a given page in the cache.

I've built a custom controller that displays a user and time. And I do have lazy_builder loading:

<?php

namespace Drupal\d10cache\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Cache\Cache;

/**
 * An example controller.
 */
class D10CacheController extends ControllerBase implements TrustedCallbackInterface {
  public static function trustedCallbacks() {
    return [
      'lazyBuilder'
    ];
  }

  public function content() {
    $build['element'] = [
      '#lazy_builder' => [static::class . '::lazyBuilder',[]],
      '#create_placeholder' => TRUE,
    ];

    return $build;
  }

  public static function lazyBuilder() {
    $build['#markup'] = '<p>Timestamp: '.time().'</p>';
    $build['#markup'] .= '<p>User: '.\Drupal::currentUser()->getAccountName().'</p>';
    $build['#cache']['max-age'] => 30;
    return $build;
  }

}

Without #create_placeholder set, the caching works as expected, but the placeholder doesn't. The entire page uses the cache and doesn't re-render until 30 seconds later, but then content() is also called, something I theoretically want to avoid with a placeholder.

Once I set #create_placeholder to true, the lazyBuilder works as expected - only loading lazyBuilder and not reloading content(), but the caching doesn't. $build['#cache']['max-age'] is entirely ignored, and lazyBuilder is called on every page load, regardless of the length of the page cache.

I found a couple of adjacent issues πŸ“Œ Compatibility with Cache Control Override: is max-age=0 really necessary for BigPipe's placeholders? Needs work and πŸ“Œ Auto-placeholdering for #lazy_builder with bubbling of max-age Needs work , but not sure those are the right path.

Is cache max-age actually ignored for placeholder lazy_builder calls? Is there an issue I can research this on? Is that just "how it is"?

Thanks in advance!

πŸ’¬ Support request
Status

Active

Version

10.2 ✨

Component
BigPipeΒ  β†’

Last updated about 13 hours ago

Created by

πŸ‡¨πŸ‡¦Canada TrevorBradley

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024