refactor: update cache handling and logging in SharedCarousel component to prioritize prerendered cache and improve clarity in data fetching

This commit is contained in:
sebseb7
2025-07-23 10:25:04 +02:00
parent f3e8395000
commit 4dd1b2d227

View File

@@ -35,14 +35,16 @@ const getProductCache = () => {
return {}; return {};
}; };
// Initialize categories - USE THE CACHE directly // Initialize categories
const initializeCategories = (language = 'en') => { const initializeCategories = (language = 'en') => {
const cache = getProductCache(); const productCache = getProductCache();
// Try language-specific cache first, then fallback to default
const categoryTree = cache[`categoryTree_209_${language}`]?.categoryTree ||
(language === 'de' && cache["categoryTree_209"]?.categoryTree);
return processCategoryTree(categoryTree || {}); // The cache is PRERENDERED - always use it first!
// Try language-specific cache first, then fallback to default
const categoryTree = productCache?.[`categoryTree_209_${language}`]?.categoryTree ||
productCache?.["categoryTree_209"]?.categoryTree;
return categoryTree ? processCategoryTree(categoryTree) : [];
}; };
const SharedCarousel = () => { const SharedCarousel = () => {
@@ -78,21 +80,24 @@ const SharedCarousel = () => {
}, [i18n]); }, [i18n]);
useEffect(() => { useEffect(() => {
// Check cache first // The cache is PRERENDERED - check it first
const cache = getProductCache(); const cache = getProductCache();
const categoryTree = cache[`categoryTree_209_${currentLanguage}`]?.categoryTree || console.log('SharedCarousel: Checking prerendered cache', cache);
(currentLanguage === 'de' && cache["categoryTree_209"]?.categoryTree);
// Try language-specific cache first, then fallback to default
const categoryTree = cache?.[`categoryTree_209_${currentLanguage}`]?.categoryTree ||
cache?.["categoryTree_209"]?.categoryTree;
// Use cache if available // Use cache if available
if (categoryTree) { if (categoryTree) {
console.log('SharedCarousel: Using cached category tree'); console.log('SharedCarousel: Using prerendered cache');
setRootCategories(processCategoryTree(categoryTree)); setRootCategories(processCategoryTree(categoryTree));
return; return;
} }
// Only fetch if needed // Only fetch if needed
if (rootCategories.length === 0 && typeof window !== "undefined") { if (rootCategories.length === 0 && typeof window !== "undefined") {
console.log('SharedCarousel: Cache not found, fetching from socket'); console.log('SharedCarousel: No prerendered cache, fetching from socket');
window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => { window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => {
if (response && response.success) { if (response && response.success) {
// Use translated data if available, otherwise fall back to original // Use translated data if available, otherwise fall back to original