Problem/Motivation

After running our site for one day, the render cache has 58,000 copies of the cheeseburger menu block. It happens because the block's cache context is set to path.url, so every page gets its own copy of the block.

On our site the menu is the same on every page, so we just need one copy. Making the change below has that effect, but is there any hook I can use or other approach so I can change the behaviour without modifying the module's code?

Thanks & regards, David

  public function getCacheContexts() {
    $cache_contexts = [
//      'url.path',
    ];
    return Cache::mergeContexts($cache_contexts, parent::getCacheContexts());
  }
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

davidhk created an issue. See original summary.

holo96’s picture

Sorry for late response, I don't know how I missed this...

Yes, I definitely see issue in this...

What are your proposals?

I guess we can make:
1. one hook for altering
2. or we can make this configurable so users change cache context from predefined list...

holo96’s picture

So, url.path cache context is added because of active menu links...

I guess I can enable users to disable this behaviour, and than I can remove `url.path` cache context.

Anyway there should be better ways of caching this, core has it's own cache context for active menu, but I will need to make one for taxonomies

holo96’s picture

I am pretty busy with other projects, but I will solve this as soon as I find time, no worries...

I will create option to disable active menu items than we don't need cache context at all.

Otherwise I will create new cache context that will track active menu items (active trail), this will reduce number of caches significantly

holo96’s picture

Status: Active » Needs review

I think I've fully covered this issue.

In case users don't need to show active menu item at all, they can disable this in block configuration.
So in your case you can disable this with checkbox.

But in case users need to keep this active menu items behaviour it is no longer cached based on `url.path`, but based on active state of menu/term items. So number of render caches should be narrowed down to minimum.

I am not sure if I should add option for users to disable cache fully...

Again, thanks for reporting this issue.

davidhk’s picture

Thank you for taking care of this problem.

I found I can use the standard HOOK_block_build_alter() to alter the #cache settings for a block, so I'm using that for now. I will switch to using the new option you've added once the updated module is released.

holo96’s picture

Yes, I knew there are ways of altering blocks cache, but I wanted to solve problem in first place :D

We will do some testing before this makes to release.

holo96’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.