feat: preload logged-in user profile image for offline access

Preload profile images when profiles are fetched and when displayed
in the sidebar to ensure they're cached by the Service Worker for
offline access.
This commit is contained in:
Gigi
2025-10-31 01:34:34 +01:00
parent 43ed41bfae
commit 911215c0fb
2 changed files with 22 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import { useEventModel } from 'applesauce-react/hooks'
import { Models } from 'applesauce-core'
import IconButton from './IconButton'
import { faBooks } from '../icons/customIcons'
import { preloadImage } from '../hooks/useImageCache'
interface SidebarHeaderProps {
onToggleCollapse: () => void
@@ -36,6 +37,13 @@ const SidebarHeader: React.FC<SidebarHeaderProps> = ({ onToggleCollapse, onLogou
const profileImage = getProfileImage()
// Preload profile image for offline access
useEffect(() => {
if (profileImage) {
preloadImage(profileImage)
}
}, [profileImage])
// Close menu when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {

View File

@@ -5,6 +5,7 @@ import { IEventStore } from 'applesauce-core'
import { prioritizeLocalRelays, partitionRelays } from '../utils/helpers'
import { rebroadcastEvents } from './rebroadcastService'
import { UserSettings } from './settingsService'
import { preloadImage } from '../hooks/useImageCache'
/**
* Fetches profile metadata (kind:0) for a list of pubkeys
@@ -65,6 +66,19 @@ export const fetchProfiles = async (
const profiles = Array.from(profilesByPubkey.values())
// Preload profile images for offline access
for (const profile of profiles) {
try {
const profileData = JSON.parse(profile.content)
const picture = profileData.picture
if (picture) {
preloadImage(picture)
}
} catch {
// Ignore parse errors - profile content might be invalid JSON
}
}
// Rebroadcast profiles to local/all relays based on settings
if (profiles.length > 0) {
await rebroadcastEvents(profiles, relayPool, settings)