|
| 1 | +import type { Config, Context } from "@netlify/edge-functions"; |
| 2 | +import { extname } from "path"; |
| 3 | + |
| 4 | +const LLMS_REWRITES = new Set(["/llms.txt", "/llms-full.txt"]); |
| 5 | + |
| 6 | +export default async function handler(request: Request, context: Context) { |
| 7 | + console.log("Received request:", request); |
| 8 | + // Only handle GET and HEAD requests |
| 9 | + if (request.method !== "GET" && request.method !== "HEAD") return; |
| 10 | + |
| 11 | + // Skip Netlify's internal prerender/CDN requests (HeadlessChrome, CloudFront) |
| 12 | + const agentCategory = request.headers.get("netlify-agent-category") || ""; |
| 13 | + if (agentCategory.includes("prerender")) return; |
| 14 | + |
| 15 | + // Skip our own Algolia crawler — it follows rel="alternate" links and |
| 16 | + // would otherwise index the .md variants. |
| 17 | + const userAgent = request.headers.get("user-agent") || ""; |
| 18 | + if (/algolia/i.test(userAgent)) return; |
| 19 | + |
| 20 | + const url = new URL(request.url); |
| 21 | + const { pathname } = url; |
| 22 | + |
| 23 | + // Respond with index.md for llms.txt and llms-full.txt, |
| 24 | + // as index.md is well suited for this |
| 25 | + if (LLMS_REWRITES.has(pathname)) { |
| 26 | + return buildTarget("/index.md", url); |
| 27 | + } |
| 28 | + |
| 29 | + const ext = extname(pathname); |
| 30 | + if (ext === ".html" || ext === ".md") { |
| 31 | + // For direct requests to .html or .md files, |
| 32 | + // add a link header pointing to the alternate format. |
| 33 | + return modifyHeaders(await context.next(), (headers) => { |
| 34 | + addAlternateLink(headers, url); |
| 35 | + }); |
| 36 | + } else if (ext) { |
| 37 | + // Skip other requests with file extensions, |
| 38 | + // as they are static assets that shouldn't have alternate links. |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // For other requests, check if the client prefers Markdown over HTML. |
| 43 | + // If so, try to serve the corresponding Markdown file |
| 44 | + // (e.g., /foo -> /foo/index.md). |
| 45 | + // If the Markdown file doesn't exist (404), |
| 46 | + // continue with the normal request handling. |
| 47 | + if (prefersMarkdown(request.headers.get("accept"))) { |
| 48 | + const target = buildTarget(joinIndex(pathname), url); |
| 49 | + const response = await fetch(target); |
| 50 | + if (response.status !== 404) return finalize(response, url); |
| 51 | + } |
| 52 | + |
| 53 | + // For all other cases, proceed with the normal request handling. |
| 54 | + return finalize(await context.next(), url); |
| 55 | +} |
| 56 | + |
| 57 | +function buildTarget(pathname: string, base: URL): URL { |
| 58 | + const target = new URL(pathname, base); |
| 59 | + target.search = base.search; |
| 60 | + return target; |
| 61 | +} |
| 62 | + |
| 63 | +function joinIndex(pathname: string): string { |
| 64 | + return pathname.replace(/\/?$/, "/") + "index.md"; |
| 65 | +} |
| 66 | + |
| 67 | +function prefersMarkdown(accept: string | null): boolean { |
| 68 | + if (!accept) return false; |
| 69 | + |
| 70 | + let markdownQ = -1; |
| 71 | + let htmlQ = -1; |
| 72 | + let textQ = -1; |
| 73 | + let anyQ = -1; |
| 74 | + |
| 75 | + for (const part of accept.split(",")) { |
| 76 | + const segments = part.trim().split(";"); |
| 77 | + const type = segments[0].trim().toLowerCase(); |
| 78 | + if (!type) continue; |
| 79 | + |
| 80 | + let q = 1; |
| 81 | + for (let i = 1; i < segments.length; i++) { |
| 82 | + const param = segments[i].trim(); |
| 83 | + if (!param.startsWith("q=")) continue; |
| 84 | + const value = Number.parseFloat(param.slice(2)); |
| 85 | + if (!Number.isNaN(value)) q = value; |
| 86 | + } |
| 87 | + |
| 88 | + if (type === "text/markdown") { |
| 89 | + if (q > markdownQ) markdownQ = q; |
| 90 | + } else if (type === "text/html") { |
| 91 | + if (q > htmlQ) htmlQ = q; |
| 92 | + } else if (type === "text/*") { |
| 93 | + if (q > textQ) textQ = q; |
| 94 | + } else if (type === "*/*") { |
| 95 | + if (q > anyQ) anyQ = q; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (htmlQ < 0) htmlQ = textQ > 0 ? textQ : anyQ; |
| 100 | + |
| 101 | + return markdownQ > 0 && markdownQ >= htmlQ; |
| 102 | +} |
| 103 | + |
| 104 | +function finalize(response: Response, url: URL): Response { |
| 105 | + return modifyHeaders(response, (headers) => { |
| 106 | + appendVary(headers, "Accept"); |
| 107 | + addAlternateLink(headers, new URL(response.url, url)); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +function modifyHeaders( |
| 112 | + response: Response, |
| 113 | + fn: (headers: Headers) => void, |
| 114 | +): Response { |
| 115 | + const headers = new Headers(response.headers); |
| 116 | + |
| 117 | + fn(headers); |
| 118 | + |
| 119 | + return new Response(response.body, { |
| 120 | + status: response.status, |
| 121 | + statusText: response.statusText, |
| 122 | + headers, |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +function appendVary(headers: Headers, value: string) { |
| 127 | + const existing = headers.get("vary"); |
| 128 | + if (!existing) { |
| 129 | + headers.set("vary", value); |
| 130 | + return; |
| 131 | + } |
| 132 | + const tokens = existing.split(",").map((s) => s.trim()); |
| 133 | + if (tokens.some((t) => t.toLowerCase() === value.toLowerCase())) return; |
| 134 | + headers.set("vary", `${existing}, ${value}`); |
| 135 | +} |
| 136 | + |
| 137 | +function addAlternateLink(headers: Headers, url: URL) { |
| 138 | + let alternatePath: string | null = null; |
| 139 | + let alternateType = "text/markdown"; |
| 140 | + |
| 141 | + const ext = extname(url.pathname); |
| 142 | + if (ext === ".html") { |
| 143 | + alternatePath = url.pathname.replace(/\.html$/, ".md"); |
| 144 | + } else if (ext === ".md") { |
| 145 | + alternatePath = url.pathname.replace(/\.md$/, ".html"); |
| 146 | + alternateType = "text/html"; |
| 147 | + } else if (ext === "") { |
| 148 | + alternatePath = joinIndex(url.pathname); |
| 149 | + } |
| 150 | + |
| 151 | + if (!alternatePath) return; |
| 152 | + |
| 153 | + const alternateUrl = buildTarget(alternatePath, url); |
| 154 | + |
| 155 | + const link = `<${alternateUrl}>; rel="alternate"; type="${alternateType}"`; |
| 156 | + headers.set("link", link); |
| 157 | +} |
| 158 | + |
| 159 | +export const config: Config = { |
| 160 | + path: "/*", |
| 161 | + excludedPath: [ |
| 162 | + "/**/*.js", |
| 163 | + "/**/*.css", |
| 164 | + "/**/*.png", |
| 165 | + "/**/*.jpg", |
| 166 | + "/**/*.jpeg", |
| 167 | + "/**/*.svg", |
| 168 | + "/**/*.ico", |
| 169 | + "/**/*.xml", |
| 170 | + "/robots.txt", |
| 171 | + "/404.html", |
| 172 | + "/_redirects", |
| 173 | + "/.nojekyll", |
| 174 | + ], |
| 175 | +}; |
0 commit comments