diff --git a/backend/utils/wineCache.js b/backend/utils/wineCache.js index 5b4b3e1..8f6dbf6 100644 --- a/backend/utils/wineCache.js +++ b/backend/utils/wineCache.js @@ -6,9 +6,24 @@ function isFresh() { return cache.fetchedAt && (Date.now() - cache.fetchedAt) < CACHE_TTL_MS; } +let refreshPromise = null; + async function getWineCache(db) { if (isFresh()) return cache; + // Batch A: share a single in-flight refresh across concurrent callers — + // without this, N requests landing on an expired cache each run the + // full-catalog Firestore read (a cold-start stampede). + if (refreshPromise) return refreshPromise; + refreshPromise = refreshWineCache(db); + try { + return await refreshPromise; + } finally { + refreshPromise = null; + } +} + +async function refreshWineCache(db) { const [winesSnap, restaurantsSnap, historySnap] = await Promise.all([ db.collectionGroup('wines').select( 'producer', 'varietal', 'region', 'year', 'name', diff --git a/frontend/src/components/ErrorBoundary.js b/frontend/src/components/ErrorBoundary.js new file mode 100644 index 0000000..43797b5 --- /dev/null +++ b/frontend/src/components/ErrorBoundary.js @@ -0,0 +1,47 @@ +import React from 'react'; + +// Batch A: a single render exception anywhere in the tree used to +// whitescreen the whole app with no way back. This boundary catches it +// and offers a reload instead; saved wines and profiles live server-side +// and are unaffected. +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = { hasError: false }; + } + + static getDerivedStateFromError() { + return { hasError: true }; + } + + componentDidCatch(error, info) { + console.error('UI error boundary caught:', error, info); + } + + handleReload = () => { + window.location.reload(); + }; + + render() { + if (this.state.hasError) { + return ( +
Sorry — the app hit an unexpected error. Your saved wines and profile are safe.
+ +