Skip to content

Commit eaf8712

Browse files
authored
Merge pull request #22 from grassrootseconomics/williamluke4/fix-api-data-timeout
fix: resolve Vercel runtime timeout on /api/data
2 parents 600758f + ffa8878 commit eaf8712

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

pages/api/cron.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Cron endpoint to pre-warm caches
3+
* Scheduled via vercel.json to run every 6 hours
4+
*/
5+
6+
import { cacheWithExpiry } from "@utils/cache";
7+
import { fetchGraphData, fetchGlobeData } from "@lib/api/data-service";
8+
import {
9+
CACHE_KEY,
10+
CACHE_TTL_SECONDS,
11+
GLOBE_CACHE_KEY,
12+
GLOBE_CACHE_TTL_SECONDS,
13+
} from "@config/cache";
14+
import type { NextApiRequest, NextApiResponse } from "next";
15+
16+
export default async function handler(
17+
req: NextApiRequest,
18+
res: NextApiResponse
19+
) {
20+
const authHeader = req.headers.authorization;
21+
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
22+
return res.status(401).json({ error: "Unauthorized" });
23+
}
24+
25+
try {
26+
await Promise.all([
27+
cacheWithExpiry(CACHE_KEY, CACHE_TTL_SECONDS, fetchGraphData, true),
28+
cacheWithExpiry(
29+
GLOBE_CACHE_KEY,
30+
GLOBE_CACHE_TTL_SECONDS,
31+
fetchGlobeData,
32+
true
33+
),
34+
]);
35+
36+
return res.status(200).json({ ok: true });
37+
} catch (error) {
38+
console.error("Cron cache warm failed:", error);
39+
return res.status(500).json({
40+
error: "Cache warm failed",
41+
details: error instanceof Error ? error.message : String(error),
42+
});
43+
}
44+
}
45+
46+
export const config = {
47+
maxDuration: 120,
48+
};

pages/api/data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default async function handler(
3636
}
3737

3838
export const config = {
39+
maxDuration: 60,
3940
api: {
4041
responseLimit: false,
4142
},

pages/api/geo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default async function handler(
3737
}
3838

3939
export const config = {
40+
maxDuration: 60,
4041
api: {
4142
responseLimit: false,
4243
},

0 commit comments

Comments
 (0)