Stop adding root twice to Breadcrumb (#3101)

* Stop adding root twice to Breadcrumbs

* Use `item.index?.url` by default in Breadcrumb

* Update tests to test includeRoot

* Format and a changeset

* Add test for includeRoot object form with custom URL

Made-with: Cursor
This commit is contained in:
Piotr Monwid-Olechnowicz
2026-03-13 12:40:23 +08:00
committed by GitHub
parent 8729068802
commit bb07706b53
3 changed files with 72 additions and 8 deletions
+8
View File
@@ -0,0 +1,8 @@
---
"fumadocs-core": patch
---
Include root items only once in Breadcrumb.
Previously, when `includeRoot` was set to `true`, the root item was added twice to breadcrumbs.
+7 -8
View File
@@ -73,8 +73,14 @@ export function getBreadcrumbItemsFromPath(
});
break;
case 'folder':
if (item.root && !includeRoot) {
if (item.root) {
items = [];
if (includeRoot) {
items.push({
name: tree.name,
url: typeof includeRoot === 'object' ? includeRoot.url : item.index?.url,
});
}
break;
}
@@ -95,13 +101,6 @@ export function getBreadcrumbItemsFromPath(
}
}
if (includeRoot) {
items.unshift({
name: tree.name,
url: typeof includeRoot === 'object' ? includeRoot.url : undefined,
});
}
return items;
}
+57
View File
@@ -96,6 +96,63 @@ test('Breadcrumbs', () => {
]);
expect(getBreadcrumbItems('/invalid', tree)).toMatchInlineSnapshot(`[]`);
const treeWithRoot: Root = {
name: 'Docs',
children: [
{
type: 'folder',
name: 'Docs',
root: true,
index: { type: 'page', name: 'Introduction', url: '/docs' },
children: [
{
type: 'folder',
name: 'Guides',
index: { type: 'page', name: 'Guides', url: '/docs/guides' },
children: [
{
type: 'page',
name: 'Getting Started',
url: '/docs/guides/getting-started',
},
],
},
],
},
],
};
expect(
getBreadcrumbItems('/docs/guides/getting-started', treeWithRoot, {
includeRoot: true,
includePage: true,
}),
).toStrictEqual([
{ name: 'Docs', url: '/docs' },
{ name: 'Guides', url: '/docs/guides' },
{ name: 'Getting Started', url: '/docs/guides/getting-started' },
]);
expect(
getBreadcrumbItems('/docs/guides/getting-started', treeWithRoot, {
includeRoot: false,
includePage: true,
}),
).toStrictEqual([
{ name: 'Guides', url: '/docs/guides' },
{ name: 'Getting Started', url: '/docs/guides/getting-started' },
]);
expect(
getBreadcrumbItems('/docs/guides/getting-started', treeWithRoot, {
includeRoot: { url: '/custom-root' },
includePage: true,
}),
).toStrictEqual([
{ name: 'Docs', url: '/custom-root' },
{ name: 'Guides', url: '/docs/guides' },
{ name: 'Getting Started', url: '/docs/guides/getting-started' },
]);
});
test('I18n: Format URL', () => {