An MDX content loader for Next.js that reads slug and locale from a filename like foo.de.mdx instead of frontmatter. New pages become two files, and a missing language becomes a fallback instead of an error.
Problem
Every project page, note and lab experiment here exists in German and English. Writing slug and language into every frontmatter means keeping two places in sync that can drift: file foo.de.mdx, frontmatter slug: foo-de, and the link points somewhere other than the file.
Solution
The content loader in lib/content.ts reads slug and locale straight off the filename instead of asking for both again in frontmatter.
function slugFromFile(name: string): { slug: string; locale: Locale } | null {
const m = name.match(/^(.+)\.(de|en)\.mdx$/);
if (!m) return null;
return { slug: m[1], locale: m[2] as Locale };
}
A new project is therefore two files: content/projects/foo.de.mdx and foo.en.mdx. No route, no index, no registry entry. generateStaticParams pulls the same slugs out of that directory at build time and the detail page comes along statically:
export async function generateStaticParams() {
const both = await Promise.all([listProjects("de"), listProjects("en")]);
const slugs = new Set(both.flat().map((p) => p.slug));
return [...slugs].flatMap((slug) => [{ lang: "de", slug }, { lang: "en", slug }]);
}
The loader overwrites any slug field from frontmatter with the one from the filename. The file is the only source.
Side effect: fallback instead of error
Only noticed later: a missing language is not an error, it is a fallback. The loader takes the other file and flags the entry with fallback: true. That is why a German-only note is allowed to sit here.
For search engines the fallback needs a rule: the page in the wrong language gets noindex and a canonical to the original language, and the sitemap does not list it at all. Otherwise the same German text would be indexed under an /en/ URL.
Tested with Next.js 16.2 and next-mdx-remote 6.
See also
- MDX paragraphs stick together: how the loaded files get styled.
- All projects: every page there is exactly such a file pair.
