The Optimizely CMS SDK provides utility functions to help you navigate and structure your site. These functions are available through the GraphClient instance and are particularly useful for building navigation menus, breadcrumbs, and understanding page hierarchies.
Tip
Consider using config() in your app's entry point to configure the client globally and then getClient() to get a pre-configured client. Manually constructing a client with new GraphClient() is still fully supported. See Fetching Content.
The getPath() method returns the ancestor pages of a given page, from the root down to the current page. This is useful for building breadcrumb navigation.
async getPath(path: string, options?: GraphGetLinksOptions): Promise<Array<PageMetadata> | null>path- The URL path of the page (e.g.,/en/about/team)options(optional)host- The host URL for filteringlocales- Array of locale codes to filter by
An array of page metadata objects sorted from root to the current page, or null if the page doesn't exist.
import { getClient } from '@optimizely/cms-sdk';
export default async function Page() {
const currentPath = '/en/about/our-team';
const client = getClient();
// Get all ancestor pages
const ancestors = (await client.getPath(currentPath)) || [];
// Filter out the start page (first item) and create breadcrumbs
const breadcrumbs = ancestors.slice(1).map((ancestor: any) => ({
key: ancestor._metadata.key,
label: ancestor._metadata.displayName,
href: ancestor._metadata.url.hierarchical,
}));
return (
<nav aria-label="Breadcrumb">
<ol>
{breadcrumbs.map((crumb) => (
<li key={crumb.key}>
<a href={crumb.href}>{crumb.label}</a>
</li>
))}
</ol>
</nav>
);
}The getItems() method returns the direct child pages of a given page. This is useful for building navigation menus and site maps.
async getItems(path: string, options?: GraphGetLinksOptions): Promise<Array<PageMetadata> | null>path- The URL path of the parent page (e.g.,/en/)options(optional)host- The host URL for filteringlocales- Array of locale codes to filter by
An array of child page metadata objects, or null if the parent page doesn't exist.
import { getClient } from '@optimizely/cms-sdk';
export default async function Navigation() {
// Get all direct children of the start page
const client = getClient();
const navLinks = (await client.getItems('/en/')) ?? [];
// Create navigation from child pages
const navigations = navLinks.map((item: any) => ({
key: item._metadata.key,
label: item._metadata.displayName,
href: item._metadata.url.hierarchical,
}));
return (
<nav>
<ul>
{navigations.map((nav) => (
<li key={nav.key}>
<a href={nav.href}>{nav.label}</a>
</li>
))}
</ul>
</nav>
);
}Here's a complete example using both functions to build breadcrumbs and primary navigation:
import { getClient } from '@optimizely/cms-sdk';
export default async function Layout({ currentPath }: { currentPath: string }) {
// Get ancestors for breadcrumbs
const client = getClient();
const ancestors = (await client.getPath(currentPath)) || [];
const breadcrumbs = ancestors.slice(1).map((ancestor: any) => ({
key: ancestor._metadata.key,
label: ancestor._metadata.displayName,
href: ancestor._metadata.url.hierarchical,
}));
// Get main navigation items
const navLinks = (await client.getItems('/en/')) ?? [];
const navigations = navLinks.map((item: any) => ({
key: item._metadata.key,
label: item._metadata.displayName,
href: item._metadata.url.hierarchical,
}));
return (
<div>
{/* Primary Navigation */}
<nav aria-label="Main Navigation">
<ul>
{navigations.map((nav) => (
<li key={nav.key}>
<a href={nav.href}>{nav.label}</a>
</li>
))}
</ul>
</nav>
{/* Breadcrumbs */}
<nav aria-label="Breadcrumb">
<ol>
{breadcrumbs.map((crumb) => (
<li key={crumb.key}>
<a href={crumb.href}>{crumb.label}</a>
</li>
))}
</ol>
</nav>
</div>
);
}Both functions support filtering by locale, which is useful for multi-language sites:
const client = getClient();
// Get navigation items only in English and French
const navLinks = await client.getItems('/en/', {
locales: ['en', 'fr'],
});
// Get breadcrumbs filtered by locale
const ancestors = await client.getPath('/en/about/team', {
locales: ['en'],
});Both functions return null if the requested page doesn't exist:
const client = getClient();
const ancestors = await client.getPath('/non-existent-page');
if (ancestors === null) {
// Page doesn't exist, handle accordingly
return <div>Page not found</div>;
}
// Safe to use ancestors
const breadcrumbs = ancestors.map(/* ... */);