diff --git a/api/article-og-refresh.ts b/api/article-og-refresh.ts index efce6fea..c967dded 100644 --- a/api/article-og-refresh.ts +++ b/api/article-og-refresh.ts @@ -8,6 +8,7 @@ export default async function handler(req: VercelRequest, res: VercelResponse) { const expectedSecret = process.env.OG_REFRESH_SECRET || '' if (providedSecret !== expectedSecret) { + console.error('Background refresh unauthorized: secret mismatch') return res.status(401).json({ error: 'Unauthorized' }) } @@ -16,19 +17,24 @@ export default async function handler(req: VercelRequest, res: VercelResponse) { return res.status(400).json({ error: 'Missing naddr parameter' }) } + console.log(`Background refresh started for ${naddr}`) + try { - // Fetch metadata via relays (WebSockets) + // Fetch metadata via relays (WebSockets) - no timeout, let it take as long as needed const meta = await fetchArticleMetadataViaRelays(naddr) if (meta) { + console.log(`Background refresh found metadata for ${naddr}:`, { title: meta.title, summary: meta.summary?.substring(0, 50) }) // Store in Redis await setArticleMeta(naddr, meta) + console.log(`Background refresh cached metadata for ${naddr}`) return res.status(200).json({ ok: true, cached: true }) } else { + console.log(`Background refresh found no metadata for ${naddr}`) return res.status(200).json({ ok: true, cached: false }) } } catch (err) { - console.error('Error refreshing article metadata:', err) + console.error(`Error refreshing article metadata for ${naddr}:`, err) return res.status(500).json({ error: 'Internal server error' }) } } diff --git a/api/article-og.ts b/api/article-og.ts index 3aa776ee..5bc100e1 100644 --- a/api/article-og.ts +++ b/api/article-og.ts @@ -67,13 +67,21 @@ export default async function handler(req: VercelRequest, res: VercelResponse) { ? `${req.headers['x-forwarded-proto']}://${req.headers['x-forwarded-host']}` : `https://read.withboris.com` - fetch(`${origin}/api/article-og-refresh?naddr=${encodeURIComponent(naddr)}`, { + const refreshUrl = `${origin}/api/article-og-refresh?naddr=${encodeURIComponent(naddr)}` + console.log(`Triggering background refresh for ${naddr}`) + + fetch(refreshUrl, { method: 'POST', headers: { 'x-refresh-key': secret }, keepalive: true - }).catch(() => { - // Ignore errors in background refresh trigger }) + .then(async (resp) => { + const result = await resp.json().catch(() => ({})) + console.log(`Background refresh response for ${naddr}:`, { status: resp.status, result }) + }) + .catch((err) => { + console.error(`Background refresh failed for ${naddr}:`, err) + }) } // Generate and send HTML