From 4dd1b2d227311341712e7a96bf0289411ed9e60a Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Wed, 23 Jul 2025 10:25:04 +0200 Subject: [PATCH] refactor: update cache handling and logging in SharedCarousel component to prioritize prerendered cache and improve clarity in data fetching --- src/components/SharedCarousel.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/components/SharedCarousel.js b/src/components/SharedCarousel.js index a63ad84..3898519 100644 --- a/src/components/SharedCarousel.js +++ b/src/components/SharedCarousel.js @@ -35,14 +35,16 @@ const getProductCache = () => { return {}; }; -// Initialize categories - USE THE CACHE directly +// Initialize categories const initializeCategories = (language = 'en') => { - const cache = getProductCache(); - // Try language-specific cache first, then fallback to default - const categoryTree = cache[`categoryTree_209_${language}`]?.categoryTree || - (language === 'de' && cache["categoryTree_209"]?.categoryTree); + const productCache = getProductCache(); - 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 = () => { @@ -78,21 +80,24 @@ const SharedCarousel = () => { }, [i18n]); useEffect(() => { - // Check cache first + // The cache is PRERENDERED - check it first const cache = getProductCache(); - const categoryTree = cache[`categoryTree_209_${currentLanguage}`]?.categoryTree || - (currentLanguage === 'de' && cache["categoryTree_209"]?.categoryTree); + console.log('SharedCarousel: Checking prerendered cache', cache); + + // Try language-specific cache first, then fallback to default + const categoryTree = cache?.[`categoryTree_209_${currentLanguage}`]?.categoryTree || + cache?.["categoryTree_209"]?.categoryTree; // Use cache if available if (categoryTree) { - console.log('SharedCarousel: Using cached category tree'); + console.log('SharedCarousel: Using prerendered cache'); setRootCategories(processCategoryTree(categoryTree)); return; } // Only fetch if needed 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) => { if (response && response.success) { // Use translated data if available, otherwise fall back to original