ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
perf(api): coalesce identical GETs that are in flight at the same moment
Juan Denis2634fdc4830d
diff --git a/frontend/src/layouts/DashboardLayout.jsx b/frontend/src/layouts/DashboardLayout.jsxindex 1290469d..401bbdc2 100644--- a/frontend/src/layouts/DashboardLayout.jsx+++ b/frontend/src/layouts/DashboardLayout.jsx@@ -9,7 +9,7 @@ import { AIProvider } from '../contexts/AIContext';import AIAssistant from '../components/ai/AIAssistant';import { ConfirmProvider } from '../contexts/ConfirmContext';import PluginLoader from '../plugins/PluginLoader';-import { refreshContributions, useContributions } from '../plugins/contributions';+import { ensureContributions, useContributions } from '../plugins/contributions';import useMediaQuery from '../hooks/useMediaQuery';import { useLockBodyScroll } from '../hooks/useLockBodyScroll';import api from '../services/api';@@ -82,7 +82,7 @@ const DashboardLayout = () => {// (Sidebar, CommandPalette, ExtensionRoutes, PageTitleUpdater) all// pick up the result via useContributions().useEffect(() => {- refreshContributions();+ ensureContributions();}, []);return (diff --git a/frontend/src/plugins/contributions.js b/frontend/src/plugins/contributions.jsindex 52e243b7..62e225b6 100644--- a/frontend/src/plugins/contributions.js+++ b/frontend/src/plugins/contributions.js@@ -204,6 +204,17 @@ function notify(value) {}}+// Load once. Callers that just need the envelope present (the dashboard shell+// on mount, useContributions' first subscriber) want THIS, not+// refreshContributions — that one is a deliberate re-fetch and starts a new+// request every time it is called, so using it to mean "make sure this is+// loaded" fetched the same envelope twice on every app load.+export function ensureContributions() {+ return cachedPromise || refreshContributions();+}++// Force a re-fetch. Correct after installing, enabling or removing an+// extension; wrong as a way to say "load this if it isn't loaded".export function refreshContributions() {cachedPromise = api.getPluginContributions().then(async (data) => {@@ -239,8 +250,8 @@ export function useContributions() {useEffect(() => {subscribers.add(setValue);- if (!cachedPromise) refreshContributions();- else if (cachedValue) setValue(cachedValue);+ ensureContributions();+ if (cachedValue) setValue(cachedValue);return () => subscribers.delete(setValue);}, []);diff --git a/frontend/src/services/api/client.js b/frontend/src/services/api/client.jsindex e2a7e03f..82cb44ff 100644--- a/frontend/src/services/api/client.js+++ b/frontend/src/services/api/client.js@@ -14,9 +14,49 @@ const API_BASE_URL = import.meta.env.DEV? '/api/v1': normalizeApiBaseUrl(import.meta.env.VITE_API_URL);+// Requests that are safe to share with a concurrent identical caller: a plain+// GET with no body, no custom headers (X-DB-Password and friends change what+// comes back) and no abort signal (one caller aborting must not cancel+// someone else's request).+// A joining caller gets its own copy, so the two callers keep the independent+// objects they had when each ran its own fetch — one of them mutating the+// result (sorting an array in place, say) must not corrupt the other's.+const cloneResult = (data) => {+ if (data === null || typeof data !== 'object') return data;+ try {+ return structuredClone(data);+ } catch {+ return data; // exotic payload — sharing beats throwing+ }+};++const isCoalescable = (options) => (+ (!options.method || options.method === 'GET')+ && !options.body+ && !options.headers+ && !options.signal+);+class ApiClient {constructor() {this.baseUrl = API_BASE_URL;+ // In-flight GET coalescing. Two callers asking for the same URL at the+ // same moment share one network request instead of racing.+ //+ // This is NOT a cache — the entry is dropped the moment the request+ // settles, so nobody ever reads a stale body. It only collapses the+ // overlap, which is where the duplicates actually came from: React's+ // StrictMode double-invokes every effect in dev, two independent+ // components own the same endpoint (/system/notices,+ // /plugins/contributions), and a poller whose previous tick has not+ // returned fires again on schedule. A DevTools capture of one page load+ // showed the same endpoint requested up to five times concurrently.+ //+ // It matters more than it looks: in dev the API shares the browser's+ // six-connection HTTP/1.1 budget with Vite's module graph, so a+ // redundant request does not merely waste the backend's time, it holds+ // a connection the page needs to finish rendering.+ this._inflight = new Map();}getToken() {@@ -36,7 +76,22 @@ class ApiClient {localStorage.removeItem('workspace_accent');}- async request(endpoint, options = {}) {+ request(endpoint, options = {}) {+ if (!isCoalescable(options)) return this._request(endpoint, options);++ // Scope the key by workspace: the same path returns different rows+ // under a different X-Workspace-Id.+ const key = `${localStorage.getItem('active_workspace_id') || ''}|${endpoint}`;+ const existing = this._inflight.get(key);+ if (existing) return existing.then(cloneResult);++ const pending = this._request(endpoint, options)+ .finally(() => { this._inflight.delete(key); });+ this._inflight.set(key, pending);+ return pending;+ }++ async _request(endpoint, options = {}) {const url = `${this.baseUrl}${endpoint}`;const token = this.getToken();