fix: add edge functions for dynamic RSS, sitemap, and API proxying to Convex HTTP endpoints

This commit is contained in:
Wayne Sutton
2025-12-14 17:00:46 -08:00
parent c6c12ecd58
commit 078fbe6698
13 changed files with 316 additions and 107 deletions

View File

@@ -0,0 +1,61 @@
import type { Context } from "@netlify/edge-functions";
// Edge function to proxy API endpoints to Convex HTTP
export default async function handler(
request: Request,
_context: Context,
): Promise<Response> {
const convexUrl =
Deno.env.get("VITE_CONVEX_URL") || Deno.env.get("CONVEX_URL");
if (!convexUrl) {
return new Response(
JSON.stringify({ error: "Configuration error: Missing Convex URL" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
// Construct the Convex site URL for the HTTP endpoint
const convexSiteUrl = convexUrl.replace(".cloud", ".site");
const url = new URL(request.url);
const targetUrl = `${convexSiteUrl}${url.pathname}${url.search}`;
try {
const response = await fetch(targetUrl, {
headers: {
Accept: "application/json",
},
});
if (!response.ok) {
return new Response(JSON.stringify({ error: "API endpoint error" }), {
status: response.status,
headers: { "Content-Type": "application/json" },
});
}
const data = await response.text();
const contentType =
response.headers.get("Content-Type") || "application/json";
return new Response(data, {
headers: {
"Content-Type": contentType,
"Cache-Control": "public, max-age=300, s-maxage=600",
"Access-Control-Allow-Origin": "*",
},
});
} catch {
return new Response(JSON.stringify({ error: "Failed to fetch from API" }), {
status: 502,
headers: { "Content-Type": "application/json" },
});
}
}
export const config = {
path: ["/api/posts", "/api/post"],
};

View File

@@ -0,0 +1,53 @@
import type { Context } from "@netlify/edge-functions";
// Edge function to proxy RSS feed to Convex HTTP endpoint
export default async function handler(
request: Request,
_context: Context,
): Promise<Response> {
const convexUrl =
Deno.env.get("VITE_CONVEX_URL") || Deno.env.get("CONVEX_URL");
if (!convexUrl) {
return new Response("Configuration error: Missing Convex URL", {
status: 500,
});
}
// Construct the Convex site URL for the HTTP endpoint
const convexSiteUrl = convexUrl.replace(".cloud", ".site");
const url = new URL(request.url);
const targetUrl = `${convexSiteUrl}${url.pathname}`;
try {
const response = await fetch(targetUrl, {
headers: {
Accept: "application/rss+xml",
},
});
if (!response.ok) {
return new Response("RSS feed not available", {
status: response.status,
headers: { "Content-Type": "text/plain" },
});
}
const xml = await response.text();
return new Response(xml, {
headers: {
"Content-Type": "application/rss+xml; charset=utf-8",
"Cache-Control": "public, max-age=3600, s-maxage=7200",
},
});
} catch {
return new Response("Failed to fetch RSS feed", {
status: 502,
headers: { "Content-Type": "text/plain" },
});
}
}
export const config = {
path: ["/rss.xml", "/rss-full.xml"],
};

View File

@@ -0,0 +1,52 @@
import type { Context } from "@netlify/edge-functions";
// Edge function to proxy sitemap to Convex HTTP endpoint
export default async function handler(
request: Request,
_context: Context,
): Promise<Response> {
const convexUrl =
Deno.env.get("VITE_CONVEX_URL") || Deno.env.get("CONVEX_URL");
if (!convexUrl) {
return new Response("Configuration error: Missing Convex URL", {
status: 500,
});
}
// Construct the Convex site URL for the HTTP endpoint
const convexSiteUrl = convexUrl.replace(".cloud", ".site");
const targetUrl = `${convexSiteUrl}/sitemap.xml`;
try {
const response = await fetch(targetUrl, {
headers: {
Accept: "application/xml",
},
});
if (!response.ok) {
return new Response("Sitemap not available", {
status: response.status,
headers: { "Content-Type": "text/plain" },
});
}
const xml = await response.text();
return new Response(xml, {
headers: {
"Content-Type": "application/xml; charset=utf-8",
"Cache-Control": "public, max-age=3600, s-maxage=7200",
},
});
} catch {
return new Response("Failed to fetch sitemap", {
status: 502,
headers: { "Content-Type": "text/plain" },
});
}
}
export const config = {
path: "/sitemap.xml",
};