Files
v10/site/netlify/edge-functions/markdown-negotiation.ts
T
2026-04-22 10:18:05 -05:00

30 lines
946 B
TypeScript

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',
},
};