fix: save articles to localStorage cache after loading from relays

We were loading articles from relays but never saving them to cache,
which meant every refresh would query relays again. Now we:
1. Save to cache immediately after successfully loading from relays
2. Export saveToCache function for reuse
3. Add debug logs to track cache saves

This ensures articles are cached after first load, enabling instant
loading on subsequent visits/refreshes.
This commit is contained in:
Gigi
2025-10-31 00:41:08 +01:00
parent d0f942c495
commit c28052720e
2 changed files with 26 additions and 5 deletions

View File

@@ -6,7 +6,7 @@ import { nip19 } from 'nostr-tools'
import { AddressPointer } from 'nostr-tools/nip19'
import { Helpers } from 'applesauce-core'
import { queryEvents } from '../services/dataFetch'
import { fetchArticleByNaddr, getFromCache } from '../services/articleService'
import { fetchArticleByNaddr, getFromCache, saveToCache } from '../services/articleService'
import { fetchHighlightsForArticle } from '../services/highlightService'
import { ReadableContent } from '../services/readerService'
import { Highlight } from '../types/highlights'
@@ -364,10 +364,11 @@ export function useArticleLoader({
wasFirstEmitted: firstEmitted
})
const title = Helpers.getArticleTitle(finalEvent) || 'Untitled Article'
setCurrentTitle(title)
const image = Helpers.getArticleImage(finalEvent)
const summary = Helpers.getArticleSummary(finalEvent)
const published = Helpers.getArticlePublished(finalEvent)
setCurrentTitle(title)
setReaderContent({
title,
markdown: finalEvent.content,
@@ -382,7 +383,20 @@ export function useArticleLoader({
setCurrentArticleCoordinate(articleCoordinate)
setCurrentArticleEventId(finalEvent.id)
setCurrentArticle?.(finalEvent)
console.log('[article-loader] ✅ Finalized with event from relays')
// Save to cache for future loads
const articleContent = {
title,
markdown: finalEvent.content,
image,
summary,
published,
author: finalEvent.pubkey,
event: finalEvent
}
saveToCache(naddr, articleContent)
console.log('[article-loader] ✅ Finalized with event from relays and saved to cache')
} else {
// As a last resort, fall back to the legacy helper (which includes cache)
console.log('[article-loader] ⚠️ No events from relays, falling back to fetchArticleByNaddr')

View File

@@ -71,16 +71,23 @@ export function getFromCache(naddr: string): ArticleContent | null {
}
}
function saveToCache(naddr: string, content: ArticleContent): void {
export function saveToCache(naddr: string, content: ArticleContent): void {
try {
const cacheKey = getCacheKey(naddr)
console.log('[article-cache] 💾 Saving to cache', {
key: cacheKey,
title: content.title,
hasMarkdown: !!content.markdown,
markdownLength: content.markdown?.length
})
const cached: CachedArticle = {
content,
timestamp: Date.now()
}
localStorage.setItem(cacheKey, JSON.stringify(cached))
console.log('[article-cache] ✅ Successfully saved to cache')
} catch (err) {
console.warn('Failed to cache article:', err)
console.warn('[article-cache] Failed to cache article:', err)
// Silently fail if storage is full or unavailable
}
}