mirror of
https://github.com/dergigi/boris.git
synced 2025-12-17 06:34:24 +01:00
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:
@@ -6,7 +6,7 @@ import { nip19 } from 'nostr-tools'
|
|||||||
import { AddressPointer } from 'nostr-tools/nip19'
|
import { AddressPointer } from 'nostr-tools/nip19'
|
||||||
import { Helpers } from 'applesauce-core'
|
import { Helpers } from 'applesauce-core'
|
||||||
import { queryEvents } from '../services/dataFetch'
|
import { queryEvents } from '../services/dataFetch'
|
||||||
import { fetchArticleByNaddr, getFromCache } from '../services/articleService'
|
import { fetchArticleByNaddr, getFromCache, saveToCache } from '../services/articleService'
|
||||||
import { fetchHighlightsForArticle } from '../services/highlightService'
|
import { fetchHighlightsForArticle } from '../services/highlightService'
|
||||||
import { ReadableContent } from '../services/readerService'
|
import { ReadableContent } from '../services/readerService'
|
||||||
import { Highlight } from '../types/highlights'
|
import { Highlight } from '../types/highlights'
|
||||||
@@ -364,10 +364,11 @@ export function useArticleLoader({
|
|||||||
wasFirstEmitted: firstEmitted
|
wasFirstEmitted: firstEmitted
|
||||||
})
|
})
|
||||||
const title = Helpers.getArticleTitle(finalEvent) || 'Untitled Article'
|
const title = Helpers.getArticleTitle(finalEvent) || 'Untitled Article'
|
||||||
setCurrentTitle(title)
|
|
||||||
const image = Helpers.getArticleImage(finalEvent)
|
const image = Helpers.getArticleImage(finalEvent)
|
||||||
const summary = Helpers.getArticleSummary(finalEvent)
|
const summary = Helpers.getArticleSummary(finalEvent)
|
||||||
const published = Helpers.getArticlePublished(finalEvent)
|
const published = Helpers.getArticlePublished(finalEvent)
|
||||||
|
|
||||||
|
setCurrentTitle(title)
|
||||||
setReaderContent({
|
setReaderContent({
|
||||||
title,
|
title,
|
||||||
markdown: finalEvent.content,
|
markdown: finalEvent.content,
|
||||||
@@ -382,7 +383,20 @@ export function useArticleLoader({
|
|||||||
setCurrentArticleCoordinate(articleCoordinate)
|
setCurrentArticleCoordinate(articleCoordinate)
|
||||||
setCurrentArticleEventId(finalEvent.id)
|
setCurrentArticleEventId(finalEvent.id)
|
||||||
setCurrentArticle?.(finalEvent)
|
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 {
|
} else {
|
||||||
// As a last resort, fall back to the legacy helper (which includes cache)
|
// 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')
|
console.log('[article-loader] ⚠️ No events from relays, falling back to fetchArticleByNaddr')
|
||||||
|
|||||||
@@ -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 {
|
try {
|
||||||
const cacheKey = getCacheKey(naddr)
|
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 = {
|
const cached: CachedArticle = {
|
||||||
content,
|
content,
|
||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
}
|
}
|
||||||
localStorage.setItem(cacheKey, JSON.stringify(cached))
|
localStorage.setItem(cacheKey, JSON.stringify(cached))
|
||||||
|
console.log('[article-cache] ✅ Successfully saved to cache')
|
||||||
} catch (err) {
|
} 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
|
// Silently fail if storage is full or unavailable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user