Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions backend/utils/wineCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
47 changes: 47 additions & 0 deletions frontend/src/components/ErrorBoundary.js
Original file line number Diff line number Diff line change
@@ -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 (
<div style={{ padding: '48px 24px', textAlign: 'center', fontFamily: 'sans-serif', color: '#333' }}>
<h2>Something went wrong</h2>
<p>Sorry — the app hit an unexpected error. Your saved wines and profile are safe.</p>
<button
onClick={this.handleReload}
style={{
marginTop: 12, padding: '12px 24px', fontSize: 16, cursor: 'pointer',
background: '#8b0000', color: '#fff', border: 'none', borderRadius: 8
}}
>
Reload the app
</button>
</div>
);
}
return this.props.children;
}
}

export default ErrorBoundary;
5 changes: 4 additions & 1 deletion frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import ErrorBoundary from './components/ErrorBoundary';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
<ErrorBoundary>
<App />
</ErrorBoundary>
</React.StrictMode>
);

Expand Down