Performance & Caching
Start with Navigation’s defaults when displaying a menu. Menu queries on frontend requests load parent and child relationships together and can reuse a cached tree. You generally only need additional loading options when your template reads custom data from linked entries or from the menu itself.
For a walkthrough of measuring a menu and choosing those options, see The Complete Guide to Navigation Performance.
Load Nested Links
This template displays a menu’s top level and its direct children. Put it in the partial where the menu belongs, replacing mainMenu with your saved menu handle:
{% set nodes = craft.navigation.nodes('mainMenu').level(1).all() %}
<ul>
{% for node in nodes %}
<li>
{{ node.link }}
{% if node.children|length %}
<ul>
{% for child in node.children %}
<li>{{ child.link }}</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>For this menu-scoped frontend query, Navigation loads the hierarchy automatically. node.children is then a collection of loaded elements. Calling .all() on that collection returns its items; it does not turn it back into a database query.
When hierarchy loading is disabled with withNodeHierarchy(false), child access can instead return an element query and executing it can query the database. The distinction is whether the hierarchy is loaded, not whether your Twig expression ends in .all().
You can also use Craft’s {% nav %} tag for recursive markup. It does not enable hierarchy loading itself. See Rendering Nodes.
Load Linked Entries or Menu Fields
A node supplies its stored title without loading its linked Craft element. When you first read a linked node's url or element, Navigation loads the pending linked elements from that query result together, grouped by element type and linked site. This also applies to nodes returned from the tree cache. URLs still come from the Craft elements, preserving URL events and custom node types.
Use withLinkedElements() when you want those elements loaded as part of the query instead of on first access. If your design reads menu field values through many nodes, withMenu() loads their menus together.
| Query option | Use when | Tree cache |
|---|---|---|
withLinkedElements() | You want linked elements loaded before the query returns | Eligible queries can be cached, and linked elements still load fresh |
withMenu() | You read menu field values through the returned nodes | Bypassed |
withNavigationCache() | Cache mode is Manual and this query should use the cache | Eligible queries can be cached |
Both automatic and explicit batching load the linked entry itself; any relational fields on that entry, such as an Assets field, can need their own eager-loading strategy.
When promotional content is needed once above the menu, fetch craft.navigation.menu('mainMenu').one() separately. This keeps the node query eligible for tree caching. See Menu Fields.
The second argument to tree() is an output-options array. Its withLinkedElements option adds linked element data to the tree output after the node fetch, which can use the tree cache. See Headless Trees.
Choose a Cache Mode
Open Navigation → Settings → Performance to choose how Navigation caches trees:
| Mode | Behaviour |
|---|---|
| Off | Fetch nodes without the plugin tree cache |
| Auto | Cache eligible frontend menu queries; the default |
| Static | Cache eligible queries using the configured duration |
| Manual | Cache eligible queries only when they opt in with withNavigationCache() |
Caching is limited to supported menu-scoped query shapes. Additional filters such as search or arbitrary ordering can bypass it. Queries that use withMenu() also bypass it. The cache stores nodes only, so withLinkedElements() and craft.navigation.render() load linked elements fresh on every read. Control-panel, console, and preview reads do not use the public tree cache.
Dynamic nodes cache their projected children separately, so a menu that lists a large section doesn't query it on every request. The cached children are cleared when content in the source is saved, deleted or moved, when the Dynamic node or its menu changes, and when a projected entry expires or a pending entry reaches its post date. They use the same cache mode as the menu, so in Manual mode only queries with withNavigationCache() use them. Dynamic nodes with a condition, previews, and sources from other plugins that don't opt in project their children on every read. Cached items are shared by every visitor. If a plugin filters entries or other elements differently for each visitor through element query events, turn off Cache Dynamic Node Items in Navigation → Settings → Performance, or set cacheProjectedChildren to false. A cached projected node's element loads on first access, together with the rest of its Dynamic node's children.
Active and current flags are resolved for the current request after the tree loads, including after a cache hit. Cached structure can therefore be reused across pages without reusing their highlights. An outer HTML cache stores the rendered highlights for its own page URL, unless you key it by active state as shown in the next section.
Cache the Rendered Menu
The tree cache saves the node queries, but the menu is still rendered on every request. Craft’s {% cache %} tag keys its cache by URL, so wrapping a menu in it writes a separate copy of the same menu for every page.
Most pages render an identical menu, because only pages in the menu, or under a node that’s active for its URL path, change which nodes are current or active. Key the cache on activeStateKey() and every page with the same active state shares one copy:
{% cache globally using key 'main-menu:' ~ craft.navigation.context('mainMenu').activeStateKey() %}
{% include '_partials/main-menu' %}
{% endcache %}This stores one copy for pages outside the menu and one for each distinct active state, for each site. Start the key with a name for the partial, such as main-menu:. Without it, two partials that render the same menu, such as a desktop and a mobile menu, get the same key and one would show the other’s HTML.
Calling context() with just the menu handle, as above, is the simplest choice. The key covers the nodes context() loads and their children, so a context for level: 1 still tells apart pages that only differ in their deeper nodes. Don’t pass context() criteria that leave out nodes the menu outputs, such as a node type filter, or pages that differ only in those nodes share a copy with the wrong highlights. Once the tree cache and Dynamic node items are cached, working out the key only runs the query that loads Navigation’s menu settings, once per request.
Saving, enabling, disabling, moving or deleting a node clears the cached HTML. When the menu’s query uses the tree cache, anything that clears the menu from Navigation’s cache clears it too, including craft.navigation.invalidateCache(). Both rely on the menu’s query running inside the {% cache %} tag, as it does in the included partial above. Add anything else the menu depends on to the key, such as the logged-in user or a request parameter.
Refresh Cached Menus
Normal menu and node saves invalidate Navigation’s cache automatically. Linked content and Dynamic source changes also trigger invalidation. To clear a menu explicitly while diagnosing a stale result, run this temporarily in a Twig template:
{% do craft.navigation.invalidateCache('mainMenu') %}Remove the call after checking the result; leaving it in a shared partial clears the cache on every render. The PHP equivalent, for a custom module, is:
use verbb\navigation\Navigation;
Navigation::$plugin->getNavigationCache()->invalidateByHandle('mainMenu');Full-page caches and CDNs are separate layers. See Navigation with Blitz and Full-Page Cache for connecting invalidation and diagnosing which layer is stale. The cache event reference lists the event payload and tag formats.
Explicit Hierarchy Loading
Outside normal frontend rendering, use withNodeHierarchy() when you need Navigation to load relationships. If you deliberately disable it, Craft eager-loading remains available:
{% set nodes = craft.navigation.nodes('mainMenu')
.level(1)
.withNodeHierarchy(false)
.with(['children'])
.all() %}This loads direct children through Craft. It is not needed for the default frontend query shown above.