mirror of
https://github.com/Alvin-Zilverstand/pokedex.git
synced 2026-03-06 11:07:31 +01:00
Increase cache duration to 24 hours and implement local storage caching for Pokémon data with lazy loading for images
This commit is contained in:
@@ -10,7 +10,7 @@ if ($conn->connect_error) {
|
|||||||
die("Connection failed: " . $conn->connect_error);
|
die("Connection failed: " . $conn->connect_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
header("Cache-Control: max-age=3600"); // Cache for 1 hour
|
header("Cache-Control: max-age=86400"); // Cache for 24 hours
|
||||||
|
|
||||||
if (isset($_GET['id'])) {
|
if (isset($_GET['id'])) {
|
||||||
$id = intval($_GET['id']);
|
$id = intval($_GET['id']);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const MAX_POKEMON = 151;
|
const MAX_POKEMON = 151;
|
||||||
|
const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
|
||||||
const listWrapper = document.querySelector(".list-wrapper");
|
const listWrapper = document.querySelector(".list-wrapper");
|
||||||
const searchInput = document.querySelector("#search-input");
|
const searchInput = document.querySelector("#search-input");
|
||||||
const numberFilter = document.querySelector("#number");
|
const numberFilter = document.querySelector("#number");
|
||||||
@@ -7,23 +8,42 @@ const notFoundMessage = document.querySelector("#not-found-message");
|
|||||||
|
|
||||||
let allPokemons = [];
|
let allPokemons = [];
|
||||||
|
|
||||||
fetch(`./get-pokemon.php`)
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
.then((response) => response.json())
|
const cachedData = localStorage.getItem("pokemons");
|
||||||
.then((data) => {
|
const cacheTimestamp = localStorage.getItem("pokemons_timestamp");
|
||||||
if (Array.isArray(data)) {
|
|
||||||
allPokemons = data;
|
if (cachedData && cacheTimestamp && (Date.now() - cacheTimestamp) < CACHE_DURATION) {
|
||||||
cacheImages(allPokemons);
|
allPokemons = JSON.parse(cachedData);
|
||||||
displayPokemons(allPokemons);
|
displayPokemons(allPokemons);
|
||||||
} else {
|
} else {
|
||||||
console.error("Unexpected response format:", data);
|
fetchPokemons();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function fetchPokemons() {
|
||||||
|
fetch(`./get-pokemon.php`)
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
allPokemons = data;
|
||||||
|
localStorage.setItem("pokemons", JSON.stringify(allPokemons));
|
||||||
|
localStorage.setItem("pokemons_timestamp", Date.now().toString());
|
||||||
|
displayPokemons(allPokemons);
|
||||||
|
} else {
|
||||||
|
console.error("Unexpected response format:", data);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error("Error fetching Pokémon data:", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchPokemonDataBeforeRedirect(id) {
|
async function fetchPokemonDataBeforeRedirect(id) {
|
||||||
try {
|
try {
|
||||||
const pokemon = await fetch(`./get-pokemon.php?id=${id}`).then((res) =>
|
const response = await fetch(`./get-pokemon.php?id=${id}`);
|
||||||
res.json()
|
const text = await response.text();
|
||||||
);
|
console.log(`Response for Pokémon ID ${id}:`, text);
|
||||||
|
const pokemon = JSON.parse(text);
|
||||||
|
|
||||||
if (pokemon.error) {
|
if (pokemon.error) {
|
||||||
throw new Error(pokemon.error);
|
throw new Error(pokemon.error);
|
||||||
@@ -36,27 +56,10 @@ async function fetchPokemonDataBeforeRedirect(id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cacheImages(pokemons) {
|
|
||||||
pokemons.forEach((pokemon) => {
|
|
||||||
const img = new Image();
|
|
||||||
img.src = pokemon.image_url;
|
|
||||||
img.onload = () => {
|
|
||||||
localStorage.setItem(`pokemon_image_${pokemon.id}`, pokemon.image_url);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCachedImageUrl(pokemonId) {
|
|
||||||
return localStorage.getItem(`pokemon_image_${pokemonId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
function displayPokemons(pokemons) {
|
function displayPokemons(pokemons) {
|
||||||
listWrapper.innerHTML = "";
|
listWrapper.innerHTML = "";
|
||||||
|
|
||||||
pokemons.forEach((pokemon) => {
|
pokemons.forEach((pokemon) => {
|
||||||
const cachedImageUrl = getCachedImageUrl(pokemon.id);
|
|
||||||
const imageUrl = cachedImageUrl || pokemon.image_url;
|
|
||||||
|
|
||||||
const listItem = document.createElement("div");
|
const listItem = document.createElement("div");
|
||||||
listItem.className = "list-item";
|
listItem.className = "list-item";
|
||||||
listItem.innerHTML = `
|
listItem.innerHTML = `
|
||||||
@@ -64,7 +67,7 @@ function displayPokemons(pokemons) {
|
|||||||
<p class="caption-fonts">#${pokemon.id}</p>
|
<p class="caption-fonts">#${pokemon.id}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="img-wrap">
|
<div class="img-wrap">
|
||||||
<img src="${imageUrl}" alt="${pokemon.name}" />
|
<img data-src="${pokemon.image_url}" alt="${pokemon.name}" />
|
||||||
</div>
|
</div>
|
||||||
<div class="name-wrap">
|
<div class="name-wrap">
|
||||||
<p class="body3-fonts">#${pokemon.name}</p>
|
<p class="body3-fonts">#${pokemon.name}</p>
|
||||||
@@ -82,6 +85,38 @@ function displayPokemons(pokemons) {
|
|||||||
|
|
||||||
listWrapper.appendChild(listItem);
|
listWrapper.appendChild(listItem);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
lazyLoadImages();
|
||||||
|
}
|
||||||
|
|
||||||
|
function lazyLoadImages() {
|
||||||
|
const images = document.querySelectorAll('.img-wrap img[data-src]');
|
||||||
|
const config = {
|
||||||
|
root: null,
|
||||||
|
rootMargin: '0px',
|
||||||
|
threshold: 0.1
|
||||||
|
};
|
||||||
|
|
||||||
|
let observer = new IntersectionObserver((entries, self) => {
|
||||||
|
entries.forEach(entry => {
|
||||||
|
if (entry.isIntersecting) {
|
||||||
|
preloadImage(entry.target);
|
||||||
|
self.unobserve(entry.target);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, config);
|
||||||
|
|
||||||
|
images.forEach(image => {
|
||||||
|
observer.observe(image);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function preloadImage(img) {
|
||||||
|
const src = img.getAttribute('data-src');
|
||||||
|
if (!src) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
img.src = src;
|
||||||
}
|
}
|
||||||
|
|
||||||
searchInput.addEventListener("keyup", handleSearch);
|
searchInput.addEventListener("keyup", handleSearch);
|
||||||
|
|||||||
Reference in New Issue
Block a user