mirror of
https://github.com/svemagie/blog-eleventy-indiekit.git
synced 2026-04-02 08:44:56 +02:00
Introduce shared cachedFetch helper (lib/data-fetch.js) wrapping EleventyFetch with two protections: - 10-second hard timeout via AbortController on every network request, preventing slow or unresponsive APIs from hanging the build - 4-hour cache TTL in watch/serve mode (vs 5-15 min originals), so incremental rebuilds serve from disk cache instead of re-fetching APIs every time a markdown file changes All 13 network _data files updated to use cachedFetch. Production builds keep original short TTLs for fresh data. Targets the "Data File" benchmark (12,169ms / 32% of incremental rebuild) — the largest remaining bottleneck after filter memoization. Confab-Link: http://localhost:8080/sessions/0b241cd6-aff2-4fec-853c-2b5a61e61946
100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
/**
|
|
* News/RSS Activity Data
|
|
* Fetches from Indiekit's endpoint-rss public API
|
|
*/
|
|
|
|
import { cachedFetch } from "../lib/data-fetch.js";
|
|
|
|
const INDIEKIT_URL = process.env.SITE_URL || "https://example.com";
|
|
|
|
/**
|
|
* Fetch from Indiekit's public RSS API endpoint
|
|
*/
|
|
async function fetchFromIndiekit(endpoint) {
|
|
try {
|
|
const url = `${INDIEKIT_URL}/rssapi/api/${endpoint}`;
|
|
console.log(`[newsActivity] Fetching from Indiekit: ${url}`);
|
|
const data = await cachedFetch(url, {
|
|
duration: "15m",
|
|
type: "json",
|
|
});
|
|
console.log(`[newsActivity] Indiekit ${endpoint} success`);
|
|
return data;
|
|
} catch (error) {
|
|
console.log(
|
|
`[newsActivity] Indiekit API unavailable for ${endpoint}: ${error.message}`
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default async function () {
|
|
try {
|
|
console.log("[newsActivity] Fetching RSS feed data...");
|
|
|
|
// Fetch all data from Indiekit API
|
|
const [itemsRes, feedsRes, statusRes] = await Promise.all([
|
|
fetchFromIndiekit("items?limit=50"),
|
|
fetchFromIndiekit("feeds"),
|
|
fetchFromIndiekit("status"),
|
|
]);
|
|
|
|
// Check if we got data
|
|
const hasData = itemsRes?.items?.length || feedsRes?.feeds?.length;
|
|
|
|
if (!hasData) {
|
|
console.log("[newsActivity] No data available from Indiekit");
|
|
return {
|
|
items: [],
|
|
feeds: [],
|
|
status: null,
|
|
lastUpdated: null,
|
|
source: "unavailable",
|
|
};
|
|
}
|
|
|
|
console.log(
|
|
`[newsActivity] Got ${itemsRes?.items?.length || 0} items from ${feedsRes?.feeds?.length || 0} feeds`
|
|
);
|
|
|
|
// Create a map of feed IDs to feed info for quick lookup
|
|
const feedMap = new Map();
|
|
for (const feed of feedsRes?.feeds || []) {
|
|
feedMap.set(feed.id, feed);
|
|
}
|
|
|
|
// Enhance items with additional feed info
|
|
const items = (itemsRes?.items || []).map((item) => {
|
|
const feed = feedMap.get(item.feedId);
|
|
return {
|
|
...item,
|
|
feedInfo: feed
|
|
? {
|
|
title: feed.title,
|
|
siteUrl: feed.siteUrl,
|
|
imageUrl: feed.imageUrl,
|
|
}
|
|
: null,
|
|
};
|
|
});
|
|
|
|
return {
|
|
items,
|
|
feeds: feedsRes?.feeds || [],
|
|
pagination: itemsRes?.pagination || null,
|
|
status: statusRes || null,
|
|
lastUpdated: statusRes?.lastSync || new Date().toISOString(),
|
|
source: "indiekit",
|
|
};
|
|
} catch (error) {
|
|
console.error("[newsActivity] Error:", error.message);
|
|
return {
|
|
items: [],
|
|
feeds: [],
|
|
status: null,
|
|
lastUpdated: null,
|
|
source: "error",
|
|
};
|
|
}
|
|
}
|