mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-03-23 21:05:07 +00:00
fix: ignore profile picture cache after profile picture gets updated
This commit is contained in:
38
frontend/src/lib/utils/profile-picture-util.ts
Normal file
38
frontend/src/lib/utils/profile-picture-util.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { browser } from '$app/environment';
|
||||
|
||||
type SkipCacheUntil = {
|
||||
[key: string]: number;
|
||||
};
|
||||
|
||||
export function getProfilePictureUrl(userId?: string) {
|
||||
if (!userId) return '';
|
||||
|
||||
let url = `/api/users/${userId}/profile-picture.png`;
|
||||
|
||||
if (browser) {
|
||||
const skipCacheUntil = getSkipCacheUntil(userId);
|
||||
const skipCache = skipCacheUntil > Date.now();
|
||||
if (skipCache) {
|
||||
const skipCacheParam = new URLSearchParams();
|
||||
skipCacheParam.append('skip-cache', skipCacheUntil.toString());
|
||||
url += '?' + skipCacheParam.toString();
|
||||
}
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function getSkipCacheUntil(userId: string) {
|
||||
const skipCacheUntil: SkipCacheUntil = JSON.parse(
|
||||
localStorage.getItem('skip-cache-until') ?? '{}'
|
||||
);
|
||||
return skipCacheUntil[userId] ?? 0;
|
||||
}
|
||||
|
||||
export function bustProfilePictureCache(userId: string) {
|
||||
const skipCacheUntil: SkipCacheUntil = JSON.parse(
|
||||
localStorage.getItem('skip-cache-until') ?? '{}'
|
||||
);
|
||||
skipCacheUntil[userId] = Date.now() + 1000 * 60 * 15; // 15 minutes
|
||||
localStorage.setItem('skip-cache-until', JSON.stringify(skipCacheUntil));
|
||||
}
|
||||
Reference in New Issue
Block a user