mirror of
https://github.com/ae-utbm/sith.git
synced 2026-05-18 23:18:08 +00:00
use sessionStorage to cache user pictures
Le sessionStorage est automatiquement vidé à la fermeture de la page, ce qui, dans le cas des photos, est un peu plus fiable et correspond un peu mieux à nos besoins.
This commit is contained in:
@@ -23,9 +23,9 @@ document.addEventListener("alpine:init", () => {
|
|||||||
|
|
||||||
async fetchPictures(): Promise<PictureSchema[]> {
|
async fetchPictures(): Promise<PictureSchema[]> {
|
||||||
// Check the cache before hitting the API.
|
// Check the cache before hitting the API.
|
||||||
const localStorageKey = "userPictures";
|
const storageKey = "userPictures";
|
||||||
const cacheContent: { userId: number; pictures: PictureSchema[] }[] = JSON.parse(
|
const cacheContent: { userId: number; pictures: PictureSchema[] }[] = JSON.parse(
|
||||||
localStorage.getItem(localStorageKey) || "[]",
|
sessionStorage.getItem(storageKey) || "[]",
|
||||||
);
|
);
|
||||||
const userPictures = cacheContent.find((obj) => obj.userId === config.userId);
|
const userPictures = cacheContent.find((obj) => obj.userId === config.userId);
|
||||||
if (
|
if (
|
||||||
@@ -45,12 +45,12 @@ document.addEventListener("alpine:init", () => {
|
|||||||
cacheContent.push({ userId: config.userId, pictures: pictures });
|
cacheContent.push({ userId: config.userId, pictures: pictures });
|
||||||
try {
|
try {
|
||||||
// cache only the pictures of the last 4 visited profiles
|
// cache only the pictures of the last 4 visited profiles
|
||||||
localStorage.setItem(localStorageKey, JSON.stringify(cacheContent.slice(-4)));
|
sessionStorage.setItem(storageKey, JSON.stringify(cacheContent.slice(-4)));
|
||||||
} catch {
|
} catch {
|
||||||
// an exception is raised if the localstorage is entirely filled.
|
// an exception is raised if the localstorage is entirely filled.
|
||||||
// To be as safe as possible, delete the cached pictures.
|
// To be as safe as possible, delete the cached pictures.
|
||||||
// A cache hit is not worth the page breaking.
|
// A cache hit is not worth the page breaking.
|
||||||
localStorage.removeItem(localStorageKey);
|
sessionStorage.removeItem(storageKey);
|
||||||
}
|
}
|
||||||
return pictures;
|
return pictures;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user