Files
pbakaus_impeccable/api/commands.js
T
Paul BakausandClaude Opus 4.6 98d16686dc Add edge cache headers to all API routes to reduce Vercel function invocations
All API routes serve static content that only changes at deploy time, but had
0% cache hit rate. Adding s-maxage=86400 lets Vercel's CDN cache responses at
the edge, which should take the ~28K daily API requests from 0% to ~99% cache.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 17:10:04 -07:00

50 lines
1.6 KiB
JavaScript

import { readdirSync, readFileSync, statSync, existsSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PROJECT_ROOT = join(__dirname, "..");
export default function handler(req, res) {
try {
const skillsDir = join(PROJECT_ROOT, "source", "skills");
const entries = readdirSync(skillsDir);
const commands = [];
for (const entry of entries) {
const entryPath = join(skillsDir, entry);
if (!statSync(entryPath).isDirectory()) continue;
const skillMd = join(entryPath, "SKILL.md");
if (!existsSync(skillMd)) continue;
const content = readFileSync(skillMd, "utf-8");
const frontmatterMatch = content.match(/^---\n([\s\S]+?)\n---/);
if (frontmatterMatch) {
const frontmatter = frontmatterMatch[1];
const userInvokable = /user-invokable:\s*true/.test(frontmatter);
if (!userInvokable) continue;
const nameMatch = frontmatter.match(/name:\s*(.+)/);
const descMatch = frontmatter.match(/description:\s*(.+)/);
commands.push({
id: entry,
name: nameMatch?.[1]?.trim() || entry,
description: descMatch?.[1]?.trim() || "No description available",
});
}
}
res.setHeader("Cache-Control", "public, s-maxage=86400, stale-while-revalidate=3600");
res.status(200).json(commands);
} catch (error) {
console.error("Error in /api/commands:", error);
res.status(500).json({ error: "Internal server error" });
}
}