Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions netlify/edge-functions/serve-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
import type { Config } from "@netlify/edge-functions";
import type { Config, Context } from "@netlify/edge-functions";

export default async (request: Request) => {
export default async (request: Request, context: Context) => {
const acceptHeader = request.headers.get("Accept") || "";

if (!acceptHeader.includes("text/markdown")) {
return;
}

const url = new URL(request.url);
const { pathname } = url;

if (pathname.endsWith(".md")) {
return;
let nextRequest: Request = request;
if (acceptHeader.includes("text/markdown") && !pathname.endsWith(".md")) {
url.pathname =
pathname === "/"
? "/index.md"
: pathname.replace(/\/?$/, ".md").replace(/\.html\.md$/, ".md");
nextRequest = new Request(url, request);
}

if (pathname === "/") {
url.pathname = "/index.md";
} else {
url.pathname = pathname.replace(/\/?$/, ".md").replace(/\.html\.md$/, ".md");
const response = await context.next(nextRequest);

const apiKey = Netlify.env.get("PROFOUND_API_KEY");
const loggingEnabled = Netlify.env.get("LOG_TO_PROFOUND") === "true";

if (apiKey && loggingEnabled) {
const params: Record<string, string> = {};
url.searchParams.forEach((value, key) => {
params[key] = value;
});

context.waitUntil(
fetch("https://artemis.api.tryprofound.com/v1/logs/custom", {
method: "POST",
headers: {
'x-api-key': apiKey,
'Content-Type': 'application/json',
Comment on lines +32 to +33
},
body: JSON.stringify([{
timestamp: new Date().toISOString(),
method: request.method,
host: request.headers.get("host"),
path: pathname,
status_code: response.status,
ip: context.ip,
Comment thread
fabianrbz marked this conversation as resolved.
user_agent: request.headers.get("user-agent"),
query_params: params,
referer: request.headers.get("referer"),
}]),
Comment thread
fabianrbz marked this conversation as resolved.
})
Comment on lines +29 to +46
);
}

return url;
return response;
};

export const config: Config = {
path: "/*",
excludedPath: [
"/assets/*",
"/vite/assets/*",
"/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg",
"/**/*.gif", "/**/*.svg", "/**/*.ico", "/**/*.webp",
"/**/*.woff", "/**/*.woff2", "/**/*.ttf", "/**/*.eot",
"/**/*.json", "/**/*.xml", "/**/*.map",
"/**/*.json", "/**/*.xml", "/**/*.map"
],
onError: "bypass",
};
Loading