Use reference-backed hyperzine manifest

This commit is contained in:
fn
2026-06-08 18:43:12 +01:00
parent ac3b7d7ce9
commit d02bca8646
6 changed files with 236 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { ReferenceClient } from "@permaweb/references";
const ROOT = process.cwd();
const DIST_DIR = path.join(ROOT, "dist");
@@ -10,6 +11,8 @@ const DEFAULTS = {
BLOG_NAME: "hyperzine",
BLOG_SITE_URL: "https://hyperzine.xyz",
BLOG_DEFAULT_DESCRIPTION: "hyperzine",
ARWEAVE_GATEWAY: "https://arweave.net",
MANIFEST_REFERENCE_NAME: "",
MANIFEST_TX_ID: ""
};
@@ -50,6 +53,8 @@ const parseConfig = async () => {
BLOG_NAME: pick("BLOG_NAME"),
BLOG_SITE_URL: pick("BLOG_SITE_URL"),
BLOG_DEFAULT_DESCRIPTION: pick("BLOG_DEFAULT_DESCRIPTION"),
ARWEAVE_GATEWAY: pick("ARWEAVE_GATEWAY"),
MANIFEST_REFERENCE_NAME: pick("MANIFEST_REFERENCE_NAME"),
MANIFEST_TX_ID: pick("MANIFEST_TX_ID")
};
};
@@ -116,13 +121,81 @@ 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}`;
const response = await fetch(url, { cache: "no-store" });
if (!response.ok) throw new Error(`Manifest fetch failed (${response.status})`);
const payload = await response.json();
return parseManifest(payload);
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;
};
const resolveManifestTxId = async (config) => {
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) return value;
throw new Error(`Reference ${config.MANIFEST_REFERENCE_NAME} did not resolve to a manifest id`);
} catch (error) {
console.warn(
`[prerender-meta] Falling back to configured manifest; reference ${config.MANIFEST_REFERENCE_NAME} failed: ${error.message}`
);
}
}
return config.MANIFEST_TX_ID;
};
const buildMetaTags = ({
@@ -197,7 +270,7 @@ const main = async () => {
let posts = [];
try {
posts = await fetchManifestPosts(config.MANIFEST_TX_ID);
posts = await fetchBestManifestPosts(config);
} catch (error) {
console.warn(
`[prerender-meta] Could not fetch manifest; generating root metadata only: ${error.message}`