0

I'm working on a paragraph type that will need to display on a very limited number of pages but has some specific functionality where the preprocess function will need to run each time.

In my preprocess function, I've tried various iterations of the following:

$variables['elements']['#cache']['max-age'] = '0';
$variables['elements']['#cache']['contexts'][] = 'url.path';
$variables['elements']['#cache']['contexts'][] = 'url.query_args:r';
$variables['elements']['#cache'] = ['tags' => ['my_paragraph']];
Cache::invalidateTags(['my_paragraph']);
\Drupal::service('page_cache_kill_switch')->trigger();

However, the preprocess function runs once after a cache clear and then doesn't run again.

What am I doing incorrectly here? How can I get this to run each time?

  • 2
    I'd recommend making a step back and start to explain the specific functionality where the preprocess function will need to run each time. – leymannx Dec 4 '19 at 14:58
0

You've got a logic error in your PHP. You're doing the following and setting some variables:

$variables['elements']['#cache']['max-age'] = '0';
$variables['elements']['#cache']['contexts'][] = 'url.path';
$variables['elements']['#cache']['contexts'][] = 'url.query_args:r';

But then in the next line you're redeclaring $variables['elements']['#cache'] as a new array of ['tags' => ['my_paragraph']]; So everything you were doing previously in the $variables['elements']['#cache'] is now gone

So your max-age declaration doesn't exist now. Try with just these 2 items and that should be all you need. The first to tell drupal not to cache the paragraph. The 2nd to disable anonymous page caching as setting max-age 0 doesnt disable the page cache:

$variables['elements']['#cache']['max-age'] = '0';
\Drupal::service('page_cache_kill_switch')->trigger();
  • I've read it as a list of things OP tried. But who knows... – leymannx Dec 4 '19 at 15:01
  • Ah yes potentially – Leigh Dec 4 '19 at 15:01
0

My understanding is that you're trying to set a cache max-age of 0 (don't cache) for a paragraph. I think your issue here is that the max-age doesn't bubble up to the page level and the page cache still caches your paragraph. This makes it so your paragraph is not processed again.

My understanding is that cache max-age data doesn't bubble up to the page level from subelements. There are a couple open issues on d.o for this.

To me, it looks as though this is something that may not be resolved in core for a while (or maybe ever?). There is a contributed module, Cache Control Override that will bubble up the correct cache data to the page level. From the module's description:

In Drupal 8 core Page Cache max age settings are applied globally to all pages, and there is no way to set a different max age per page or leverage cache metadata max-age to override Cache-Control max-age value. This means that if you're hosting your website behind a reverse proxy that honors Cache-Control header, all pages are cached with equal TTL. You can either disable cache with response policies, or cache it for the same global value.

This module is intended to solve that problem by leveraging cacheability metadata bubbled up to the page response level to override Cache-Control max-age parameter.

If you install the Cache Control Override module, then set the max-age in your preprocess function, e.g. $variables['elements']['#cache']['max-age'] = '0';, you should see your preprocess run every page load. However this will also ensure your pages with your paragraph never get cached at the page level. This could be a problem.

Depending on your site, it may be more prudent to use an alternative solution and render a placeholder in your paragraph and then use an ajax request to load the dynamic data into the placeholder. This will allow your full pages to stay cached while also allowing you to load dynamic data into your paragraph.

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.