mirror of
https://github.com/pbakaus/impeccable.git
synced 2026-09-12 14:16:28 +03:00
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>
33 lines
1018 B
JavaScript
33 lines
1018 B
JavaScript
import { readFileSync, 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, "../..");
|
|
|
|
const VALID_ID = /^[a-zA-Z0-9_-]+$/;
|
|
|
|
export default function handler(req, res) {
|
|
try {
|
|
const { id } = req.query;
|
|
|
|
if (!id || !VALID_ID.test(id)) {
|
|
return res.status(400).json({ error: "Invalid command ID" });
|
|
}
|
|
|
|
const commandPath = join(PROJECT_ROOT, "source", "skills", id, "SKILL.md");
|
|
|
|
if (!existsSync(commandPath)) {
|
|
return res.status(404).json({ error: "Command not found" });
|
|
}
|
|
|
|
const content = readFileSync(commandPath, "utf-8");
|
|
res.setHeader("Cache-Control", "public, s-maxage=86400, stale-while-revalidate=3600");
|
|
res.status(200).json({ content });
|
|
} catch (error) {
|
|
console.error("Error in /api/command-source:", error);
|
|
res.status(500).json({ error: "Internal server error" });
|
|
}
|
|
}
|