Search and navigation
Cookbook builds routes from your content sources. With no explicit navigation,
it lists published pages from those sources. Add navigation.items to choose
their order and groups, or navigation.tabs to give major sections separate
sidebars. The navigation reference covers every
item form and the rules for nested groups and tabs.
For example, a small site can give its guide and API reference separate
sidebars. Create pages at each tab’s link route first:
navigation: {
tabs: [
{ label: "Guide", link: "/", items: ["/", "/getting-started"] },
{ label: "API", link: "/api", items: ["/api", "/api/client"] },
],
}The production build creates a local Pagefind index. Readers can select Search
in the header or press Ctrl+K (⌘+K on macOS). The dialog focuses its input,
shows matching pages and headings, and closes with Escape or its close button.
Focus returns to the control that opened it. Search assets load when the dialog
opens. During astro dev, the dialog explains that results require a build;
use npm run build && npm run preview to test them.
Search is enabled by default. Set search.enabled: false when the site does
not need an index. Set pagefind: false in a published page’s frontmatter to
omit just that page. Draft pages are always omitted from search, even though
their direct URLs remain available. The publishing guide
explains how preview indexing and draft visibility work.
Use groups for a short, scannable sidebar. A group may link to its own overview
page; deeper groups expand to show their children. Cookbook checks that every
internal navigation route exists. The order also sets previous and next page
links. Tabs give each major section its own sidebar, including pages outside
the tab’s URL prefix when listed in that tab’s items.
Top-level groups keep their headings and direct links visible. Deeper groups open on click or keyboard activation, and the current page’s ancestors open on arrival. Clicking a linked group header first selects its page and opens the group; once that page is selected, clicking the header toggles the group. Explicit expansion choices are remembered for the browser tab. Only the current page is highlighted.
On narrow screens, the active sidebar moves into a drawer. It includes the section selector, links, and a close control. The drawer closes when a reader follows a link. Keyboard focus stays in the drawer while it is open and returns to the menu control when it closes. Content and links remain readable if client JavaScript is unavailable.
Add a short badge beside a navigation link or group label:
navigation: [
{
label: "Guides",
items: [
"/authentication",
{ label: "Offline support", link: "/offline-support", badge: "NEW" },
"/deploying",
],
},
],To keep the badge with its page, set it in frontmatter instead:
sidebar:
badge: NEWDefault and autogenerated sidebars, route strings, and linked navigation groups
inherit the page’s badge. An explicit navigation badge overrides frontmatter;
use badge: "" to hide an inherited badge. Badges are plain text, including
characters such as <, and do not change pagination labels.
Badges use the brand-derived accent-text and accent-surface-subtle palette roles
in every color scheme and contrast mode. Customize their appearance with a
partial component style override:
theme: {
styles: {
Sidebar: {
Badge: { radius: "0.5rem" },
},
},
}Translate explicitly configured badges with keys such as "navigation.NEW" in
translations. Frontmatter badges come from each locale’s page.
Desktop articles show an on-page table of contents. Enable an optional compact contents list below the header on smaller screens with:
tableOfContents: { mobile: true, minHeadingLevel: 2, maxHeadingLevel: 3 }The same field in page frontmatter overrides the site setting for that page;
tableOfContents: false hides both lists. See Authoring
for heading behavior.
Configure locales to offer translated pages and site.versions to offer
documentation versions. The selectors prefer the equivalent page when it
exists and fall back to a real language or version home when it does not.
Only available translations appear in alternate-language metadata. See the
language and
version configuration examples.
Internal page links use client navigation over the generated static HTML. The router loads when an internal page link is hovered, focused, or clicked, so it does not add to the initial page download for readers who stay on one page. The current page stays visible while the next page and its styles load, then swaps without a full-page fade. Appearance preferences, sidebar expansion choices, and desktop sidebar scroll within the same navigation section are preserved. Article scroll resets to the top on page changes, including Back and Forward; links with fragments still navigate to their heading. The current page and its ancestors update from the destination markup. Mobile navigation and other open panels close.
Cookbook enables Astro’s built-in hover prefetching by default. Hovering over or focusing an internal link for about 80 ms downloads the destination HTML in the background. Moving across links briefly does not prefetch them, and repeated links to the same page share a prefetch. When the browser reports data-saving mode or a slow connection, Astro waits for tap intent instead.
Prefetching warms the browser’s HTTP cache. Static hosting should send cache
headers such as Cache-Control or ETag so navigation can reuse the response;
uncacheable responses may still be downloaded again on click. Browser and private
session policies can also limit cache reuse. Destination stylesheets remain part
of the router’s preparation before the page swap.
Set prefetch: false in astro.config.ts to disable page prefetching while
retaining client navigation. Cookbook preserves any explicit Astro prefetch
configuration, including prefetchAll: false for selective prefetching. For a
single link, use data-astro-prefetch="false" to opt out or
data-astro-prefetch to opt in. See Astro’s prefetch guide
for the supported strategies.
Search keeps its query and loaded engine between pages in the same language,
and closes when a result is followed. Language changes initialize the translated
search interface. External links, downloads, modified clicks, and links marked
data-astro-reload keep normal browser behavior. Without JavaScript, page links
use normal document navigation.
Custom browser scripts should attach behavior through custom-element lifecycle
callbacks or initialize on astro:page-load; bundled scripts are not rerun on
every client navigation. The router and appearance initialization belong to the
common page shell and remain available with replacement Header, Head, and
PageFrame components.