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 {};
};
// 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