{"title":"Markdown Copies Of Pages For Agents Without Hurting SEO","html_url":"https://deallocatedobjects.com/posts/markdown-copies-of-pages-for-agents-without-hurting-seo","source_url":"","markdown_content":"When I [made this blog readable by agents](/posts/making-my-blog-readable-by-ai-agents), every post got a Markdown copy at its URL plus `.md`, and the normal URL answers with Markdown when a client sends `Accept: text/markdown`. Each HTML page also links its copy with `<link rel=\"alternate\" type=\"text/markdown\">`, so agents can find it.\n\nSo can search engines. Now every page exists twice with the same text, and the copy has no `<head>` to put a canonical tag in. The fix is three response headers on the copy. Getting them to reach clients took more care than I expected, especially in Next.js.\n\n### The Three Headers\n\nHere's a Markdown copy on this blog:\n\n```shellsession\n$ curl -sI https://deallocatedobjects.com/posts/the-art-of-the-reveal.md\nHTTP/2 200\ncontent-type: text/markdown; charset=utf-8\nvary: Accept\nlink: </llms.txt>; rel=\"describedby\"; type=\"text/plain\"; title=\"Guide for AI agents\", </sitemap.xml>; rel=\"sitemap\"; type=\"application/xml\", </feed>; rel=\"alternate\"; type=\"application/atom+xml\"\nlink: <https://deallocatedobjects.com/posts/the-art-of-the-reveal>; rel=\"canonical\"\nx-robots-tag: noindex, follow\n```\n\n**`Link: <...>; rel=\"canonical\"`** says the HTML page is the real one. Google supports the canonical as an HTTP header for exactly this case, documents that aren't HTML, in its [guide to duplicate URLs](https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls). Use an absolute URL there, as the same page asks. It also says to pick one place per URL, header or `<link>` element, since giving both invites them to disagree. A Markdown file has no element, so the header is the only choice anyway.\n\n**`X-Robots-Tag: noindex, follow`** keeps the copy out of results. Google's [robots meta tag docs](https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag) say the header form is how you noindex non-HTML resources. `follow` is already Google's default (it follows links unless told `nofollow`); I spell it out so the intent is obvious to anyone reading the headers.\n\nGoogle's duplicate-URL guide does say it doesn't recommend `noindex` for choosing a canonical, \"because it will completely block the page from Search.\" For the copy, blocking it completely is what I want. The canonical header still names the page that should rank.\n\n**`Vary: Accept`** matters wherever one URL answers with HTML or Markdown depending on the request. Without it, a cache in the middle can store the Markdown and hand it to the next browser (or the reverse). The `.md` URL itself never negotiates, but sending `Vary: Accept` there is harmless, and on this blog it's on every response.\n\nOne more thing: don't disallow `*.md` in `robots.txt` to keep the copies out. Google only sees `X-Robots-Tag` and the canonical when it can fetch the URL, and [its docs](https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag) say blocked URLs have their rules \"ignored.\"\n\n### In nginx\n\nThe blog is static files behind nginx. A `map` turns the `Accept` header into a suffix, the post location rewrites to the `.md` file when it's set, and the `.md` location adds the two headers:\n\n```nginx title=\"deploy/nginx.conf\"\nlocation ~ ^/posts/[^/.]+$ {\n    if ($deallocated_md) { rewrite ^ $uri.md last; }\n    try_files $uri.html =404;\n}\nlocation ~ ^(?<md_page>/.*?)(?:index)?\\.md$ {\n    types { }\n    default_type text/markdown;\n    include snippets/deallocatedobjects-headers.conf;\n    add_header Link \"<https://deallocatedobjects.com$md_page>; rel=\\\"canonical\\\"\" always;\n    add_header X-Robots-Tag \"noindex, follow\" always;\n    try_files $uri =404;\n}\n```\n\nThe `include` line is the nginx gotcha. The shared snippet holds the security headers, `Vary: Accept` and the sitewide `Link` to `llms.txt`, and it's included at the server level. But per the [nginx docs](https://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header), `add_header` directives are inherited \"if and only if there are no `add_header` directives defined on the current level.\" The moment this location adds its own canonical, every server-level header disappears from `.md` responses, so the location includes the snippet again.\n\nThat's why the curl output above has two `link:` lines. nginx sends each `add_header` as its own line, and that's fine: [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288#section-3) defines `Link` as a comma-separated list, so two lines mean the same thing as one line with both values.\n\n### In Next.js\n\nIn a Next.js app, a proxy (Next 16's name for middleware) rewrites `/guides/foo.md`, or `/guides/foo` with `Accept: text/markdown` and no `text/html`, to a route handler that returns the copy. This is from [Rookiefeed](https://rookiefeed.com/?ref=deallocated-post):\n\n```ts title=\"src/lib/markdown.ts\"\nexport function markdownResponse(body: string, canonicalPath: string) {\n  return new Response(body, {\n    headers: {\n      \"Content-Type\": \"text/markdown; charset=utf-8\",\n      Vary: \"Accept\",\n      Link: `<${siteUrl(canonicalPath)}>; rel=\"canonical\"`,\n      \"X-Robots-Tag\": \"noindex, follow\",\n      \"Cache-Control\": \"public, max-age=600\",\n    },\n  });\n}\n```\n\nThe same app also sent a sitewide `Link` header from `next.config.ts`, pointing agents at `llms.txt` and the sitemap:\n\n```ts title=\"next.config.ts\"\nasync headers() {\n  const discovery = '</llms.txt>; rel=\"service-doc\"; type=\"text/plain\", </sitemap.xml>; rel=\"sitemap\"; type=\"application/xml\"';\n  return [\n    { source: \"/:path*\", headers: [{ key: \"Link\", value: discovery }, ...SECURITY_HEADERS] },\n  ];\n},\n```\n\nWith both in place, the `.md` responses came back with the discovery `Link` and no canonical. The route's `Link` never reached the client.\n\n### How Next Combines Them\n\nThe [`headers()` docs](https://nextjs.org/docs/app/api-reference/config/next-config-js/headers) only cover two config rules setting the same key: \"the last header key will override the first.\" They don't say what happens when a config rule and a route handler set the same header, so I read the code.\n\nConfig headers are applied first. The router collects every matching rule into one object (`resHeaders[key] = value`, so a later rule replaces an earlier one) and calls `res.setHeader` for each before your route runs. Then [`send-response.ts`](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/send-response.ts) copies your route's `Response` headers onto that same response, like this (16.4.5, trimmed):\n\n```js title=\"next/dist/server/send-response.js\"\nconst headersWithMultipleValuesAllowed = [\n    'set-cookie',\n    'www-authenticate',\n    'proxy-authenticate',\n    'vary'\n];\nresponse.headers.forEach((value, name)=>{\n    // only append the header if it is either not present in the outbound response\n    // or if the header supports multiple values\n    const isHeaderPresent = typeof res.getHeader(name) !== 'undefined';\n    if (headersWithMultipleValuesAllowed.includes(name.toLowerCase()) || !isHeaderPresent) {\n        res.appendHeader(name, value);\n    }\n});\n```\n\nSo for any header your config already set, the route's value is skipped without a warning. Four names are exceptions and get appended. `Link` isn't one of them, so the config's `Link` stays and the route's canonical is dropped. `Vary` is one of them, which is why the route's `Vary: Accept` makes it through:\n\n```shellsession\n$ curl -sI https://rookiefeed.com/guides/how-much-does-mc-authority-cost.md\nHTTP/2 200\ncontent-type: text/markdown; charset=utf-8\nvary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch, Accept, Accept-Encoding\nlink: <https://rookiefeed.com/guides/how-much-does-mc-authority-cost>; rel=\"canonical\"\nx-robots-tag: noindex, follow\n```\n\nThat's the output after the fix, which is to keep the config rule off the `.md` paths with a negative lookahead in `source`:\n\n```ts title=\"next.config.ts\"\n{ source: \"/:path*\", headers: SECURITY_HEADERS },\n// Not on .md twins: a config Link header replaces the route's own rel=\"canonical\".\n{ source: \"/:path((?!.*\\\\.md$).*)\", headers: [{ key: \"Link\", value: discovery }] },\n```\n\nIf you still want the discovery links on the copies, have the route send them itself, in the same header as the canonical. [Runheed](https://runheed.com/?ref=deallocated-post) does that:\n\n```shellsession\n$ curl -sI https://runheed.com/errors/econnreset.md\nlink: <https://runheed.com/errors/econnreset>; rel=\"canonical\", </llms.txt>; rel=\"llms-txt\", </openapi.json>; rel=\"describedby\"; type=\"application/json\", </SKILL.md>; rel=\"describedby\"; type=\"text/markdown\"\nx-robots-tag: noindex, follow\n```\n\nRookiefeed's config test now checks that the discovery rule matches `/guides/how-much-does-mc-authority-cost` and doesn't match the `.md` version, so the exclusion can't quietly go away.\n\n### Vary On The HTML Side\n\n`Vary` has its own trap, on the HTML page rather than the copy. When `/errors/econnreset` negotiates, the HTML response needs `Vary: Accept` too, and setting it in the proxy isn't enough. Next's app-page handler runs after the proxy and calls `res.setHeader('Vary', 'rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch')`, and `setHeader` replaces whatever was there.\n\nHow much that matters depends on your cache headers. Rookiefeed's pages are sent `private, no-store`, so nothing between the app and the browser keeps a copy. Runheed's are `s-maxage=31536000`, which invites a shared cache to store them, so Runheed patches `ServerResponse.prototype.setHeader` and `appendHeader` from `instrumentation.ts` to add `Accept` to `Vary` on the negotiated paths, each token once:\n\n```shellsession\n$ curl -sI https://runheed.com/errors/econnreset\ncontent-type: text/html; charset=utf-8\nlink: <https://runheed.com/errors/econnreset.md>; rel=\"alternate\"; type=\"text/markdown\", </llms.txt>; rel=\"llms-txt\", </openapi.json>; rel=\"describedby\"; type=\"application/json\", </SKILL.md>; rel=\"describedby\"; type=\"text/markdown\"\nvary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch, Accept, Accept-Encoding\n```\n\n### Checking Your Own\n\nWhatever the stack, the check is three requests:\n\n```bash\ncurl -sI https://example.com/page.md\ncurl -sI -H \"Accept: text/markdown\" https://example.com/page\ncurl -sI https://example.com/page\n```\n\nThe first two should carry the absolute canonical `Link`, `X-Robots-Tag: noindex, follow` and `Accept` in `Vary`. The third should be HTML, with `Accept` in `Vary` if the URL negotiates and your responses can be cached. Run them against the deployed site, since that's the response where config, proxy and route headers all meet.","html_content":"<p>When I <a href=\"https://deallocatedobjects.com/posts/making-my-blog-readable-by-ai-agents\">made this blog readable by agents</a>, every post got a Markdown copy at its URL plus <code>.md</code>, and the normal URL answers with Markdown when a client sends <code>Accept: text/markdown</code>. Each HTML page also links its copy with <code>&lt;link rel=&quot;alternate&quot; type=&quot;text/markdown&quot;&gt;</code>, so agents can find it.</p>\n<p>So can search engines. Now every page exists twice with the same text, and the copy has no <code>&lt;head&gt;</code> to put a canonical tag in. The fix is three response headers on the copy. Getting them to reach clients took more care than I expected, especially in Next.js.</p>\n<h2 id=\"the-three-headers\" data-level=\"3\">The Three Headers</h2>\n<p>Here’s a Markdown copy on this blog:</p>\n<figure class=\"code\" data-lang=\"shellsession\"><figcaption><span class=\"lang\">Terminal</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">$ curl -sI https://deallocatedobjects.com/posts/the-art-of-the-reveal.md</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">HTTP/2 200</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">content-type: text/markdown; charset=utf-8</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">vary: Accept</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">link: &#x3C;/llms.txt>; rel=\"describedby\"; type=\"text/plain\"; title=\"Guide for AI agents\", &#x3C;/sitemap.xml>; rel=\"sitemap\"; type=\"application/xml\", &#x3C;/feed>; rel=\"alternate\"; type=\"application/atom+xml\"</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">link: &#x3C;https://deallocatedobjects.com/posts/the-art-of-the-reveal>; rel=\"canonical\"</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">x-robots-tag: noindex, follow</span></span></code></pre></figure><p><strong><code>Link: &lt;...&gt;; rel=&quot;canonical&quot;</code></strong> says the HTML page is the real one. Google supports the canonical as an HTTP header for exactly this case, documents that aren’t HTML, in its <a href=\"https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls\">guide to duplicate URLs</a>. Use an absolute URL there, as the same page asks. It also says to pick one place per URL, header or <code>&lt;link&gt;</code> element, since giving both invites them to disagree. A Markdown file has no element, so the header is the only choice anyway.</p>\n<p><strong><code>X-Robots-Tag: noindex, follow</code></strong> keeps the copy out of results. Google’s <a href=\"https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag\">robots meta tag docs</a> say the header form is how you noindex non-HTML resources. <code>follow</code> is already Google’s default (it follows links unless told <code>nofollow</code>); I spell it out so the intent is obvious to anyone reading the headers.</p>\n<p>Google’s duplicate-URL guide does say it doesn’t recommend <code>noindex</code> for choosing a canonical, “because it will completely block the page from Search.” For the copy, blocking it completely is what I want. The canonical header still names the page that should rank.</p>\n<p><strong><code>Vary: Accept</code></strong> matters wherever one URL answers with HTML or Markdown depending on the request. Without it, a cache in the middle can store the Markdown and hand it to the next browser (or the reverse). The <code>.md</code> URL itself never negotiates, but sending <code>Vary: Accept</code> there is harmless, and on this blog it’s on every response.</p>\n<p>One more thing: don’t disallow <code>*.md</code> in <code>robots.txt</code> to keep the copies out. Google only sees <code>X-Robots-Tag</code> and the canonical when it can fetch the URL, and <a href=\"https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag\">its docs</a> say blocked URLs have their rules “ignored.”</p>\n<h2 id=\"in-nginx\" data-level=\"3\">In nginx</h2>\n<p>The blog is static files behind nginx. A <code>map</code> turns the <code>Accept</code> header into a suffix, the post location rewrites to the <code>.md</code> file when it’s set, and the <code>.md</code> location adds the two headers:</p>\n<figure class=\"code\" data-lang=\"nginx\"><figcaption><span class=\"file\">deploy/nginx.conf</span><span class=\"lang\">nginx</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">location</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> ~</span><span style=\"--shiki-light:#032F62;--shiki-dark:#DBEDFF\"> ^/posts/[^/.]+$ </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> ($deallocated_md) { </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">rewrite</span><span style=\"--shiki-light:#032F62;--shiki-dark:#DBEDFF\"> ^ $</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">uri.md </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">last</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">; }</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    try_files </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">$uri.html </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">=404</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">location</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> ~</span><span style=\"--shiki-light:#032F62;--shiki-dark:#DBEDFF\"> ^(?&#x3C;md_page>/.*?)(?:index)?\\.md$ </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    types</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> { }</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    default_type </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">text/markdown;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    include </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">snippets/deallocatedobjects-headers.conf;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    add_header </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">Link </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"&#x3C;https://deallocatedobjects.com$</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">md_page</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">>; rel=</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">\\\"</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">canonical</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">\\\"</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> always;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    add_header </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">X-Robots-Tag </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"noindex, follow\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> always;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    try_files </span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">$uri </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">=404</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre></figure><p>The <code>include</code> line is the nginx gotcha. The shared snippet holds the security headers, <code>Vary: Accept</code> and the sitewide <code>Link</code> to <code>llms.txt</code>, and it’s included at the server level. But per the <a href=\"https://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header\">nginx docs</a>, <code>add_header</code> directives are inherited “if and only if there are no <code>add_header</code> directives defined on the current level.” The moment this location adds its own canonical, every server-level header disappears from <code>.md</code> responses, so the location includes the snippet again.</p>\n<p>That’s why the curl output above has two <code>link:</code> lines. nginx sends each <code>add_header</code> as its own line, and that’s fine: <a href=\"https://www.rfc-editor.org/rfc/rfc8288#section-3\">RFC 8288</a> defines <code>Link</code> as a comma-separated list, so two lines mean the same thing as one line with both values.</p>\n<h2 id=\"in-next-js\" data-level=\"3\">In Next.js</h2>\n<p>In a Next.js app, a proxy (Next 16’s name for middleware) rewrites <code>/guides/foo.md</code>, or <code>/guides/foo</code> with <code>Accept: text/markdown</code> and no <code>text/html</code>, to a route handler that returns the copy. This is from <a href=\"https://rookiefeed.com/?ref=deallocated-post\">Rookiefeed</a>:</p>\n<figure class=\"code\" data-lang=\"typescript\"><figcaption><span class=\"file\">src/lib/markdown.ts</span><span class=\"lang\">TypeScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">export</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> function</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> markdownResponse</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">body</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> string</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">canonicalPath</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> string</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">) {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">  return</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> new</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Response</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(body, {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    headers: {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">      \"Content-Type\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"text/markdown; charset=utf-8\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">      Vary: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"Accept\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">      Link: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">`&#x3C;${</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">siteUrl</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">(</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">canonicalPath</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">)</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">}>; rel=\"canonical\"`</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">      \"X-Robots-Tag\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"noindex, follow\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">      \"Cache-Control\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"public, max-age=600\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    },</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre></figure><p>The same app also sent a sitewide <code>Link</code> header from <code>next.config.ts</code>, pointing agents at <code>llms.txt</code> and the sitemap:</p>\n<figure class=\"code\" data-lang=\"typescript\"><figcaption><span class=\"file\">next.config.ts</span><span class=\"lang\">TypeScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">async </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">headers</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">() {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">  const</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> discovery</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> '&#x3C;/llms.txt>; rel=\"service-doc\"; type=\"text/plain\", &#x3C;/sitemap.xml>; rel=\"sitemap\"; type=\"application/xml\"'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">  return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    { source: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"/:path*\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, headers: [{ key: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"Link\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, value: discovery }, </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">...</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">SECURITY_HEADERS</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] },</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  ];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">},</span></span></code></pre></figure><p>With both in place, the <code>.md</code> responses came back with the discovery <code>Link</code> and no canonical. The route’s <code>Link</code> never reached the client.</p>\n<h2 id=\"how-next-combines-them\" data-level=\"3\">How Next Combines Them</h2>\n<p>The <a href=\"https://nextjs.org/docs/app/api-reference/config/next-config-js/headers\"><code>headers()</code> docs</a> only cover two config rules setting the same key: “the last header key will override the first.” They don’t say what happens when a config rule and a route handler set the same header, so I read the code.</p>\n<p>Config headers are applied first. The router collects every matching rule into one object (<code>resHeaders[key] = value</code>, so a later rule replaces an earlier one) and calls <code>res.setHeader</code> for each before your route runs. Then <a href=\"https://github.com/vercel/next.js/blob/canary/packages/next/src/server/send-response.ts\"><code>send-response.ts</code></a> copies your route’s <code>Response</code> headers onto that same response, like this (16.4.5, trimmed):</p>\n<figure class=\"code\" data-lang=\"javascript\"><figcaption><span class=\"file\">next/dist/server/send-response.js</span><span class=\"lang\">JavaScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">const</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> headersWithMultipleValuesAllowed</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">    'set-cookie'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">    'www-authenticate'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">    'proxy-authenticate'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">    'vary'</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">response.headers.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">forEach</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">((</span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">value</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">name</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">)</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=></span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6A737D;--shiki-dark:#6A737D\">    // only append the header if it is either not present in the outbound response</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6A737D;--shiki-dark:#6A737D\">    // or if the header supports multiple values</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    const</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> isHeaderPresent</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> typeof</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> res.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">getHeader</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(name) </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">!==</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> 'undefined'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> (headersWithMultipleValuesAllowed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">includes</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(name.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">toLowerCase</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">()) </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">||</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> !</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">isHeaderPresent) {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">        res.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">appendHeader</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(name, value);</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    }</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">});</span></span></code></pre></figure><p>So for any header your config already set, the route’s value is skipped without a warning. Four names are exceptions and get appended. <code>Link</code> isn’t one of them, so the config’s <code>Link</code> stays and the route’s canonical is dropped. <code>Vary</code> is one of them, which is why the route’s <code>Vary: Accept</code> makes it through:</p>\n<figure class=\"code\" data-lang=\"shellsession\"><figcaption><span class=\"lang\">Terminal</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">$ curl -sI https://rookiefeed.com/guides/how-much-does-mc-authority-cost.md</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">HTTP/2 200</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">content-type: text/markdown; charset=utf-8</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">vary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch, Accept, Accept-Encoding</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">link: &#x3C;https://rookiefeed.com/guides/how-much-does-mc-authority-cost>; rel=\"canonical\"</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">x-robots-tag: noindex, follow</span></span></code></pre></figure><p>That’s the output after the fix, which is to keep the config rule off the <code>.md</code> paths with a negative lookahead in <code>source</code>:</p>\n<figure class=\"code\" data-lang=\"typescript\"><figcaption><span class=\"file\">next.config.ts</span><span class=\"lang\">TypeScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{ </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">source</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"/:path*\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">headers</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">SECURITY_HEADERS</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> },</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6A737D;--shiki-dark:#6A737D\">// Not on .md twins: a config Link header replaces the route's own rel=\"canonical\".</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{ </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">source</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"/:path((?!.*</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">\\\\</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">.md$).*)\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">headers</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">: [{ key: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">\"Link\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, value: discovery }] },</span></span></code></pre></figure><p>If you still want the discovery links on the copies, have the route send them itself, in the same header as the canonical. <a href=\"https://runheed.com/?ref=deallocated-post\">Runheed</a> does that:</p>\n<figure class=\"code\" data-lang=\"shellsession\"><figcaption><span class=\"lang\">Terminal</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">$ curl -sI https://runheed.com/errors/econnreset.md</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">link: &#x3C;https://runheed.com/errors/econnreset>; rel=\"canonical\", &#x3C;/llms.txt>; rel=\"llms-txt\", &#x3C;/openapi.json>; rel=\"describedby\"; type=\"application/json\", &#x3C;/SKILL.md>; rel=\"describedby\"; type=\"text/markdown\"</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">x-robots-tag: noindex, follow</span></span></code></pre></figure><p>Rookiefeed’s config test now checks that the discovery rule matches <code>/guides/how-much-does-mc-authority-cost</code> and doesn’t match the <code>.md</code> version, so the exclusion can’t quietly go away.</p>\n<h2 id=\"vary-on-the-html-side\" data-level=\"3\">Vary On The HTML Side</h2>\n<p><code>Vary</code> has its own trap, on the HTML page rather than the copy. When <code>/errors/econnreset</code> negotiates, the HTML response needs <code>Vary: Accept</code> too, and setting it in the proxy isn’t enough. Next’s app-page handler runs after the proxy and calls <code>res.setHeader('Vary', 'rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch')</code>, and <code>setHeader</code> replaces whatever was there.</p>\n<p>How much that matters depends on your cache headers. Rookiefeed’s pages are sent <code>private, no-store</code>, so nothing between the app and the browser keeps a copy. Runheed’s are <code>s-maxage=31536000</code>, which invites a shared cache to store them, so Runheed patches <code>ServerResponse.prototype.setHeader</code> and <code>appendHeader</code> from <code>instrumentation.ts</code> to add <code>Accept</code> to <code>Vary</code> on the negotiated paths, each token once:</p>\n<figure class=\"code\" data-lang=\"shellsession\"><figcaption><span class=\"lang\">Terminal</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">$ curl -sI https://runheed.com/errors/econnreset</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">content-type: text/html; charset=utf-8</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">link: &#x3C;https://runheed.com/errors/econnreset.md>; rel=\"alternate\"; type=\"text/markdown\", &#x3C;/llms.txt>; rel=\"llms-txt\", &#x3C;/openapi.json>; rel=\"describedby\"; type=\"application/json\", &#x3C;/SKILL.md>; rel=\"describedby\"; type=\"text/markdown\"</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">vary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch, Accept, Accept-Encoding</span></span></code></pre></figure><h2 id=\"checking-your-own\" data-level=\"3\">Checking Your Own</h2>\n<p>Whatever the stack, the check is three requests:</p>\n<figure class=\"code\" data-lang=\"bash\"><figcaption><span class=\"lang\">Shell</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">curl</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> -sI</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> https://example.com/page.md</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">curl</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> -sI</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> -H</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> \"Accept: text/markdown\"</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> https://example.com/page</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">curl</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> -sI</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> https://example.com/page</span></span></code></pre></figure><p>The first two should carry the absolute canonical <code>Link</code>, <code>X-Robots-Tag: noindex, follow</code> and <code>Accept</code> in <code>Vary</code>. The third should be HTML, with <code>Accept</code> in <code>Vary</code> if the URL negotiates and your responses can be cached. Run them against the deployed site, since that’s the response where config, proxy and route headers all meet.</p>","created_at":"2026-11-28T14:00:00.000Z","updated_at":"2026-11-28T14:00:00.000Z"}