refactor: improve cache handling and logging in CategoryList component to prioritize prerendered cache and enhance data fetching clarity

This commit is contained in:
sebseb7
2025-07-23 10:24:41 +02:00
parent 95f303bc68
commit f3e8395000

View File

@@ -174,20 +174,27 @@ class CategoryList extends Component {
// Ensure cache exists // Ensure cache exists
windowObj.productCache = windowObj.productCache || {}; windowObj.productCache = windowObj.productCache || {};
// Get cache key // The cache is PRERENDERED - always use it first!
const cacheKey = `categoryTree_209_${currentLanguage}`; console.log('CategoryList: Checking prerendered cache', windowObj.productCache);
// Use either language-specific or default cache
const cacheKey = `categoryTree_209_${currentLanguage}`;
const defaultCacheKey = "categoryTree_209";
// Try language-specific cache first, then fall back to default
const categoryTree =
windowObj.productCache[cacheKey]?.categoryTree ||
windowObj.productCache[defaultCacheKey]?.categoryTree;
// USE THE CACHE directly - if it has data, use it
const categoryTree = windowObj.productCache[cacheKey]?.categoryTree;
if (categoryTree) { if (categoryTree) {
console.log('CategoryList: Using cached category tree'); console.log('CategoryList: Using prerendered cache');
this.processCategoryTree(categoryTree); this.processCategoryTree(categoryTree);
this.setState({ fetchedCategories: true }); this.setState({ fetchedCategories: true });
return; return;
} }
// No cache, so fetch // Only fetch if no prerendered cache exists
console.log('CategoryList: Cache not found, fetching from socket'); console.log('CategoryList: No prerendered cache, fetching from socket');
windowObj.productCache[cacheKey] = { fetching: true, timestamp: Date.now() }; windowObj.productCache[cacheKey] = { fetching: true, timestamp: Date.now() };
this.setState({ fetchedCategories: true }); this.setState({ fetchedCategories: true });
window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => { window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => {