Pages router: directory/index.mdx renders as a self-referencing folder instead of merging like the sibling-file pattern
A directory containing index.mdx renders as an expandable sidebar folder with a single child page literally named "index," instead of collapsing into one link the way the docs' folder+file merge pattern describes. Filing this after digging through the actual [email protected] package (not just docs) to confirm it's a real gap in the pages router source, not a docs typo, prompted by https://github.com/shuding/nextra/discussions/5035.
Repro
pages/
my-directory/
index.mdxExpected (matching the documented "fruits" sibling-merge example, and matching how Next.js itself treats pages/my-directory/index.mdx as equivalent to pages/my-directory.mdx for routing): the sidebar shows my-directory as a single clickable link.
Actual: the sidebar shows my-directory as an expandable folder, and inside it a child entry called index.
Where it comes from
dist/client/normalize-pages.js's merge step only collapses a folder into a single link when a sibling file shares the folder's exact name in the same parent list:
const prevItem = list[index - 1];
if (prevItem && prevItem.name === item.name) {
items[items.length - 1] = { ...prevItem, withIndexPage: true, frontMatter: item.frontMatter };
continue;
}That's comparing pages/my-directory.mdx against a my-directory/ folder at the same list level, i.e. the documented pattern. It never runs for a folder against its own nested index.mdx, since the index page is one level down (a child of the folder), not a sibling of it.
dist/server/page-map.js's collectFiles confirms what actually gets built for the repro above: a folder entry { name: 'my-directory', route: '/my-directory', children: [...] } whose children contains { name: 'index', route: '/my-directory', frontMatter: {...} }, both resolving to the same route but under two different sidebar names. The theme renders that literally.
asIndexPage as a front-matter key (mentioned in the current docs site) isn't in the v3 pages-router source at all, it only exists in packages/nextra/src/server/page-map/normalize.ts, which is the App Router / page.mdx codepath. Setting it on a pages-router index.mdx is silently ignored since nothing on that path reads it.
Impact
Anyone using the standard Next.js pages-router convention of directory/index.mdx for a folder's own landing content (rather than directory.mdx next to the folder) gets a self-referencing sidebar entry, with no documented workaround short of restructuring to the sibling-file pattern.
Source: shuding/nextra