diff --git a/www/apps/book/app/md-content/[...slug]/route.ts b/www/apps/book/app/md-content/[[...slug]]/route.ts similarity index 84% rename from www/apps/book/app/md-content/[...slug]/route.ts rename to www/apps/book/app/md-content/[[...slug]]/route.ts index 35a39fe8ae..0926eeaf71 100644 --- a/www/apps/book/app/md-content/[...slug]/route.ts +++ b/www/apps/book/app/md-content/[[...slug]]/route.ts @@ -1,5 +1,5 @@ import { getCleanMd } from "docs-utils" -import { existsSync } from "fs" +import { existsSync, readFileSync } from "fs" import { unstable_cache } from "next/cache" import { notFound } from "next/navigation" import { NextRequest, NextResponse } from "next/server" @@ -16,7 +16,21 @@ type Params = { } export async function GET(req: NextRequest, { params }: Params) { - const { slug } = await params + const { slug = ["/"] } = await params + + if (slug[0] === "/") { + const llmsFile = readFileSync( + path.join(process.cwd(), "public", "llms.txt"), + "utf-8" + ) + + return new NextResponse(llmsFile, { + headers: { + "Content-Type": "text/markdown", + }, + status: 200, + }) + } // keep this so that Vercel keeps the files in deployment const basePath = path.join(process.cwd(), "app") diff --git a/www/apps/book/middleware.ts b/www/apps/book/middleware.ts deleted file mode 100644 index e08d3efa9d..0000000000 --- a/www/apps/book/middleware.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { NextResponse } from "next/server" -import type { NextRequest } from "next/server" - -export function middleware(request: NextRequest) { - const path = request.nextUrl.pathname.replace("/index.html.md", "") - return NextResponse.rewrite( - new URL(`/md-content${path.replace("/index.html.md", "")}`, request.url) - ) -} - -export const config = { - matcher: "/((?!resources|api|ui|user-guide|cloud).*)index.html.md", -} diff --git a/www/apps/book/next.config.mjs b/www/apps/book/next.config.mjs index 80e536240a..53d2381551 100644 --- a/www/apps/book/next.config.mjs +++ b/www/apps/book/next.config.mjs @@ -115,6 +115,35 @@ const nextConfig = { transpilePackages: ["docs-ui"], async rewrites() { return { + beforeFiles: [ + { + source: + "/:path((?!resources|api|ui|user-guide|cloud).*)index.html.md", + destination: "/md-content/:path*", + }, + { + source: "/:path*", + has: [ + { + type: "header", + key: "Accept", + value: "text/markdown", + }, + ], + destination: "/md-content/:path*", + }, + { + source: "/:path*", + has: [ + { + type: "header", + key: "Accept", + value: "text/plain", + }, + ], + destination: "/md-content/:path*", + }, + ], fallback: [ { source: "/resources", diff --git a/www/apps/cloud/middleware.ts b/www/apps/cloud/middleware.ts deleted file mode 100644 index 45470c2329..0000000000 --- a/www/apps/cloud/middleware.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { NextResponse } from "next/server" -import type { NextRequest } from "next/server" - -export function middleware(request: NextRequest) { - return NextResponse.rewrite( - new URL( - `${request.nextUrl.basePath}/md-content${request.nextUrl.pathname.replace("/index.html.md", "")}`, - request.url - ) - ) -} - -export const config = { - matcher: "/:path*/index.html.md", -} diff --git a/www/apps/cloud/next.config.mjs b/www/apps/cloud/next.config.mjs index 9d09497b64..865a5f763e 100644 --- a/www/apps/cloud/next.config.mjs +++ b/www/apps/cloud/next.config.mjs @@ -152,6 +152,38 @@ const nextConfig = { permanent: true, }, ], + rewrites: async () => { + return { + beforeFiles: [ + { + source: "/:path*/index.html.md", + destination: "/md-content/:path*", + }, + { + source: "/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/markdown", + }, + ], + destination: "/md-content/:path*", + }, + { + source: "/:path*", + destination: "/md-content/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/plain", + }, + ], + }, + ], + } + }, } const withBundleAnalyzer = bundleAnalyzer({ diff --git a/www/apps/resources/middleware.ts b/www/apps/resources/middleware.ts deleted file mode 100644 index 45470c2329..0000000000 --- a/www/apps/resources/middleware.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { NextResponse } from "next/server" -import type { NextRequest } from "next/server" - -export function middleware(request: NextRequest) { - return NextResponse.rewrite( - new URL( - `${request.nextUrl.basePath}/md-content${request.nextUrl.pathname.replace("/index.html.md", "")}`, - request.url - ) - ) -} - -export const config = { - matcher: "/:path*/index.html.md", -} diff --git a/www/apps/resources/next.config.mjs b/www/apps/resources/next.config.mjs index 68d7d5cff6..7fbca316f0 100644 --- a/www/apps/resources/next.config.mjs +++ b/www/apps/resources/next.config.mjs @@ -286,6 +286,38 @@ const nextConfig = { experimental: { optimizePackageImports: ["@medusajs/icons", "@medusajs/ui", "elkjs"], }, + rewrites: async () => { + return { + beforeFiles: [ + { + source: "/:path*/index.html.md", + destination: "/md-content/:path*", + }, + { + source: "/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/markdown", + }, + ], + destination: "/md-content/:path*", + }, + { + source: "/:path*", + destination: "/md-content/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/plain", + }, + ], + }, + ], + } + }, } const withBundleAnalyzer = bundleAnalyzer({ diff --git a/www/apps/ui/middleware.ts b/www/apps/ui/middleware.ts deleted file mode 100644 index 45470c2329..0000000000 --- a/www/apps/ui/middleware.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { NextResponse } from "next/server" -import type { NextRequest } from "next/server" - -export function middleware(request: NextRequest) { - return NextResponse.rewrite( - new URL( - `${request.nextUrl.basePath}/md-content${request.nextUrl.pathname.replace("/index.html.md", "")}`, - request.url - ) - ) -} - -export const config = { - matcher: "/:path*/index.html.md", -} diff --git a/www/apps/ui/next.config.mjs b/www/apps/ui/next.config.mjs index 8cec3b84b0..0dfcbef752 100644 --- a/www/apps/ui/next.config.mjs +++ b/www/apps/ui/next.config.mjs @@ -141,6 +141,38 @@ const nextConfig = { experimental: { optimizePackageImports: ["@medusajs/icons", "@medusajs/ui"], }, + rewrites: async () => { + return { + beforeFiles: [ + { + source: "/:path*/index.html.md", + destination: "/md-content/:path*", + }, + { + source: "/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/markdown", + }, + ], + destination: "/md-content/:path*", + }, + { + source: "/:path*", + destination: "/md-content/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/plain", + }, + ], + }, + ], + } + }, } const withBundleAnalyzer = bundleAnalyzer({ diff --git a/www/apps/user-guide/middleware.ts b/www/apps/user-guide/middleware.ts deleted file mode 100644 index 45470c2329..0000000000 --- a/www/apps/user-guide/middleware.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { NextResponse } from "next/server" -import type { NextRequest } from "next/server" - -export function middleware(request: NextRequest) { - return NextResponse.rewrite( - new URL( - `${request.nextUrl.basePath}/md-content${request.nextUrl.pathname.replace("/index.html.md", "")}`, - request.url - ) - ) -} - -export const config = { - matcher: "/:path*/index.html.md", -} diff --git a/www/apps/user-guide/next.config.mjs b/www/apps/user-guide/next.config.mjs index f67ccd7995..1f1eae4180 100644 --- a/www/apps/user-guide/next.config.mjs +++ b/www/apps/user-guide/next.config.mjs @@ -129,6 +129,38 @@ const nextConfig = { experimental: { optimizePackageImports: ["@medusajs/icons", "@medusajs/ui"], }, + rewrites: async () => { + return { + beforeFiles: [ + { + source: "/:path*/index.html.md", + destination: "/md-content/:path*", + }, + { + source: "/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/markdown", + }, + ], + destination: "/md-content/:path*", + }, + { + source: "/:path*", + destination: "/md-content/:path*", + has: [ + { + type: "header", + key: "accept", + value: "text/plain", + }, + ], + }, + ], + } + }, } const withBundleAnalyzer = bundleAnalyzer({