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
97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
/**
|
|
* Mastodon Feed Data
|
|
* Fetches recent posts from Mastodon using the public API
|
|
*/
|
|
|
|
import { cachedFetch } from "../lib/data-fetch.js";
|
|
|
|
export default async function () {
|
|
const instance = process.env.MASTODON_INSTANCE?.replace("https://", "") || "";
|
|
const username = process.env.MASTODON_USER || "";
|
|
|
|
try {
|
|
// First, look up the account ID
|
|
const lookupUrl = `https://${instance}/api/v1/accounts/lookup?acct=${username}`;
|
|
|
|
const account = await cachedFetch(lookupUrl, {
|
|
duration: "1h", // Cache account lookup for 1 hour
|
|
type: "json",
|
|
fetchOptions: {
|
|
headers: {
|
|
Accept: "application/json",
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!account || !account.id) {
|
|
console.log("Mastodon account not found:", username);
|
|
return [];
|
|
}
|
|
|
|
// Fetch recent statuses (excluding replies and boosts for cleaner feed)
|
|
const statusesUrl = `https://${instance}/api/v1/accounts/${account.id}/statuses?limit=10&exclude_replies=true&exclude_reblogs=true`;
|
|
|
|
const statuses = await cachedFetch(statusesUrl, {
|
|
duration: "15m", // Cache for 15 minutes
|
|
type: "json",
|
|
fetchOptions: {
|
|
headers: {
|
|
Accept: "application/json",
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!statuses || !Array.isArray(statuses)) {
|
|
console.log("No Mastodon statuses found for:", username);
|
|
return [];
|
|
}
|
|
|
|
// Transform statuses into a simpler format
|
|
return statuses.map((status) => ({
|
|
id: status.id,
|
|
url: status.url,
|
|
text: stripHtml(status.content),
|
|
htmlContent: status.content,
|
|
createdAt: status.created_at,
|
|
author: {
|
|
username: status.account.username,
|
|
displayName: status.account.display_name || status.account.username,
|
|
avatar: status.account.avatar,
|
|
url: status.account.url,
|
|
},
|
|
favouritesCount: status.favourites_count || 0,
|
|
reblogsCount: status.reblogs_count || 0,
|
|
repliesCount: status.replies_count || 0,
|
|
// Media attachments
|
|
media: status.media_attachments
|
|
? status.media_attachments.map((m) => ({
|
|
type: m.type,
|
|
url: m.url,
|
|
previewUrl: m.preview_url,
|
|
description: m.description,
|
|
}))
|
|
: [],
|
|
}));
|
|
} catch (error) {
|
|
console.error("Error fetching Mastodon feed:", error.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
// Simple HTML stripper for plain text display
|
|
function stripHtml(html) {
|
|
if (!html) return "";
|
|
return html
|
|
.replace(/<br\s*\/?>/gi, " ")
|
|
.replace(/<\/p>/gi, " ")
|
|
.replace(/<[^>]+>/g, "")
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, "'")
|
|
.replace(/ /g, " ")
|
|
.replace(/\s+/g, " ")
|
|
.trim();
|
|
}
|