Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

docs: assert sidebar position relatively, drop the unkeyed nav-index cache

Kshitij Jhunjhunwalada72336dc275
diff --git a/docs/app/(home)/reference/(v31)/layout.tsx b/docs/app/(home)/reference/(v31)/layout.tsx
index 267acfde8..180b08cd8 100644
--- a/docs/app/(home)/reference/(v31)/layout.tsx
+++ b/docs/app/(home)/reference/(v31)/layout.tsx
@@ -4,18 +4,8 @@ import type { ReactNode } from 'react';
import { prepareTree } from '@/lib/filter-api-version';
import { buildSidebarNavIndex, type SidebarNavIndex } from '@/lib/sidebar-nav-index';
import { SidebarAnalytics } from '@/components/sidebar-analytics';
-import type { Root } from 'fumadocs-core/page-tree';
-// The reference tree is only reachable after an await, so it is memoized here
-// rather than hoisted to module scope like the docs and examples sidebars.
-let navIndex: SidebarNavIndex | undefined;
-
-function getNavIndex(tree: Root): SidebarNavIndex {
- navIndex ??= buildSidebarNavIndex(tree);
- return navIndex;
-}
-
-export default async function Layout({ children }: { children: ReactNode }) {
+async function buildReferenceTree() {
const source = await getReferenceSource();
const tree = prepareTree(source.pageTree, '3.1');
const changelogPage = { type: 'page' as const, name: 'Changelog', url: '/reference/changelog' };
@@ -24,7 +14,8 @@ export default async function Layout({ children }: { children: ReactNode }) {
child => child.type === 'page' && child.name === 'Overview'
);
const insertIdx = overviewIdx === -1 ? Math.min(1, tree.children.length) : overviewIdx + 1;
- const pageTree = {
+
+ return {
...tree,
children: [
...tree.children.slice(0, insertIdx),
@@ -32,6 +23,19 @@ export default async function Layout({ children }: { children: ReactNode }) {
...tree.children.slice(insertIdx),
] as typeof tree.children,
};
+}
+
+// The tree is only reachable after an await, so the index is memoized here
+// rather than hoisted to module scope like the docs and examples sidebars.
+let navIndex: SidebarNavIndex | undefined;
+
+async function getNavIndex(): Promise<SidebarNavIndex> {
+ navIndex ??= buildSidebarNavIndex(await buildReferenceTree());
+ return navIndex;
+}
+
+export default async function Layout({ children }: { children: ReactNode }) {
+ const pageTree = await buildReferenceTree();
return (
<DocsLayout
@@ -41,7 +45,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
sidebar={{ collapsible: false, footer: null, tabs: false }}
themeSwitch={{ enabled: false }}
>
- <SidebarAnalytics index={getNavIndex(pageTree)} />
+ <SidebarAnalytics index={await getNavIndex()} />
{children}
</DocsLayout>
);
diff --git a/docs/app/(home)/reference/v3/layout.tsx b/docs/app/(home)/reference/v3/layout.tsx
index a5b961c3c..c8c1de638 100644
--- a/docs/app/(home)/reference/v3/layout.tsx
+++ b/docs/app/(home)/reference/v3/layout.tsx
@@ -4,20 +4,23 @@ import type { ReactNode } from 'react';
import { prepareTree } from '@/lib/filter-api-version';
import { buildSidebarNavIndex, type SidebarNavIndex } from '@/lib/sidebar-nav-index';
import { SidebarAnalytics } from '@/components/sidebar-analytics';
-import type { Root } from 'fumadocs-core/page-tree';
-// The reference tree is only reachable after an await, so it is memoized here
+async function buildReferenceTree() {
+ const source = await getReferenceSource();
+ return prepareTree(source.pageTree, '3.0');
+}
+
+// The tree is only reachable after an await, so the index is memoized here
// rather than hoisted to module scope like the docs and examples sidebars.
let navIndex: SidebarNavIndex | undefined;
-function getNavIndex(tree: Root): SidebarNavIndex {
- navIndex ??= buildSidebarNavIndex(tree);
+async function getNavIndex(): Promise<SidebarNavIndex> {
+ navIndex ??= buildSidebarNavIndex(await buildReferenceTree());
return navIndex;
}
export default async function Layout({ children }: { children: ReactNode }) {
- const source = await getReferenceSource();
- const tree = prepareTree(source.pageTree, '3.0');
+ const tree = await buildReferenceTree();
return (
<DocsLayout
@@ -27,7 +30,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
sidebar={{ collapsible: false, footer: null, tabs: false }}
themeSwitch={{ enabled: false }}
>
- <SidebarAnalytics index={getNavIndex(tree)} />
+ <SidebarAnalytics index={await getNavIndex()} />
{children}
</DocsLayout>
);
diff --git a/docs/tests/static/sidebar-nav-index.test.ts b/docs/tests/static/sidebar-nav-index.test.ts
index 4022a06a6..0b42db3d1 100644
--- a/docs/tests/static/sidebar-nav-index.test.ts
+++ b/docs/tests/static/sidebar-nav-index.test.ts
@@ -21,14 +21,14 @@ describe('buildSidebarNavIndex', () => {
group: 'Core concepts',
folder: 'Authentication',
depth: 2,
- position: 7,
+ position: index['/docs/authentication/controlling-scopes'].position + 1,
});
expect(index['/docs/extending-sessions/shared-connections']).toEqual({
group: 'Guides',
folder: 'Extend sessions',
depth: 2,
- position: 3,
+ position: index['/docs/extending-sessions/custom-tools-and-toolkits'].position + 1,
});
});
@@ -37,7 +37,7 @@ describe('buildSidebarNavIndex', () => {
group: 'Get Started',
folder: null,
depth: 1,
- position: 2,
+ position: index['/docs'].position + 1,
});
});
@@ -46,13 +46,14 @@ describe('buildSidebarNavIndex', () => {
group: 'Core concepts',
folder: 'Authentication',
depth: 1,
- position: 3,
+ position: index['/docs/configuring-sessions'].position + 1,
});
});
test('position resets at the next separator', () => {
+ // Core concepts opens a new group, so its first entry restarts at 1 rather
+ // than continuing Get Started's numbering.
expect(index['/docs'].position).toBe(1);
- expect(index['/docs/quickstart'].position).toBe(2);
expect(index['/docs/how-composio-works'].position).toBe(1);
});
@@ -61,11 +62,12 @@ describe('buildSidebarNavIndex', () => {
expect(index['/docs/extending-sessions/proxy-execute'].position).toBe(1);
});
- test('a folder counts as one row for its siblings', () => {
- // Authentication is row 3 in Core concepts and holds 7 children. Counting
- // through them would report Triggers as row 11 while it renders as row 4.
- expect(index['/docs/triggers'].position).toBe(4);
- expect(index['/docs/skills'].position).toBe(5);
+ test('a folder counts as one entry for its siblings', () => {
+ // Authentication holds 7 children, so a counter that threaded through them
+ // would put Triggers 7 sibling slots later than Authentication instead of
+ // directly after it.
+ expect(index['/docs/triggers'].position).toBe(index['/docs/authentication'].position + 1);
+ expect(index['/docs/skills'].position).toBe(index['/docs/triggers'].position + 1);
});
test('nested folders restart the sequence at each level', () => {
@@ -73,7 +75,7 @@ describe('buildSidebarNavIndex', () => {
group: 'Get Started',
folder: 'Custom providers',
depth: 2,
- position: 12,
+ position: index['/docs/providers/mastra'].position + 1,
});
expect(index['/docs/providers/custom-providers/typescript'].position).toBe(1);
});