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());
}
Comments
Comment #2
holo96 commentedSorry 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...
Comment #3
holo96 commentedSo, 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
Comment #4
holo96 commentedI 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
Comment #6
holo96 commentedI 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.
Comment #7
davidhk commentedThank 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.
Comment #8
holo96 commentedYes, 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.
Comment #9
holo96 commented