From 52d20bfca828231d420be9a516def54547d0c7af Mon Sep 17 00:00:00 2001 From: Darius Cepulis Date: Thu, 19 Feb 2026 08:39:56 -0600 Subject: [PATCH] feat(site): add markdown content negotiation via Netlify edge function (#573) Co-authored-by: Claude Opus 4.6 --- pnpm-lock.yaml | 5 +++- .../edge-functions/markdown-negotiation.ts | 29 +++++++++++++++++++ site/package.json | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 site/netlify/edge-functions/markdown-negotiation.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b47fe28c..ce79718c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -529,6 +529,9 @@ importers: '@astrojs/check': specifier: ^0.9.6 version: 0.9.6(prettier@3.8.1)(typescript@5.9.3) + '@netlify/edge-functions': + specifier: ^3.0.3 + version: 3.0.3 '@testing-library/jest-dom': specifier: ^6.9.1 version: 6.9.1 @@ -6323,7 +6326,7 @@ packages: tar@7.5.2: resolution: {integrity: sha512-7NyxrTE4Anh8km8iEy7o0QYPs+0JKBTj5ZaqHg6B39erLg0qYXN3BijtShwbsNSvQ+LN75+KV+C4QR/f6Gwnpg==} engines: {node: '>=18'} - deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me + deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exhorbitant rates) by contacting i@izs.me tar@7.5.7: resolution: {integrity: sha512-fov56fJiRuThVFXD6o6/Q354S7pnWMJIVlDBYijsTNx6jKSE4pvrDTs6lUnmGvNyfJwFQQwWy3owKz1ucIhveQ==} diff --git a/site/netlify/edge-functions/markdown-negotiation.ts b/site/netlify/edge-functions/markdown-negotiation.ts new file mode 100644 index 00000000..dededbe8 --- /dev/null +++ b/site/netlify/edge-functions/markdown-negotiation.ts @@ -0,0 +1,29 @@ +import type { Config, Context } from '@netlify/edge-functions'; + +export default async (request: Request, context: Context) => { + const url = new URL(request.url); + const path = url.pathname.replace(/\/$/, ''); + + // Fetch the pre-built .md file from static assets + const mdResponse = await fetch(new URL(`${path}.md`, request.url)); + if (!mdResponse.ok) return; + + const body = await mdResponse.text(); + + const headers = new Headers(); + headers.set('content-type', 'text/markdown; charset=utf-8'); + headers.set('cache-control', 'public, s-maxage=31536000'); + headers.set('vary', 'Accept'); + headers.set('x-markdown-tokens', String(Math.ceil(body.length / 4))); + + return new Response(body, { status: 200, headers }); +}; + +export const config: Config = { + // https://docs.netlify.com/build/edge-functions/optional-configuration/#caching + cache: 'manual', + path: ['/blog/*', '/docs/*'], + header: { + accept: 'text/markdown', + }, +}; diff --git a/site/package.json b/site/package.json index 2c269e81..1faa93d7 100644 --- a/site/package.json +++ b/site/package.json @@ -56,6 +56,7 @@ }, "devDependencies": { "@astrojs/check": "^0.9.6", + "@netlify/edge-functions": "^3.0.3", "@testing-library/jest-dom": "^6.9.1", "@testing-library/react": "^16.3.0", "@testing-library/user-event": "^14.6.1",