feat(hosting): per-site nginx micro-cache toggle

Opt-in 10s FastCGI/proxy micro-cache per site (migration 053) with safe
bypasses for logged-in/admin/cart cookies and paths, X-SK-Cache header
for verification, and a shared-zone purge. Flows through the drift-safe
vhost render seam so detection and writes always agree.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Juan DenisandClaude Fable 5 committed 2026-07-03 15:21:50 -04:00
1 parent 3cbbcb5e6b
commit 96d162e9a4
7 files changed
+663

No files matched your search

@@ -0,0 +1,117 @@
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 (
<div className="app-panel">
<div className="app-panel-header">
<Zap />
<span>Micro-cache</span>
</div>
<div className="app-panel-body">
<p className="app-panel-hint">
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.
</p>
<div className="settings-row">
<div className="settings-label">
<span>Enable micro-cache</span>
<span className="settings-hint">
Rewrites this site&apos;s nginx config with the cache rules.
Turning it off removes them again.
</span>
</div>
<div className="settings-control">
<Switch
checked={enabled}
onCheckedChange={handleToggle}
disabled={saving}
aria-label="Enable micro-cache"
/>
{saving && <span className="settings-saving">Saving...</span>}
</div>
</div>
{enabled && (
<div className="settings-row">
<div className="settings-label">
<span>Clear cache</span>
<span className="settings-hint">
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.
</span>
</div>
<div className="settings-control">
<Button variant="outline" size="sm" onClick={handlePurge} disabled={purging}>
<Eraser size={14} />
{purging ? 'Clearing…' : 'Clear cache'}
</Button>
</div>
</div>
)}
<p className="app-panel-hint">
To verify it works, check the <code>X-SK-Cache</code> response header
on the site: <code>HIT</code> means the page came from the cache,
<code> MISS</code>/<code>EXPIRED</code> that it was fetched fresh, and
<code> BYPASS</code> that a bypass rule applied.
</p>
</div>
</div>
);
};
export default MicroCachePanel;
@@ -16,6 +16,7 @@ import {
CircleCheck,
CircleX,
Sparkles,
Zap,
} from 'lucide-react';
import api from '../../services/api';
import { useToast } from '../../contexts/ToastContext';
@@ -25,6 +26,7 @@ import ProtectionPanel from '../backups/ProtectionPanel';
import ContainerOpsPanel from '../apps/ContainerOpsPanel';
import VolumesPanel from '../apps/VolumesPanel';
import ResourceLimitsPanel from '../apps/ResourceLimitsPanel';
import MicroCachePanel from '../apps/MicroCachePanel';
import AppWafPanel from '../apps/AppWafPanel';
import BuildTab from '../appdetail/BuildTab';
import DeployTab from '../appdetail/DeployTab';
@@ -45,6 +47,9 @@ import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from '@
function buildSettingsGroups(app) {
const isDocker = app.isDocker ?? app.app_type === 'docker';
const isPython = app.isPython ?? ['flask', 'django'].includes(app.app_type);
// Micro-cache applies to anything nginx fronts with an upstream (proxied
// or PHP-FPM); static sites already serve files straight off disk.
const isCacheable = isDocker || isPython || ['php', 'wordpress'].includes(app.app_type);
return [
{
@@ -56,6 +61,7 @@ function buildSettingsGroups(app) {
{ id: 'domain', label: 'Domain & SSL', icon: Shield },
...(isDocker ? [{ id: 'ops', label: 'Container Ops', icon: Boxes }] : []),
...(isDocker ? [{ id: 'resources', label: 'Resource Limits', icon: Gauge }] : []),
...(isCacheable ? [{ id: 'cache', label: 'Cache', icon: Zap }] : []),
],
},
{
@@ -308,6 +314,15 @@ const SettingsTab = ({ app, deployConfig, domains, primaryDomain, onUpdate }) =>
</div>
)}
{/* Cache — opt-in nginx micro-cache (task #21): 10s page cache
with auth/admin/cart bypasses and a manual purge. */}
{section === 'cache' && (
<div className="svc-settings__section">
<h3 className="svc-settings__section-title">Cache</h3>
<MicroCachePanel app={app} onChanged={onUpdate} />
</div>
)}
{/* Storage (Docker only) — first-class managed volumes that
survive redeploys, replacing fragile relative bind mounts. */}
{section === 'storage' && (
+9
View File
@@ -127,6 +127,15 @@ export async function updateAppResources(id, data) {
return this.request(`/apps/${id}/resources`, { method: 'PUT', body: data });
}
// Per-site nginx micro-cache (short-TTL page cache with auth/admin/cart bypasses).
export async function setMicroCache(id, enabled) {
return this.request(`/apps/${id}/micro-cache`, { method: 'PUT', body: { enabled } });
}
export async function purgeMicroCache(id) {
return this.request(`/apps/${id}/micro-cache/purge`, { method: 'POST' });
}
export async function startApp(id) {
return this.request(`/apps/${id}/start`, { method: 'POST' });
}