Showing local tasks in toolbar with hook_toolbar: caching problem I'm creating a module using the toolbar api showing all administrative functions in a more compact toolbar. I'm having "cache" troubles showing the local tasks in the toolbar... How can I keep these items outside the toolbar cache?

In my custom module I use hook_toolbar to add the local tasks

/**
 * Implements hook_toolbar().
 */
function compact_toolbar_toolbar() {

    $items = [];
    ...

    $tasks = _compact_toolbar_local_tasks();
    $items = array_merge($items,$tasks);

    return $items;
}


And a function to generate the toolbar local tasks...

/**
 * Create local tasks toolbar items
 */
function _compact_toolbar_local_tasks() {

    $items = [];
    $route = \Drupal::routeMatch()-&gt;getRouteName();
    $manager = \Drupal::service('plugin.manager.menu.local_task');
    $primary = $manager-&gt;getLocalTasks($route, 0);

    foreach($primary['tabs'] as $key =&gt; $tab) {

        // add toolbar tab
        $items[$key] = [
            '#type' =&gt; 'toolbar_item',
            '#cache' =&gt; [ // Can I overrule the toolbar cache here?
                'max-age' =&gt; 0, 
            ],
            'tab' =&gt; [
                '#type' =&gt; 'link',
                '#title' =&gt; $tab['#link']['title'],
                '#url' =&gt; $tab['#link']['url'],
                '#access' =&gt; $tab['#access'],
                '#attributes' =&gt; [
                    'title' =&gt; $tab['#link']['title'],
                ],
            ],
            '#weight' =&gt; $tab['#weight'],
        ];

        // check if task is active
        if($tab['#active']) {
            $items[$key]['tab']['#attributes']['class'][] = 'is-active';
        }

        // get secondary tabs for each primary tab
        $route = $items[$key]['tab']['#url']-&gt;getRouteName();
        $secondary = $manager-&gt;getLocalTasks($route, 1);

        // get secondary tabs as tray
        if (!empty($secondary['tabs'])) {

            $links = [];
            $items[$key]['tab']['#attributes']['class'][] = 'toolbar-parent'; 

            // create renderable array of secondary links
            foreach ($secondary['tabs'] as $skey =&gt; $stab) {
                $sname = substr($skey, strrpos($key, '.') + 1);
                $links[$sname] = [
                    'type' =&gt; 'link',
                    'title' =&gt; $stab['#link']['title'],
                    'url' =&gt; $stab['#link']['url'],
                ];
            }

            // add tray with secondary tasks
            $items[$key]['tray'] = [
                '#heading' =&gt; t('Secondary tasks'),
                'toolbar_secondary_tasks' =&gt; [
                    '#theme' =&gt; 'links__toolbar',
                    '#links' =&gt; $links,
                    '#attributes' =&gt; ['class' =&gt; ['toolbar-menu']],
                ],
            ];

        }

    }

    return $items;
}


When I disable the cache in hook_page_top in core/modules/toolbar/toolbar.module everything works like a charm. Otherwise my custom items won't follow the current page...

/**
 * Implements hook_page_top().
 *
 * Add admin toolbar to the top of the page automatically.
 */
function toolbar_page_top(array &amp;$page_top) {
  $page_top['toolbar'] = array(
    '#type' =&gt; 'toolbar',
    '#access' =&gt; \Drupal::currentUser()-&gt;hasPermission('access toolbar'),
    /*
    '#cache' =&gt; [
      'keys' =&gt; ['toolbar'],
      'contexts' =&gt; ['user.permissions'],
    ],
    */
  );
}


Can I overrule the toolbar cache by using the #cache array in my custom toolbar items? Which tags/context should I use?

This is how the toolbar looks like with my compact_toolbar module enabled... (but toolbar cache disabled) I really like the result! - I also made the menu behave like a simple dropdown menu and added a "content menu" to quickly add new content...
"The toolbar is a container for administrative and site-global interactive components."

https://api.drupal.org/api/drupal/core!modules!toolbar!toolbar.api.php/function/hook_toolbar/8.2.x

That would indicate that the toolbar is not the solution you are looking for as the more contextual tabs are not site global but page specific.