From cf657cc2ec7198754f569d195e7bcc721364dc83 Mon Sep 17 00:00:00 2001 From: mantrakp04 Date: Mon, 23 Mar 2026 10:36:26 -0700 Subject: [PATCH] Update LLM route handling and improve URL validation ### Summary of Changes - Set `revalidate` to false for statically generated page content paths in `route.ts`. - Updated the redirect URL in the LLM route to point to `/llms.txt` instead of the root. - Enhanced URL validation in `llms.txt` to ensure proper prefixes for documentation and API pages, throwing errors for unexpected URLs. These changes improve the routing logic and ensure better handling of page URLs. --- docs/src/app/llms.mdx/[[...slug]]/route.ts | 2 +- docs/src/app/llms.txt/route.ts | 10 ++++++++-- docs/src/app/llms/[[...slug]]/route.ts | 2 ++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/src/app/llms.mdx/[[...slug]]/route.ts b/docs/src/app/llms.mdx/[[...slug]]/route.ts index 2d4f35086..738007b4c 100644 --- a/docs/src/app/llms.mdx/[[...slug]]/route.ts +++ b/docs/src/app/llms.mdx/[[...slug]]/route.ts @@ -19,7 +19,7 @@ export async function GET( } if (!page) { - return NextResponse.redirect(new URL('/', request.url), 307); + return NextResponse.redirect(new URL('/llms.txt', request.url), 307); } try { diff --git a/docs/src/app/llms.txt/route.ts b/docs/src/app/llms.txt/route.ts index 2209e1e5e..bc97d3ba9 100644 --- a/docs/src/app/llms.txt/route.ts +++ b/docs/src/app/llms.txt/route.ts @@ -12,14 +12,20 @@ export async function GET(request: Request) { const apiBaseUrl = `${origin}/llms/api/`; for (const page of source.getPages()) { - const relativeUrl = page.url.replace(/^\/docs\/?/, ''); + if (page.url !== '/docs' && !page.url.startsWith('/docs/')) { + throw new Error(`Unexpected page URL "${page.url}" in docs source — expected "/docs" or "/docs/..." prefix`); + } + const relativeUrl = page.url === '/docs' ? '' : page.url.slice('/docs/'.length); if (relativeUrl !== '') { docsUrls.add(relativeUrl); } } for (const page of apiSource.getPages()) { - const relativeUrl = page.url.replace(/^\/api\/?/, ''); + if (page.url !== '/api' && !page.url.startsWith('/api/')) { + throw new Error(`Unexpected page URL "${page.url}" in API source — expected "/api" or "/api/..." prefix`); + } + const relativeUrl = page.url === '/api' ? '' : page.url.slice('/api/'.length); if (relativeUrl !== '') { apiUrls.add(relativeUrl); } diff --git a/docs/src/app/llms/[[...slug]]/route.ts b/docs/src/app/llms/[[...slug]]/route.ts index 34327b80e..5b83f0f30 100644 --- a/docs/src/app/llms/[[...slug]]/route.ts +++ b/docs/src/app/llms/[[...slug]]/route.ts @@ -2,6 +2,8 @@ import { NextResponse, type NextRequest } from 'next/server'; import { getLLMText } from '../../../../lib/get-llm-text'; import { apiSource, source } from '../../../../lib/source'; +// revalidate = false applies only to the statically generated page content paths +// (those emitted by generateStaticParams). The empty-slug redirect is always dynamic. export const revalidate = false; function resolvePage(slug: string[] | undefined) {