import { useState } from 'react'; import { Zap, Eraser } from 'lucide-react'; import api from '../../services/api'; import { useToast } from '../../contexts/ToastContext'; import { Button } from '@/components/ui/button'; import { Switch } from '@/components/ui/switch'; // Micro-cache panel (task #21) — opt-in nginx page cache per site. The // backend rewrites the site's vhost with short-TTL cache directives plus // hard bypasses for anything personalized (logged-in cookies, carts, // admin/login paths, non-GET requests, query strings). const MicroCachePanel = ({ app, onChanged }) => { const toast = useToast(); const [enabled, setEnabled] = useState(!!app.micro_cache_enabled); const [saving, setSaving] = useState(false); const [purging, setPurging] = useState(false); async function handleToggle(next) { setSaving(true); setEnabled(next); // optimistic; reverted on failure try { const data = await api.setMicroCache(app.id, next); if (data.warning) toast.warning(data.warning); if (data.note) { toast.info(data.note); } else { toast.success(next ? 'Micro-cache enabled — the site config was updated.' : 'Micro-cache disabled — the site config was updated.'); } onChanged?.(); } catch (err) { setEnabled(!next); toast.error(err.message || 'Failed to update micro-cache'); } finally { setSaving(false); } } async function handlePurge() { if (!confirm('Clear the micro-cache now? This clears cached pages for every site using the shared cache (entries expire within 10 seconds anyway).')) return; setPurging(true); try { const data = await api.purgeMicroCache(app.id); toast.success(data.message || 'Micro-cache cleared'); } catch (err) { toast.error(err.message || 'Failed to clear the micro-cache'); } finally { setPurging(false); } } return (
Micro-cache

Caches full pages in nginx for 10 seconds, so traffic spikes hit the cache instead of your app — a big, cheap win for WordPress and PHP sites. It is safe to enable: requests from logged-in users, carts and checkouts, admin and login pages, non-GET requests, and URLs with query strings always bypass the cache and reach the app directly.

Enable micro-cache Rewrites this site's nginx config with the cache rules. Turning it off removes them again.
{saving && Saving...}
{enabled && (
Clear cache Entries expire on their own within 10 seconds; use this when a change must be visible immediately. The cache is shared, so this clears it for every site that uses it.
)}

To verify it works, check the X-SK-Cache response header on the site: HIT means the page came from the cache, MISS/EXPIRED that it was fetched fresh, and BYPASS that a bypass rule applied.

); }; export default MicroCachePanel;