mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-30 21:01:54 +08:00
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:
parent
58f8d23a6c
commit
35b7ac78d2
@ -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));
|
||||
|
||||
@ -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 = [
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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*',
|
||||
],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user