|
| 1 | +// This function provides the desired server dance for .md endpoints |
| 2 | +// that perform Markdown content negotiation for AI agents |
| 3 | +import type { Context } from "@netlify/edge-functions"; |
| 4 | + |
| 5 | +function prefersMarkdown(accept: string | null): boolean { |
| 6 | + if (!accept) return false; |
| 7 | + let md = 0; |
| 8 | + let html = 0; |
| 9 | + for (const part of accept.split(",")) { |
| 10 | + const [type, ...params] = part |
| 11 | + .trim() |
| 12 | + .split(";") |
| 13 | + .map((s) => s.trim()); |
| 14 | + const qParam = params.find((p) => p.startsWith("q=")); |
| 15 | + const q = qParam ? parseFloat(qParam.slice(2)) : 1; |
| 16 | + if (Number.isNaN(q)) continue; |
| 17 | + if (type === "text/markdown") md = Math.max(md, q); |
| 18 | + else if (type === "text/html" || type === "application/xhtml+xml") { |
| 19 | + html = Math.max(html, q); |
| 20 | + } |
| 21 | + } |
| 22 | + return md > 0 && md >= html; |
| 23 | +} |
| 24 | + |
| 25 | +export default async (request: Request, context: Context) => { |
| 26 | + const url = new URL(request.url); |
| 27 | + |
| 28 | + const looksLikeAsset = /\.[a-z0-9]+$/i.test(url.pathname); |
| 29 | + if (request.method !== "GET" || looksLikeAsset) { |
| 30 | + return context.next(); |
| 31 | + } |
| 32 | + |
| 33 | + const accept = request.headers.get("Accept"); |
| 34 | + const mdPath = url.pathname.replace(/\/$/, "") + ".md"; |
| 35 | + const mdUrl = new URL(mdPath, url); |
| 36 | + |
| 37 | + if (prefersMarkdown(accept)) { |
| 38 | + try { |
| 39 | + const mdResponse = await fetch(mdUrl, { |
| 40 | + headers: { "User-Agent": request.headers.get("User-Agent") ?? "" }, |
| 41 | + }); |
| 42 | + const upstreamCt = mdResponse.headers.get("Content-Type") ?? ""; |
| 43 | + if (mdResponse.ok && upstreamCt.includes("markdown")) { |
| 44 | + return new Response(mdResponse.body, { |
| 45 | + status: 200, |
| 46 | + headers: { |
| 47 | + "Content-Type": "text/markdown; charset=utf-8", |
| 48 | + Vary: "Accept", |
| 49 | + "Cache-Control": "public, max-age=3600", |
| 50 | + Link: `<${url.href}>; rel="canonical"`, |
| 51 | + }, |
| 52 | + }); |
| 53 | + } |
| 54 | + } catch { |
| 55 | + // Upstream .md fetch failed; falling through to normal HTML response |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Fell through to normal HTML response due to lack of .md sibling |
| 60 | + const response = await context.next(); |
| 61 | + const ct = response.headers.get("Content-Type") ?? ""; |
| 62 | + if (ct.includes("text/html")) { |
| 63 | + response.headers.append( |
| 64 | + "Link", |
| 65 | + `<${mdUrl.href}>; rel="alternate"; type="text/markdown"`, |
| 66 | + ); |
| 67 | + response.headers.append("Vary", "Accept"); |
| 68 | + } |
| 69 | + return response; |
| 70 | +}; |
0 commit comments