export const onRequest: PagesFunction<{ ASSETS: Fetcher }> = async (context) => { const url = new URL(context.request.url); // The app-specific guide now lives in the registry integration section. if (/^\/api-sync\/attio(?:\/|\.md)?$/.test(url.pathname)) { url.pathname = url.pathname.endsWith('.md') ? '/integrations/attio.md' : '/integrations/attio/'; return Response.redirect(url.toString(), 301); } // The product landing and CLI overview now live under /sync/. // Redirect their old HTML and Markdown URLs before the legacy guide rule. if (/^\/ingestion(?:\/cli)?(?:\/|\.md)?$/.test(url.pathname)) { url.pathname = url.pathname.replace(/^\/ingestion/, '/sync'); return Response.redirect(url.toString(), 301); } // Older source-authoring guides moved to /api-sync/. Keep those published // links working, including .md URLs. const moved = url.pathname.match(/^\/ingestion(\/.*|\.md)?$/); if (moved) { url.pathname = `/api-sync${moved[1] ?? '/'}`; return Response.redirect(url.toString(), 301); } // Serve the raw Markdown asset behind /_raw/ as text/markdown. async function serveRaw(slug: string): Promise { const assetUrl = new URL(`/_raw/${slug}.md`, url.origin); const asset = await context.env.ASSETS.fetch(assetUrl.toString()); if (!asset.ok) return null; return new Response(asset.body, { status: 200, headers: { 'Content-Type': 'text/markdown; charset=utf-8', 'Vary': 'Accept', }, }); } // 1. Explicit ".md" URL (e.g. /ai-agents.md) — serve raw Markdown directly, // no Accept header required. Skip assets already under /_raw/. if (url.pathname.endsWith('.md') && !url.pathname.startsWith('/_raw/')) { const slug = url.pathname.replace(/^\//, '').replace(/\.md$/, ''); const raw = await serveRaw(slug); if (raw) return raw; } // 2. Content negotiation — same HTML URL, served as Markdown when the // client explicitly asks for it. Ignore Accept: */* so browsers get HTML. const accept = context.request.headers.get('Accept') ?? ''; if (accept.includes('text/markdown') && !accept.startsWith('*/*')) { const slug = (url.pathname.replace(/\/$/, '') || '/index').replace(/^\//, ''); const raw = await serveRaw(slug); if (raw) return raw; } const response = await context.next(); const patched = new Response(response.body, response); patched.headers.set('Vary', 'Accept'); return patched; };