Refactor LLM route handling and improve URL generation

### Summary of Changes
- Replaced `NextResponse.redirect` with `notFound()` for better error handling when a page is not found in the LLM route.
- Simplified the `GET` function in `llms.txt` to dynamically generate base URLs for documentation and API pages based on the request URL.
- Removed unnecessary try-catch blocks in both routes to streamline the code.

These updates enhance the clarity and efficiency of the LLM routing logic.
This commit is contained in:
mantrakp04 2026-03-20 15:09:34 -07:00
parent 35b7ac78d2
commit 12bf5e0ecb
2 changed files with 19 additions and 26 deletions

View File

@ -4,9 +4,11 @@ import { apiSource, source } from 'lib/source';
// cached forever
export const revalidate = false;
export async function GET() {
export async function GET(request: Request) {
const docsUrls = new Set<string>();
const apiUrls = new Set<string>();
const docsBaseUrl = new URL('/llms/docs/', request.url).toString();
const apiBaseUrl = new URL('/llms/api/', request.url).toString();
for (const page of source.getPages()) {
const relativeUrl = page.url.replace(/^\/docs\/?/, '');
@ -24,11 +26,11 @@ export async function GET() {
const body = [
'# Stack Auth Docs',
'docs base url: https://docs.stack-auth.com/llms/docs/',
`docs base url: ${docsBaseUrl}`,
'',
...[...docsUrls].sort((left, right) => stringCompare(left, right)),
'',
'api base url: https://docs.stack-auth.com/llms/api/',
`api base url: ${apiBaseUrl}`,
'',
...[...apiUrls].sort((left, right) => stringCompare(left, right)),
'',

View File

@ -1,3 +1,4 @@
import { notFound } from 'next/navigation';
import { NextResponse, type NextRequest } from 'next/server';
import { getLLMText } from '../../../../lib/get-llm-text';
import { apiSource, source } from '../../../../lib/source';
@ -35,33 +36,23 @@ export async function GET(
const page = resolvePage(slug);
if (!page) {
return NextResponse.redirect(new URL('/', request.url), 307);
notFound();
}
try {
return new NextResponse(await getLLMText(page), {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
});
} catch (error) {
console.error('Error generating LLM text:', error);
return new NextResponse('Error generating content', { status: 500 });
}
return new NextResponse(await getLLMText(page), {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
});
}
export function generateStaticParams() {
try {
const docsParams = source.generateParams().map((param) => ({
slug: ['docs', ...param.slug],
}));
const apiParams = apiSource.generateParams().map((param) => ({
slug: ['api', ...param.slug],
}));
const docsParams = source.generateParams().map((param) => ({
slug: ['docs', ...param.slug],
}));
const apiParams = apiSource.generateParams().map((param) => ({
slug: ['api', ...param.slug],
}));
return [...docsParams, ...apiParams];
} catch (error) {
console.error('Error generating static params:', error);
return [];
}
return [...docsParams, ...apiParams];
}