Prefer reference manifest over fallback

This commit is contained in:
fn
2026-06-08 18:59:34 +01:00
parent d02bca8646
commit 71a8a23054
3 changed files with 60 additions and 129 deletions

View File

@@ -121,20 +121,6 @@ const parseManifest = (input) => {
});
};
const timestampMs = (value) => {
if (typeof value !== "string" || !value) return 0;
const parsed = Date.parse(value);
return Number.isNaN(parsed) ? 0 : parsed;
};
const manifestFreshness = (payload, posts) =>
Math.max(
timestampMs(payload?.updated),
timestampMs(payload?.date),
timestampMs(payload?.generatedAt),
...posts.map((post) => Math.max(timestampMs(post.updated), timestampMs(post.publishedAt)))
);
const fetchManifestPosts = async (manifestTxId) => {
if (!manifestTxId) return [];
const url = `https://arweave.net/${manifestTxId}`;
@@ -142,43 +128,7 @@ const fetchManifestPosts = async (manifestTxId) => {
if (!response.ok) throw new Error(`Manifest fetch failed (${response.status})`);
const payload = await response.json();
const posts = parseManifest(payload);
return { txId: manifestTxId, posts, freshness: manifestFreshness(payload, posts) };
};
const resolveManifestTxIds = async (config) => {
const candidates = [];
if (config.MANIFEST_REFERENCE_NAME) {
try {
const names = new ReferenceClient({ gateway: config.ARWEAVE_GATEWAY || DEFAULTS.ARWEAVE_GATEWAY });
const value = await names.resolveName(config.MANIFEST_REFERENCE_NAME);
if (typeof value === "string" && value.length > 0) candidates.push(value);
else throw new Error(`Reference ${config.MANIFEST_REFERENCE_NAME} did not resolve to a manifest id`);
} catch (error) {
console.warn(
`[prerender-meta] Trying configured manifest; reference ${config.MANIFEST_REFERENCE_NAME} failed: ${error.message}`
);
}
}
if (config.MANIFEST_TX_ID) candidates.push(config.MANIFEST_TX_ID);
return [...new Set(candidates)];
};
const fetchBestManifestPosts = async (config) => {
const candidates = await resolveManifestTxIds(config);
const loaded = [];
for (const manifestTxId of candidates) {
try {
loaded.push(await fetchManifestPosts(manifestTxId));
} catch (error) {
console.warn(`[prerender-meta] Manifest ${manifestTxId} failed: ${error.message}`);
}
}
if (!loaded.length) return [];
loaded.sort((a, b) => b.freshness - a.freshness);
return loaded[0].posts;
return { txId: manifestTxId, posts };
};
const resolveManifestTxId = async (config) => {
@@ -198,6 +148,22 @@ const resolveManifestTxId = async (config) => {
return config.MANIFEST_TX_ID;
};
const fetchCurrentManifestPosts = async (config) => {
const manifestTxId = await resolveManifestTxId(config);
if (!manifestTxId) return [];
try {
return (await fetchManifestPosts(manifestTxId)).posts;
} catch (error) {
if (!config.MANIFEST_REFERENCE_NAME || manifestTxId === config.MANIFEST_TX_ID) throw error;
console.warn(
`[prerender-meta] Falling back to configured manifest; reference manifest ${manifestTxId} failed: ${error.message}`
);
}
return config.MANIFEST_TX_ID ? (await fetchManifestPosts(config.MANIFEST_TX_ID)).posts : [];
};
const buildMetaTags = ({
title,
description,
@@ -270,7 +236,7 @@ const main = async () => {
let posts = [];
try {
posts = await fetchBestManifestPosts(config);
posts = await fetchCurrentManifestPosts(config);
} catch (error) {
console.warn(
`[prerender-meta] Could not fetch manifest; generating root metadata only: ${error.message}`