fix(caddy): decouple status check from routes fetch, use useEffect for load trigger
Routes now load immediately from DB without waiting for the Caddy Admin API status check (which can take up to 2s timeout). A dedicated useEffect on caddyEnabled replaces the unreliable fire-and-forget call inside loadSettings.
This commit is contained in:
@ -223,6 +223,10 @@ export default function Settings({ currentUser: _currentUser }: SettingsProps) {
|
|||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (caddyEnabled) loadCaddyRoutes();
|
||||||
|
}, [caddyEnabled]);
|
||||||
|
|
||||||
async function loadSettings() {
|
async function loadSettings() {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError('');
|
setError('');
|
||||||
@ -255,7 +259,6 @@ export default function Settings({ currentUser: _currentUser }: SettingsProps) {
|
|||||||
setSemaphoreProjectId(data.semaphore_project_id || '');
|
setSemaphoreProjectId(data.semaphore_project_id || '');
|
||||||
setCaddyEnabled(data.caddy_enabled === 'true');
|
setCaddyEnabled(data.caddy_enabled === 'true');
|
||||||
setCaddyAdminUrl(data.caddy_admin_url || 'http://localhost:2019');
|
setCaddyAdminUrl(data.caddy_admin_url || 'http://localhost:2019');
|
||||||
if (data.caddy_enabled === 'true') loadCaddyRoutes();
|
|
||||||
} catch {
|
} catch {
|
||||||
setError('Network error loading settings.');
|
setError('Network error loading settings.');
|
||||||
} finally {
|
} finally {
|
||||||
@ -357,25 +360,17 @@ export default function Settings({ currentUser: _currentUser }: SettingsProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadCaddyRoutes() {
|
async function loadCaddyRoutes() {
|
||||||
const [statusResult, routesResult] = await Promise.allSettled([
|
// Status check is background — never blocks route display
|
||||||
authFetch('/api/caddy/status'),
|
authFetch('/api/caddy/status')
|
||||||
authFetch('/api/caddy/routes'),
|
.then(res => res.ok ? res.json() : null)
|
||||||
]);
|
.then(s => setCaddyStatus(s?.available ? 'available' : 'unavailable'))
|
||||||
if (statusResult.status === 'fulfilled' && statusResult.value.ok) {
|
.catch(() => setCaddyStatus('unavailable'));
|
||||||
try {
|
|
||||||
const s = await statusResult.value.json();
|
// Routes load immediately from DB
|
||||||
setCaddyStatus(s.available ? 'available' : 'unavailable');
|
try {
|
||||||
} catch {
|
const res = await authFetch('/api/caddy/routes');
|
||||||
setCaddyStatus('unavailable');
|
if (res.ok) setCaddyRoutes(await res.json());
|
||||||
}
|
} catch {}
|
||||||
} else {
|
|
||||||
setCaddyStatus('unavailable');
|
|
||||||
}
|
|
||||||
if (routesResult.status === 'fulfilled' && routesResult.value.ok) {
|
|
||||||
try {
|
|
||||||
setCaddyRoutes(await routesResult.value.json());
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleAddRoute() {
|
async function handleAddRoute() {
|
||||||
@ -918,7 +913,7 @@ export default function Settings({ currentUser: _currentUser }: SettingsProps) {
|
|||||||
</span>
|
</span>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => { setCaddyEnabled(v => !v); if (!caddyEnabled) loadCaddyRoutes(); }}
|
onClick={() => setCaddyEnabled((v: boolean) => !v)}
|
||||||
className={`relative w-10 h-5 rounded-full transition-all duration-200 focus:outline-none ${caddyEnabled ? 'bg-sky-600 shadow-[0_0_10px_rgba(2,132,199,0.4)]' : 'bg-slate-800 border border-slate-700'}`}
|
className={`relative w-10 h-5 rounded-full transition-all duration-200 focus:outline-none ${caddyEnabled ? 'bg-sky-600 shadow-[0_0_10px_rgba(2,132,199,0.4)]' : 'bg-slate-800 border border-slate-700'}`}
|
||||||
>
|
>
|
||||||
<span className={`absolute top-0.5 w-4 h-4 bg-white rounded-full shadow transition-all duration-200 ${caddyEnabled ? 'left-5' : 'left-0.5'}`} />
|
<span className={`absolute top-0.5 w-4 h-4 bg-white rounded-full shadow transition-all duration-200 ${caddyEnabled ? 'left-5' : 'left-0.5'}`} />
|
||||||
|
|||||||
Reference in New Issue
Block a user