PRIORITY: HIGHEST - Page updates must be visible immediately after clicking save, not after 30-60 seconds.
Previously, page updates were taking 30-60 seconds to appear due to aggressive caching at multiple layers:
- Unified cache: 30-60 second TTL
- API response headers: 5-30 minute cache headers
- Page cache: 10-30 minute TTL
- Client-side fetch: 30 second cache-control headers
This created a poor user experience where users would save changes and not see them reflected immediately.
File: app/utils/serverCache.ts
const CACHE_TTL = {
FAST: process.env.NODE_ENV === 'development' ? 5 * 1000 : 10 * 1000, // 5s dev, 10s prod
SLOW: process.env.NODE_ENV === 'development' ? 30 * 1000 : 60 * 1000, // 30s dev, 1min prod
}File: app/utils/pagesListCache.ts
private readonly HOT_TTL = 10 * 1000; // 10 seconds - IMMEDIATE UPDATES
private readonly WARM_TTL = 30 * 1000; // 30 seconds - FAST UPDATES
private readonly COLD_TTL = 60 * 1000; // 1 minute - QUICK UPDATESFile: app/api/pages/[id]/route.ts
// Development: 5s browser, 10s CDN
response.headers.set('Cache-Control', 'public, max-age=5, s-maxage=10');
// Production: 10s browser, 30s CDN
response.headers.set('Cache-Control', 'public, max-age=10, s-maxage=30, stale-while-revalidate=60');File: app/firebase/database/pages.ts
const response = await fetch(`/api/pages/${pageId}`, {
headers: {
'Cache-Control': 'no-cache', // No caching for immediate updates
}
});Every page save operation triggers immediate cache invalidation:
- Version Save (
app/firebase/database/versions.ts) - Page Update (
app/components/pages/PageView.tsx) - API Routes (
app/api/pages/[id]/versions/route.ts)
All call invalidatePageData(pageId, userId) which clears:
- Unified cache entries
- Page cache entries
- Recent edits cache
- User-specific caches
- Open a page in edit mode
- Make a change to the content
- Click "Save"
- EXPECTED: Changes appear immediately (within 1-2 seconds)
- FAILURE: If changes take >10 seconds, investigate caching
// Test immediate update visibility
test('page updates appear immediately after save', async () => {
const originalContent = await getPageContent(pageId);
const newContent = originalContent + ' UPDATED';
await savePage(pageId, newContent);
// Should see update within 2 seconds
const updatedContent = await getPageContent(pageId);
expect(updatedContent).toBe(newContent);
});- 30-60 second cache TTL
- 5-30 minute API cache headers
- Fewer database reads
- Poor user experience
- 5-10 second cache TTL
- 10-30 second API cache headers
- More database reads (but still cached)
- Excellent user experience
- Update Latency: Time from save to visible update
- Cache Hit Rates: Should remain >80% despite shorter TTL
- Database Read Costs: Monitor for significant increases
- User Experience: Feedback on update responsiveness
- CRITICAL: Update latency >10 seconds
- WARNING: Cache hit rate <70%
- INFO: Database read cost increase >50%
Consider WebSocket or Server-Sent Events for truly real-time updates without any caching delays.
Implement more granular cache invalidation based on content change types.
Optimize CDN/edge caching while maintaining immediate updates for the page author.
- Always consider immediate updates - default to shorter TTL
- Test save-to-visible latency before deploying
- Document cache behavior in code comments
- Implement proper invalidation for all new caches
- Check unified cache TTL settings
- Verify API response cache headers
- Confirm cache invalidation is working
- Test with browser dev tools (disable cache)
- Monitor console logs for cache hits/misses
app/utils/serverCache.ts- Main cache TTL settingsapp/utils/pagesListCache.ts- Page-specific cache TTLapp/utils/globalCacheInvalidation.ts- Cache invalidation utilitiesapp/firebase/database/pages.ts- Client-side fetch headers
app/firebase/database/versions.ts- Version save invalidationapp/components/pages/PageView.tsx- Client-side invalidationapp/api/pages/[id]/versions/route.ts- API route invalidation
app/api/pages/[id]/route.ts- Individual page APIapp/api/pages/route.ts- Pages list APIapp/middleware/apiOptimization.ts- Global API caching
REMEMBER: User experience is paramount. Immediate updates after save are a critical requirement that should never be compromised for cost optimization.