From cbd3d28a456bcdc08a29fde63b6c9d7b75adf4a5 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 15 May 2026 13:42:44 +0530 Subject: [PATCH] fix: move API markdown route to utils to prevent SSR interception The catch-all `apis/[...slug].md.ts` route intercepted all `/apis/**` requests. For non-`.md` paths it returned undefined, which Nitro treated as empty 200, blocking SSR. Moved to a util function called from the root `[...slug].md.ts` handler instead. Co-Authored-By: Claude Opus 4.6 (1M context) --- packages/chronicle/src/server/routes/[...slug].md.ts | 6 +++++- .../apis/[...slug].md.ts => utils/api-markdown.ts} | 9 +++------ 2 files changed, 8 insertions(+), 7 deletions(-) rename packages/chronicle/src/server/{routes/apis/[...slug].md.ts => utils/api-markdown.ts} (96%) diff --git a/packages/chronicle/src/server/routes/[...slug].md.ts b/packages/chronicle/src/server/routes/[...slug].md.ts index 8075f815..8e317b3d 100644 --- a/packages/chronicle/src/server/routes/[...slug].md.ts +++ b/packages/chronicle/src/server/routes/[...slug].md.ts @@ -3,11 +3,15 @@ import matter from 'gray-matter'; import { defineHandler, HTTPError } from 'nitro'; import { getPage, getOriginalPath } from '@/lib/source'; import { safePath } from '@/server/utils/safe-path'; +import { handleApiMarkdown } from '@/server/utils/api-markdown'; export default defineHandler(async event => { const pathname = event.path || event.req.url?.split('?')[0] || ''; if (!pathname.endsWith('.md')) return; - if (pathname.startsWith('/apis/')) return; + + if (pathname.startsWith('/apis/')) { + return handleApiMarkdown(pathname); + } const stripped = pathname.replace(/\.md$/, ''); const parts = stripped === '/index' || stripped === '/' diff --git a/packages/chronicle/src/server/routes/apis/[...slug].md.ts b/packages/chronicle/src/server/utils/api-markdown.ts similarity index 96% rename from packages/chronicle/src/server/routes/apis/[...slug].md.ts rename to packages/chronicle/src/server/utils/api-markdown.ts index 48739347..87bc32e2 100644 --- a/packages/chronicle/src/server/routes/apis/[...slug].md.ts +++ b/packages/chronicle/src/server/utils/api-markdown.ts @@ -1,15 +1,12 @@ import type { OpenAPIV3 } from 'openapi-types' -import { defineHandler, HTTPError } from 'nitro' +import { HTTPError } from 'nitro' import { loadConfig } from '@/lib/config' import { loadApiSpecs } from '@/lib/openapi' import { findApiOperation } from '@/lib/api-routes' import { flattenSchema, generateExampleJson, type SchemaField } from '@/lib/schema' import { generateCurl } from '@/lib/snippet-generators' -export default defineHandler(async event => { - const pathname = event.path || event.req.url?.split('?')[0] || '' - if (!pathname.endsWith('.md')) return - +export async function handleApiMarkdown(pathname: string) { const stripped = pathname.replace(/\.md$/, '').replace(/^\/apis\//, '') const slug = stripped.split('/').filter(Boolean) if (slug.length < 2) { @@ -26,7 +23,7 @@ export default defineHandler(async event => { const md = generateApiMarkdown(match.method, match.path, match.operation, match.spec.server.url, match.spec.auth) return new Response(md, { headers: { 'Content-Type': 'text/markdown; charset=utf-8' } }) -}) +} function generateApiMarkdown( method: string,