Skip to content
91 changes: 91 additions & 0 deletions apps/framework-docs/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

export function middleware(request: NextRequest) {
const match = matchLlmPath(request.nextUrl.pathname);

if (!match) {
return NextResponse.next();
}

const rewriteUrl = request.nextUrl.clone();
rewriteUrl.pathname = `/api/llm/${match.language}`;

if (match.scope) {
rewriteUrl.searchParams.set("scope", match.scope);
} else {
rewriteUrl.searchParams.delete("scope");
}

return NextResponse.rewrite(rewriteUrl);
}

interface LlmPathMatch {
language: "py" | "ts";
scope?: string;
}

function matchLlmPath(pathname: string): LlmPathMatch | null {
const trimmed = pathname.replace(/\/+$/, "");
if (!trimmed) {
return null;
}

const segments = trimmed.split("/").filter(Boolean);
if (segments.length === 0) {
return null;
}

const filename = segments.pop();
if (!filename) {
return null;
}
const language = extractLanguage(filename);
if (!language) {
return null;
}

const scopeSegments = segments.slice();
const scopeSuffix = extractScopeSuffix(filename, language);

if (scopeSuffix) {
scopeSegments.push(scopeSuffix);
}

const scope = scopeSegments.join("/");

return {
language,
scope: scope || undefined,
};
}

function extractLanguage(filename: string): "py" | "ts" | null {
if (filename.endsWith("llm-py.txt")) {
return "py";
}

if (filename.endsWith("llm-ts.txt")) {
return "ts";
}

return null;
}

function extractScopeSuffix(
filename: string,
language: "py" | "ts",
): string | undefined {
const suffix = `llm-${language}.txt`;

if (filename === suffix) {
return undefined;
}

const scopePart = filename.slice(0, filename.length - suffix.length);
return scopePart.replace(/^-+|-+$/g, "") || undefined;
}

export const config = {
matcher: ["/((?!api/|_next/|static/|favicon\\.ico).*)"],
};
23 changes: 22 additions & 1 deletion apps/framework-docs/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const nextConfig = {
},
// Optional build-time configuration options
async rewrites() {
return [
const baseRewrites = [
{
source: "/robots.txt",
destination: "/api/robots.txt",
Expand All @@ -58,6 +58,27 @@ const nextConfig = {
destination: "https://us.i.posthog.com/decide",
},
];

const llmRewrites = [
{
source: "/llm-ts.txt",
destination: "/api/llm/ts",
},
{
source: "/llm-py.txt",
destination: "/api/llm/py",
},
{
source: "/:scope*/llm-ts.txt",
destination: "/api/llm/ts?scope=:scope*",
},
{
source: "/:scope*/llm-py.txt",
destination: "/api/llm/py?scope=:scope*",
},
];

return [...baseRewrites, ...llmRewrites];
},
skipTrailingSlashRedirect: true,
};
Expand Down
Loading
Loading