menu-cache icon indicating copy to clipboard operation
menu-cache copied to clipboard

Documentation: any strategies for dealing with dynamics like `.current-menu-item`?

Open lkraav opened this issue 4 years ago • 3 comments

With time-based caching, dynamics like .current-menu-item get lost for all cached hits.

Are there any well known strategies for making it work again via AJAX / REST API?

lkraav avatar Apr 13 '20 11:04 lkraav

It really depends on your page structure, the used CSS classes, the structure and styling of the menu etc., but in general you might want to use the cache key filter. You could make the current post ID part of the key, or maybe only include it if the post/page is part of the menu etc., and use 0 otherwise. The concrete logic is up to you.

tfrommen avatar Apr 13 '20 14:04 tfrommen

Indeed, page-specific cache key might work in the next complexity layer level.

But on our system, another complexity layer level up from that, menu system is also showing/hiding some menu items based on roles / membership plans https://github.com/helgatheviking/Nav-Menu-Roles

Menu is cached once per user, with 10 minute timer.

Page-specific keys means cache entry count would be multiplied by the number of users, and the cache would lose a lot of its hit efficiency.

I'm thinking JS-based re-implementation of finding .current-menu-item vs window.location is the only effective way. It's also clear JS can't replicate all or even most of wp_nav_menu() utility classes, since it doesn't have enough information about ancestors etc.

lkraav avatar Apr 13 '20 16:04 lkraav

multiplied by the number of users,

You could try adding only the user role / membership to the cache key.

szepeviktor avatar Apr 13 '20 16:04 szepeviktor