Improve cacheability metadata

Created on 28 January 2024, 5 months ago
Updated 29 January 2024, 5 months ago

Problem/Motivation

I think there might be some areas where the current cacheability metadata isn't quite right. It's a thorny area so apologies in advance if I've got anything wrong (:

  1. Use entity bundle cache tags and update minimum Drupal version to 8.9. As far as I can tell this also means we can remove the prevnext-ENTITY_TYPE-BUNDLE cache tag.
  2. Use the route rather than url cache context.
  3. Ensure the entity view cache is cleared when the module config's updated, regardless of whether it's currently enabled for the entity type.
  4. Ensure the user.permissions cache context is always added if links are enabled for the entity type. It's not likely to make a difference because of the default cache contexts but I think it's strictly speaking a little more correct.
  5. Ensure cacheability metadata is added even if the link doesn't generate correctly.
  6. Remove the ENTITY_TYPE_view cache tag: IIUC it's for content that's built with the entity view builder. If eg. a block with links is cached, it doesn't need to be purged every time the entity view display's saved.
  7. Track the URL cacheability metadata.
  8. (Not connected with render caching, but I figured it's close enough...) Clear extra fields cache on config change.

Thanks!

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇬🇧United Kingdom AndyF

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

Merge Requests

Comments & Activities

  • Issue created by @AndyF
  • Issue was unassigned.
  • 🇬🇧United Kingdom AndyF

    I've added a first stab at that. If reviewing please be aware that some of the changes are from 📌 Refactor building links render array into prevnext.service Needs review and any comments on those changes should be made on that ticket.

    Also I thought I'd explain the latest commit (fc936c4eb444158c9d6c08f9849c8d639f67a032). Without that commit I think the caching is correct, but it adds the config:prevnext.settings cache tag to every single entity view, which means every entity view gets purged on any prevnext config change, regardless of whether it's using prevnext. To avoid that I removed the cache tag and instead explicitly flush the entity bundle tags when the module config changes. It might be overkill, but I felt guilty just adding the config cache tag to every single entity view (:

    Thanks!

  • 🇮🇹Italy kopeboy Mainland

    I would also consider this improvement if I understand correctly:

    I noticed this at the end of prevnext.module:

    /**
     * Implements hook_ENTITY_TYPE_presave().
     */
    function prevnext_node_presave(NodeInterface $entity) {
      $config = \Drupal::config('prevnext.settings');
      $enabled_nodetypes = $config->get('prevnext_enabled_nodetypes');
      if (is_array($enabled_nodetypes) && in_array($entity->bundle(), $enabled_nodetypes)) {
        // We are saving a node of a type with prevnext enabled, so invalidate
        // all cached rendered output of other nodes of this type with our tag.
        Cache::invalidateTags(['prevnext-' . $entity->bundle()]);
      }
    }

    Why do we have to invalidate cache of all nodes of some type with prevnext?
    Since the nid is fixed, shouldn't we invalidate only the previous and next nids?

    1. in case the node was deleted/disabled we regenerate the next link on the previous nid and the prev link on the next nid
    2. in case the node was inserted we generate the next link of the previous nid
    3. in case the node was enabled we regenerate the previous link of the next nid and the next link of the previous nid.
  • 🇬🇧United Kingdom AndyF

    Thanks @kopeboy!

    The MR already removes the section at the end of the module, though note if I understand correctly the same thing will happen anyway due to Drupal core. \Drupal\Core\Entity\EntityBase::invalidateTagsOnSave() calls ::getListCacheTagsToInvalidate() which adds the tags ENTITY_TYPE_list and if there's a bundle ENTITY_TYPE_list:BUNDLE.

  • Merge request !29Update cache handling → (Open) created by AndyF
  • Status changed to Needs review 5 months ago
  • 🇬🇧United Kingdom AndyF

    Rebased off 3.0.x, it no longer includes external code and is ready for review/merge. Thanks!

  • 🇬🇧United Kingdom AndyF
  • Pipeline finished with Success
    5 months ago
    Total: 138s
    #87374
  • Pipeline finished with Canceled
    3 months ago
    Total: 117s
    #122223
  • Pipeline finished with Success
    3 months ago
    Total: 138s
    #122227
Production build 0.69.0 2024