diff options
Diffstat (limited to 'feed')
| -rw-r--r-- | feed/note-page.js | 19 | ||||
| -rw-r--r-- | feed/profile-page.js | 10 | ||||
| -rw-r--r-- | feed/server.js | 3 |
3 files changed, 23 insertions, 9 deletions
diff --git a/feed/note-page.js b/feed/note-page.js index 3145f14..36f54fd 100644 --- a/feed/note-page.js +++ b/feed/note-page.js @@ -19,6 +19,11 @@ export const NOTE_RELAYS = ['wss://relay.damus.io', 'wss://nos.lol', 'wss://rela // where kind 0s live when the conversation relays lack them const PROFILE_RELAYS = ['wss://purplepag.es', 'wss://user.kindpag.es']; const BUDGET_MS = 500; +// A link-preview crawler reads the page once and keeps that card (Telegram, +// 2026-12-04: a fresh post shared at once got the site's card because its +// thread took longer than BUDGET_MS); it can wait for the real thing. +export const BOT_BUDGET_MS = 7500; // a reply's thread: 2.5 s + 2.5 s + 2 s of relay asks +export const isPreviewBot = (ua) => /bot\b|bot\/|crawler|spider|facebookexternalhit|facebookcatalog|whatsapp|telegram|slack|discord|twitter|linkedin|skype|embedly|iframely|mastodon|pleroma|misskey|akkoma|signal|vkshare|redditbot|applebot|bingpreview|google|yandex|duckduck|pinterest|tumblr|snapchat|preview/i.test(String(ua || '')); const FRESH_MS = 60_000; // a thread younger than this is served as is const KEEP_MS = 6 * 3600_000; // older, it's still served while a refresh runs const MAX_THREADS = 500; @@ -126,6 +131,12 @@ function nameOf(data, pk) { try { const m = JSON.parse(p.content); return { name: m.display_name || m.name || null, picture: m.picture || null }; } catch { return {}; } } +// index.html carries the site's own share tags between these markers; a +// rendered page drops them for its own (crawlers take the first of a pair). +// The site's picture stays as the stand-in for a page that has none. +export const siteTagsOut = (html) => html.replace(/<!--site-tags-->[\s\S]*?<!--\/site-tags-->\n?/, ''); +export const siteImage = (origin) => (origin ? origin + '/og-image.png' : ''); + // The app's index.html with the thread and its preview tags in the head. export function renderNotePage(html, data, ref, origin) { const focus = [data.root, ...data.replies].find((e) => e.id === ref.id) || data.root; @@ -141,13 +152,13 @@ export function renderNotePage(html, data, ref, origin) { desc ? `<meta property="og:description" content="${esc(desc)}">` : '', desc ? `<meta name="description" content="${esc(desc)}">` : '', origin ? `<meta property="og:url" content="${esc(origin + '/' + (ref.path || ''))}">` : '', - image ? `<meta property="og:image" content="${esc(image)}">` : '', - `<meta name="twitter:card" content="${image && image !== who.picture ? 'summary_large_image' : 'summary'}">`, + image || siteImage(origin) ? `<meta property="og:image" content="${esc(image || siteImage(origin))}">` : '', + `<meta name="twitter:card" content="${!image || image !== who.picture ? 'summary_large_image' : 'summary'}">`, // the author's face starts downloading with the page, not after the app boots who.picture && /^https:\/\//.test(who.picture) ? `<link rel="preload" as="image" href="${esc(who.picture)}">` : '', `<script type="application/json" id="boot-thread">${scriptJson(data)}</script>`, ].filter(Boolean).join('\n'); - let out = html.replace(/<title>[^<]*<\/title>/i, `<title>${esc(title)}</title>`); + let out = siteTagsOut(html).replace(/<title>[^<]*<\/title>/i, `<title>${esc(title)}</title>`); out = out.includes('</head>') ? out.replace('</head>', head + '\n</head>') : head + out; return out; } @@ -173,7 +184,7 @@ export async function notePageResponse(req, { index, threads }) { const headers = { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-cache, must-revalidate' }; if (!ref) return new Response(html, { headers }); ref.path = u.pathname.slice(1); - const data = await threads.get(ref).catch(() => null); + const data = await threads.get(ref, isPreviewBot(req.headers.get('user-agent')) ? BOT_BUDGET_MS : undefined).catch(() => null); const proto = req.headers.get('x-forwarded-proto') || 'https'; const host = req.headers.get('x-forwarded-host') || req.headers.get('host'); return new Response(data ? renderNotePage(html, data, ref, host ? proto + '://' + host : '') : html, { headers }); diff --git a/feed/profile-page.js b/feed/profile-page.js index a30c2ab..22a8370 100644 --- a/feed/profile-page.js +++ b/feed/profile-page.js @@ -11,7 +11,7 @@ // page goes out (and the build carries on into the cache). import { decode } from 'nostr-tools/nip19'; import { verifyEvent } from 'nostr-tools/pure'; -import { NOTE_RELAYS, esc, scriptJson } from './note-page.js'; +import { NOTE_RELAYS, esc, scriptJson, siteTagsOut, siteImage, isPreviewBot, BOT_BUDGET_MS } from './note-page.js'; export const PROFILE_PATH = /^\/((?:npub1|nprofile1)[a-z0-9]+|[A-Za-z0-9._-]{1,30})\/?$/; // single-segment app routes and files that are never a person @@ -186,14 +186,14 @@ export function renderProfilePage(html, data, ref, origin) { desc ? `<meta property="og:description" content="${esc(desc)}">` : '', desc ? `<meta name="description" content="${esc(desc)}">` : '', origin ? `<meta property="og:url" content="${esc(origin + '/' + ref.path)}">` : '', - picture ? `<meta property="og:image" content="${esc(picture)}">` : '', - `<meta name="twitter:card" content="summary">`, + picture || siteImage(origin) ? `<meta property="og:image" content="${esc(picture || siteImage(origin))}">` : '', + `<meta name="twitter:card" content="${picture ? 'summary' : 'summary_large_image'}">`, // the face and the cover start downloading with the page picture ? `<link rel="preload" as="image" href="${esc(picture)}">` : '', banner ? `<link rel="preload" as="image" href="${esc(banner)}">` : '', `<script type="application/json" id="boot-profile">${scriptJson({ ...data, path: ref.path })}</script>`, ].filter(Boolean).join('\n'); - let out = html.replace(/<title>[^<]*<\/title>/i, `<title>${esc(title)}</title>`); + let out = siteTagsOut(html).replace(/<title>[^<]*<\/title>/i, `<title>${esc(title)}</title>`); out = out.includes('</head>') ? out.replace('</head>', head + '\n</head>') : head + out; return out; } @@ -207,7 +207,7 @@ export async function profilePageResponse(req, { index, profiles, resolveName }) if (!ref.pk) ref.pk = await resolveName(ref.name); if (!ref.pk) return new Response(html, { headers }); ref.id = ref.pk; // the cache's key - const data = await profiles.get(ref, PROFILE_BUDGET_MS).catch(() => null); + const data = await profiles.get(ref, isPreviewBot(req.headers.get('user-agent')) ? BOT_BUDGET_MS : PROFILE_BUDGET_MS).catch(() => null); const proto = req.headers.get('x-forwarded-proto') || 'https'; const host = req.headers.get('x-forwarded-host') || req.headers.get('host'); return new Response(data ? renderProfilePage(html, data, ref, host ? proto + '://' + host : '') : html, { headers }); diff --git a/feed/server.js b/feed/server.js index 9566447..cc97d8b 100644 --- a/feed/server.js +++ b/feed/server.js @@ -25,6 +25,9 @@ const query = (relays, filter, maxWait, { idle = 0 } = {}) => new Promise((resol onclose: finish, }); if (done) try { sub.close(); } catch {} + // maxWait starts only once a socket is open: a relay that's slow to connect + // (every socket is cold after a restart) held builds past 6 s. A hard stop. + setTimeout(finish, maxWait); }); const threads = createThreadCache({ build: (ref) => buildThread(ref, query) }); const primal = createPrimal(); |
