Files
pbakaus_impeccable/api/download/bundle/[provider].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

40 lines
1.3 KiB
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 ALLOWED_PROVIDERS = [
'universal', 'universal-prefixed',
];
export default function handler(req, res) {
try {
const { provider } = req.query;
if (!provider || !ALLOWED_PROVIDERS.includes(provider)) {
return res.status(400).json({ error: "Invalid provider" });
}
const distDir = join(PROJECT_ROOT, "dist");
const zipPath = join(distDir, `${provider}.zip`);
if (!existsSync(zipPath)) {
return res.status(404).json({ error: "Bundle not found" });
}
const content = readFileSync(zipPath);
res.setHeader("Cache-Control", "public, s-maxage=86400, stale-while-revalidate=3600");
res.setHeader("Content-Type", "application/zip");
const safeProvider = provider.replace(/[^a-zA-Z0-9._-]/g, '');
res.setHeader("Content-Disposition", `attachment; filename="impeccable-style-${safeProvider}.zip"`);
res.send(content);
} catch (error) {
console.error("Error downloading bundle:", error);
res.status(500).json({ error: "Internal server error" });
}
}