@ -60,6 +60,9 @@ export async function prewarmCaches(
} ;
} ;
try {
try {
// Get feed kinds once for use in multiple steps
const feedKinds = getFeedKinds ( ) ;
// Step 1: Initialize database
// Step 1: Initialize database
updateProgress ( 0 , 0 ) ;
updateProgress ( 0 , 0 ) ;
await getDB ( ) ;
await getDB ( ) ;
@ -67,95 +70,157 @@ export async function prewarmCaches(
// Step 2: Load user profile if logged in
// Step 2: Load user profile if logged in
updateProgress ( 1 , 0 ) ;
updateProgress ( 1 , 0 ) ;
if ( sessionManager . isLoggedIn ( ) ) {
try {
const pubkey = sessionManager . getSession ( ) ? . pubkey ;
if ( sessionManager . isLoggedIn ( ) ) {
if ( pubkey ) {
const pubkey = sessionManager . getSession ( ) ? . pubkey ;
const profileRelays = relayManager . getProfileReadRelays ( ) ;
if ( pubkey ) {
await nostrClient . fetchEvents (
const profileRelays = relayManager . getProfileReadRelays ( ) ;
[ { kinds : [ KIND . METADATA ] , authors : [ pubkey ] , limit : 1 } ] ,
// Use a shorter timeout for prewarming to avoid hanging
profileRelays ,
const prewarmTimeout = Math . min ( config . standardTimeout , 5000 ) ; // Max 5 seconds
{ useCache : 'cache-first' , cacheResults : true , timeout : config.standardTimeout }
await Promise . race ( [
) ;
nostrClient . fetchEvents (
[ { kinds : [ KIND . METADATA ] , authors : [ pubkey ] , limit : 1 } ] ,
profileRelays ,
{ useCache : 'cache-first' , cacheResults : true , timeout : prewarmTimeout }
) ,
new Promise ( ( _ , reject ) = >
setTimeout ( ( ) = > reject ( new Error ( 'Profile fetch timeout' ) ) , prewarmTimeout + 1000 )
)
] ) . catch ( ( ) = > {
// Ignore errors - prewarming is non-critical
} ) ;
}
}
}
} catch ( error ) {
// Ignore errors - prewarming is non-critical
}
}
updateProgress ( 1 , 100 ) ;
updateProgress ( 1 , 100 ) ;
// Step 3: Load user lists (contacts and follow sets)
// Step 3: Load user lists (contacts and follow sets)
updateProgress ( 2 , 0 ) ;
updateProgress ( 2 , 0 ) ;
if ( sessionManager . isLoggedIn ( ) ) {
try {
const pubkey = sessionManager . getSession ( ) ? . pubkey ;
if ( sessionManager . isLoggedIn ( ) ) {
if ( pubkey ) {
const pubkey = sessionManager . getSession ( ) ? . pubkey ;
const relays = [
if ( pubkey ) {
. . . config . defaultRelays ,
const relays = [
. . . config . profileRelays ,
. . . config . defaultRelays ,
. . . relayManager . getFeedReadRelays ( )
. . . config . profileRelays ,
] ;
. . . relayManager . getFeedReadRelays ( )
const uniqueRelays = [ . . . new Set ( relays ) ] ;
] ;
const uniqueRelays = [ . . . new Set ( relays ) ] ;
await Promise . all ( [
const prewarmTimeout = Math . min ( config . standardTimeout , 5000 ) ; // Max 5 seconds
nostrClient . fetchEvents (
[ { kinds : [ KIND . CONTACTS ] , authors : [ pubkey ] , limit : 1 } ] ,
await Promise . race ( [
uniqueRelays ,
Promise . all ( [
{ useCache : 'cache-first' , cacheResults : true , timeout : config.standardTimeout }
nostrClient . fetchEvents (
) ,
[ { kinds : [ KIND . CONTACTS ] , authors : [ pubkey ] , limit : 1 } ] ,
nostrClient . fetchEvents (
uniqueRelays ,
[ { kinds : [ KIND . FOLLOW_SET ] , authors : [ pubkey ] } ] ,
{ useCache : 'cache-first' , cacheResults : true , timeout : prewarmTimeout }
uniqueRelays ,
) ,
{ useCache : 'cache-first' , cacheResults : true , timeout : config.standardTimeout }
nostrClient . fetchEvents (
)
[ { kinds : [ KIND . FOLLOW_SET ] , authors : [ pubkey ] } ] ,
] ) ;
uniqueRelays ,
{ useCache : 'cache-first' , cacheResults : true , timeout : prewarmTimeout }
)
] ) ,
new Promise ( ( _ , reject ) = >
setTimeout ( ( ) = > reject ( new Error ( 'Lists fetch timeout' ) ) , prewarmTimeout + 1000 )
)
] ) . catch ( ( ) = > {
// Ignore errors - prewarming is non-critical
} ) ;
}
}
}
} catch ( error ) {
// Ignore errors - prewarming is non-critical
}
}
updateProgress ( 2 , 100 ) ;
updateProgress ( 2 , 100 ) ;
// Step 4: Load recent feed events
// Step 4: Load recent feed events
updateProgress ( 3 , 0 ) ;
updateProgress ( 3 , 0 ) ;
const feedKinds = getFeedKinds ( ) ;
try {
const feedRelays = relayManager . getFeedReadRelays ( ) ;
const feedRelays = relayManager . getFeedReadRelays ( ) ;
await nostrClient . fetchEvents (
const prewarmTimeout = Math . min ( config . standardTimeout , 5000 ) ; // Max 5 seconds
[ { kinds : feedKinds.slice ( 0 , 10 ) , limit : 50 } ] , // Load first 10 feed kinds, limit 50 events
feedRelays ,
await Promise . race ( [
{ useCache : 'cache-first' , cacheResults : true , timeout : config.standardTimeout }
nostrClient . fetchEvents (
) ;
[ { kinds : feedKinds.slice ( 0 , 10 ) , limit : 50 } ] , // Load first 10 feed kinds, limit 50 events
feedRelays ,
{ useCache : 'cache-first' , cacheResults : true , timeout : prewarmTimeout }
) ,
new Promise ( ( _ , reject ) = >
setTimeout ( ( ) = > reject ( new Error ( 'Feed events fetch timeout' ) ) , prewarmTimeout + 1000 )
)
] ) . catch ( ( ) = > {
// Ignore errors - prewarming is non-critical
} ) ;
} catch ( error ) {
// Ignore errors - prewarming is non-critical
}
updateProgress ( 3 , 100 ) ;
updateProgress ( 3 , 100 ) ;
// Step 5: Load profiles for recent events (if logged in)
// Step 5: Load profiles for recent events (if logged in)
updateProgress ( 4 , 0 ) ;
updateProgress ( 4 , 0 ) ;
if ( sessionManager . isLoggedIn ( ) ) {
try {
// Get some recent events to extract pubkeys
if ( sessionManager . isLoggedIn ( ) ) {
const { getRecentCachedEvents } = await import ( './event-cache.js' ) ;
// Get some recent events to extract pubkeys
const recentEvents = await getRecentCachedEvents ( feedKinds , 24 * 60 * 60 * 1000 , 20 ) ;
const { getRecentCachedEvents } = await import ( './event-cache.js' ) ;
const pubkeys = [ . . . new Set ( recentEvents . map ( e = > e . pubkey ) ) ] . slice ( 0 , 20 ) ;
const recentEvents = await getRecentCachedEvents ( feedKinds , 24 * 60 * 60 * 1000 , 20 ) ;
const pubkeys = [ . . . new Set ( recentEvents . map ( e = > e . pubkey ) ) ] . slice ( 0 , 20 ) ;
if ( pubkeys . length > 0 ) {
const profileRelays = relayManager . getProfileReadRelays ( ) ;
if ( pubkeys . length > 0 ) {
await nostrClient . fetchEvents (
const profileRelays = relayManager . getProfileReadRelays ( ) ;
[ { kinds : [ KIND . METADATA ] , authors : pubkeys , limit : 1 } ] ,
const prewarmTimeout = Math . min ( config . standardTimeout , 5000 ) ; // Max 5 seconds
profileRelays ,
{ useCache : 'cache-first' , cacheResults : true , timeout : config.standardTimeout }
await Promise . race ( [
) ;
nostrClient . fetchEvents (
[ { kinds : [ KIND . METADATA ] , authors : pubkeys , limit : 1 } ] ,
profileRelays ,
{ useCache : 'cache-first' , cacheResults : true , timeout : prewarmTimeout }
) ,
new Promise ( ( _ , reject ) = >
setTimeout ( ( ) = > reject ( new Error ( 'Profiles fetch timeout' ) ) , prewarmTimeout + 1000 )
)
] ) . catch ( ( ) = > {
// Ignore errors - prewarming is non-critical
} ) ;
}
}
}
} catch ( error ) {
// Ignore errors - prewarming is non-critical
}
}
updateProgress ( 4 , 100 ) ;
updateProgress ( 4 , 100 ) ;
// Step 6: Load RSS feeds (if logged in)
// Step 6: Load RSS feeds (if logged in)
updateProgress ( 5 , 0 ) ;
updateProgress ( 5 , 0 ) ;
if ( sessionManager . isLoggedIn ( ) ) {
try {
const pubkey = sessionManager . getSession ( ) ? . pubkey ;
if ( sessionManager . isLoggedIn ( ) ) {
if ( pubkey ) {
const pubkey = sessionManager . getSession ( ) ? . pubkey ;
const relays = [
if ( pubkey ) {
. . . config . defaultRelays ,
const relays = [
. . . config . profileRelays ,
. . . config . defaultRelays ,
. . . relayManager . getFeedReadRelays ( )
. . . config . profileRelays ,
] ;
. . . relayManager . getFeedReadRelays ( )
const uniqueRelays = [ . . . new Set ( relays ) ] ;
] ;
const uniqueRelays = [ . . . new Set ( relays ) ] ;
await nostrClient . fetchEvents (
const prewarmTimeout = Math . min ( config . standardTimeout , 5000 ) ; // Max 5 seconds
[ { kinds : [ KIND . RSS_FEED ] , authors : [ pubkey ] , limit : 10 } ] ,
uniqueRelays ,
await Promise . race ( [
{ useCache : 'cache-first' , cacheResults : true , timeout : config.standardTimeout }
nostrClient . fetchEvents (
) ;
[ { kinds : [ KIND . RSS_FEED ] , authors : [ pubkey ] , limit : 10 } ] ,
uniqueRelays ,
{ useCache : 'cache-first' , cacheResults : true , timeout : prewarmTimeout }
) ,
new Promise ( ( _ , reject ) = >
setTimeout ( ( ) = > reject ( new Error ( 'RSS feeds fetch timeout' ) ) , prewarmTimeout + 1000 )
)
] ) . catch ( ( ) = > {
// Ignore errors - prewarming is non-critical
} ) ;
}
}
}
} catch ( error ) {
// Ignore errors - prewarming is non-critical
}
}
updateProgress ( 5 , 100 ) ;
updateProgress ( 5 , 100 ) ;