Enhance middleware and route handling for LLMs

### Summary of Changes
- Updated middleware to include additional redirects for `/skill.md` and `/skills.md` to `/llms.txt`.
- Refactored redirect logic in LLM routes to use `NextResponse.redirect` for improved clarity and consistency.
- Adjusted URL handling in `llms.txt` to ensure proper relative URL extraction for documentation and API pages.

These changes improve the routing logic and ensure better handling of various URL formats.
This commit is contained in:
mantrakp04 2026-03-20 15:08:13 -07:00
parent 58f8d23a6c
commit 35b7ac78d2
4 changed files with 18 additions and 8 deletions

View File

@ -1,4 +1,3 @@
import { redirect } from 'next/navigation';
import { NextResponse, type NextRequest } from 'next/server';
import { getLLMText } from '../../../../lib/get-llm-text';
import { apiSource, source } from '../../../../lib/source';
@ -6,7 +5,7 @@ import { apiSource, source } from '../../../../lib/source';
export const revalidate = false;
export async function GET(
_req: NextRequest,
request: NextRequest,
{ params }: { params: Promise<{ slug?: string[] }> },
) {
const { slug } = await params;
@ -19,7 +18,9 @@ export async function GET(
page = apiSource.getPage(slug);
}
if (!page) redirect("/");
if (!page) {
return NextResponse.redirect(new URL('/', request.url), 307);
}
try {
return new NextResponse(await getLLMText(page));

View File

@ -9,11 +9,17 @@ export async function GET() {
const apiUrls = new Set<string>();
for (const page of source.getPages()) {
docsUrls.add(`/llms${page.url}`.slice('/llms/docs/'.length));
const relativeUrl = page.url.replace(/^\/docs\/?/, '');
if (relativeUrl !== '') {
docsUrls.add(relativeUrl);
}
}
for (const page of apiSource.getPages()) {
apiUrls.add(`/llms${page.url}`.slice('/llms/api/'.length));
const relativeUrl = page.url.replace(/^\/api\/?/, '');
if (relativeUrl !== '') {
apiUrls.add(relativeUrl);
}
}
const body = [

View File

@ -1,4 +1,3 @@
import { redirect } from 'next/navigation';
import { NextResponse, type NextRequest } from 'next/server';
import { getLLMText } from '../../../../lib/get-llm-text';
import { apiSource, source } from '../../../../lib/source';
@ -36,7 +35,7 @@ export async function GET(
const page = resolvePage(slug);
if (!page) {
redirect("/");
return NextResponse.redirect(new URL('/', request.url), 307);
}
try {

View File

@ -18,7 +18,9 @@ export default function middleware(request: NextRequest, event: NextFetchEvent)
if (
pathname === '/SKILL.md' ||
pathname === '/SKILLS.md'
pathname === '/SKILLS.md' ||
pathname === '/skill.md' ||
pathname === '/skills.md'
) {
const url = request.nextUrl.clone();
url.pathname = '/llms.txt';
@ -81,6 +83,8 @@ export const config = {
matcher: [
'/SKILL.md',
'/SKILLS.md',
'/skill.md',
'/skills.md',
'/docs/:path*',
'/api/:path*',
],