From 4406b69e591bb103831200df03fac5e3dbb685eb Mon Sep 17 00:00:00 2001 From: Marc LeBlanc <7050295+marcleblanc2@users.noreply.github.com> Date: Thu, 10 Sep 2026 23:55:48 -0600 Subject: [PATCH] site/api-md: Prerender /api/md/* at build time Every doc page's markdown is now written as a static file during the build, so .md requests are served from the CDN instead of invoking a serverless function per request. Unknown slugs 404 without a function call (dynamicParams = false). Co-authored-by: Amp Amp-Thread-ID: https://ampcode.com/threads/T-01a0a7d0-966f-7278-9368-707fc1017ab6 --- src/app/api/md/[...slug]/route.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app/api/md/[...slug]/route.ts b/src/app/api/md/[...slug]/route.ts index e8f9b167e..a6f17633f 100644 --- a/src/app/api/md/[...slug]/route.ts +++ b/src/app/api/md/[...slug]/route.ts @@ -1,8 +1,19 @@ import {allPosts} from 'contentlayer/generated'; -import {NextRequest, NextResponse} from 'next/server'; +import {NextResponse} from 'next/server'; + +// Prerender every post's markdown at build time; unknown slugs 404 without +// invoking a function. +export const dynamicParams = false; + +export function generateStaticParams() { + // The root index.mdx has an empty path; a catch-all needs at least one segment. + return allPosts + .filter(post => post._raw.flattenedPath !== '') + .map(post => ({slug: post._raw.flattenedPath.split('/')})); +} export async function GET( - _: NextRequest, + _: Request, {params}: {params: Promise<{slug: string[]}>} ) { const docPath = (await params).slug.join('/');