notecrumbs

a nostr opengraph server build on nostrdb and egui
git clone git://jb55.com/notecrumbs
Log | Files | Refs | README | LICENSE

commit 0c3f192bbdfb951988f2bb7dbc3758bfa9178422
parent a4f6400693f8d645d8a9a71dc983eaf4bf4a5ef8
Author: alltheseas <64376233+alltheseas@users.noreply.github.com>
Date:   Thu, 18 Dec 2025 12:40:32 -0600

perf: skip blocking profile feed fetch when notes are cached

Check local nostrdb for existing notes before awaiting fetch_profile_feed().
If cached notes exist, render immediately using local data and spawn a
background task to refresh the feed. First-time profile loads still block
on relay fetch to ensure we have data to display.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Diffstat:
Msrc/main.rs | 35++++++++++++++++++++++++++++++-----
1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -17,7 +17,7 @@ use crate::{ render::{ProfileRenderData, RenderData}, }; use nostr_sdk::prelude::*; -use nostrdb::{Config, Ndb, NoteKey, Transaction}; +use nostrdb::{Config, Filter, Ndb, NoteKey, Transaction}; use std::time::Duration; mod abbrev; @@ -185,10 +185,35 @@ async fn serve( }; if let Some(pubkey) = maybe_pubkey { - if let Err(err) = - render::fetch_profile_feed(app.relay_pool.clone(), app.ndb.clone(), pubkey).await - { - error!("Error fetching profile feed: {err}"); + // Check if we have cached notes for this profile + let has_cached_notes = { + let txn = Transaction::new(&app.ndb)?; + let notes_filter = Filter::new() + .authors([&pubkey]) + .kinds([1]) + .limit(1) + .build(); + app.ndb + .query(&txn, &[notes_filter], 1) + .map(|results| !results.is_empty()) + .unwrap_or(false) + }; + + let pool = app.relay_pool.clone(); + let ndb = app.ndb.clone(); + + if has_cached_notes { + // Cached data exists: spawn background refresh so we don't block response + tokio::spawn(async move { + if let Err(err) = render::fetch_profile_feed(pool, ndb, pubkey).await { + error!("Background profile feed refresh failed: {err}"); + } + }); + } else { + // No cached data: must wait for relay fetch before rendering + if let Err(err) = render::fetch_profile_feed(pool, ndb, pubkey).await { + error!("Error fetching profile feed: {err}"); + } } } }