The precomputed sitemap loader is a Webpack/Turbopack loader that processes markdown files at build time to create optimized search indexes and navigation data. It extracts metadata from documentation pages and generates structured sitemap data with Orama search schema.
Tip
For documentation on the
createSitemapfactory function, seecreateSitemap.
The loader processes createSitemap factory calls, automatically reading and parsing imported markdown files to extract metadata, generate search-friendly data structures, and create navigation hierarchies.
Tip
This loader is designed to work with the
transformMarkdownMetadataplugin which extracts metadata from MDX files.
The easiest way to configure this loader is with the withDocsInfra Next.js plugin:
// next.config.js
import { withDocsInfra } from '@mui/internal-docs-infra/withDocsInfra';
export default withDocsInfra({
// Automatically includes:
// - './app/sitemap/index.ts'
// The loader is pre-configured and ready to use
});
If you need manual control, add the loader directly to your next.config.mjs:
Note
The Turbopack loader requires Next.js version v15.5 or later (depends on this fix)
/** @type {import('next').NextConfig} */
const nextConfig = {
turbopack: {
rules: {
'./app/sitemap/index.ts': {
loaders: ['@mui/internal-docs-infra/pipeline/loadPrecomputedSitemap'],
},
},
},
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.module.rules.push({
test: /\/sitemap\/index\.ts$/,
use: [defaultLoaders.babel, '@mui/internal-docs-infra/pipeline/loadPrecomputedSitemap'],
});
return config;
},
};
The loader expects a sitemap index file that imports markdown pages:
app/
├── sitemap/
│ └── index.ts # Factory file (processed by loader)
├── docs-infra/
│ ├── components/
│ │ └── page.mdx # Imported markdown file
│ └── functions/
│ └── page.mdx # Imported markdown file
Create an index.ts file using the createSitemap factory:
import { createSitemap } from '../functions/createSitemap';
import DocsInfraComponents from '../docs-infra/components/page.mdx';
import DocsInfraFunctions from '../docs-infra/functions/page.mdx';
export const sitemap = createSitemap(import.meta.url, {
DocsInfraComponents,
DocsInfraFunctions,
});
You can disable precomputation for development or testing:
export const sitemap = createSitemap(
import.meta.url,
{
DocsInfraComponents,
DocsInfraFunctions,
},
{ skipPrecompute: true },
);
The loader follows these steps to generate sitemap data:
Finds the createSitemap function call and extracts imported markdown files.
Generates section titles and URL prefixes from file paths:
/app/docs-infra/components/page.mdx → { title: "Docs Infra Components", prefix: "/docs-infra/components/" }/app/docs-infra/functions/page.mdx → { title: "Docs Infra Functions", prefix: "/docs-infra/functions/" }Reads each markdown file in parallel and extracts metadata using markdownToMetadata:
Strips unnecessary fields to reduce bundle size:
descriptionMarkdown (markdown AST nodes)titleMarkdown from section hierarchieschildren to undefinedCreates an Orama-compatible search schema:
{
schema: {
slug: 'string',
path: 'string',
title: 'string',
description: 'string',
keywords: 'string[]',
section: 'string',
prefix: 'string',
},
data: { /* sitemap sections */ }
}
Inserts the precomputed data structure with schema into the source code.
The loader generates a structured data format:
{
"precompute": {
"schema": {
"slug": "string",
"path": "string",
"title": "string",
"description": "string",
"keywords": "string[]",
"section": "string",
"prefix": "string"
},
"data": {
"DocsInfraComponents": {
"title": "Docs Infra Components",
"prefix": "/docs-infra/components/",
"pages": [
{
"slug": "code-highlighter",
"path": "./code-highlighter/page.mdx",
"title": "Code Highlighter",
"description": "Component for displaying code...",
"keywords": ["code", "syntax", "highlighting"],
"sections": {
"features": {
"title": "Features",
"children": undefined
}
}
}
]
}
}
}
}
The precomputed data is designed to work seamlessly with Orama search:
import { create, insertMultiple, search } from '@orama/orama';
// Load the sitemap
const { sitemap } = await import('./app/sitemap');
// Create search index with the precomputed schema
const searchIndex = await create({
schema: sitemap.precompute.schema,
});
// Flatten and insert pages
const pages = Object.entries(sitemap.precompute.data).flatMap(([_key, section]) => {
return section.pages.map((page) => ({
...page,
section: section.title,
prefix: section.prefix,
}));
});
await insertMultiple(searchIndex, pages);
// Search
const results = await search(searchIndex, { term: 'code' });
The loader automatically generates metadata from file paths:
Converts kebab-case path segments to Title Case:
docs-infra → "Docs Infra"code-highlighter → "Code Highlighter"abstract-create-demo → "Abstract Create Demo"Generates URL prefixes from the directory structure:
/app/docs-infra/components/page.mdx → "/docs-infra/components/"/app/blog/2024/page.mdx → "/blog/2024/"/app/page.mdx → "/"Route groups (parentheses) are automatically removed:
/app/(shared)/page.mdx → "/"app/sitemap/index.ts for consistencyThe loader includes performance tracking for optimization:
descriptionMarkdown AST nodes (can be 50%+ of metadata size)titleMarkdown from section hierarchieschildren: {} to children: undefinedEnable performance logging in your loader configuration:
{
performance: {
logging: true,
notableMs: 100,
}
}
If a markdown file cannot be found, the loader logs a warning and continues processing other files.
If markdown parsing fails, the loader logs the error and skips that file while processing others.
Only one createSitemap call is allowed per file:
// ✓ Valid
export const sitemap = createSitemap(import.meta.url, { ... });
// ✗ Invalid - multiple calls
export const sitemap1 = createSitemap(import.meta.url, { ... });
export const sitemap2 = createSitemap(import.meta.url, { ... });
createSitemap - Factory function for defining sitemapsloadServerSitemap - Runtime sitemap loading from index filesloadServerPageIndex - Loads individual page metadata at runtimetransformMarkdownMetadata - Extracts metadata from MDX fileswithDocsInfra - Configures all docs infrastructure loaders