Skip to content

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.

Fix first-run sidebar preferences and extension loading [skip version]

Juan Denisfc5a04176417
diff --git a/backend/app/api/auth.py b/backend/app/api/auth.py
index 975c78c9..8e55f2e6 100644
--- a/backend/app/api/auth.py
+++ b/backend/app/api/auth.py
@@ -585,7 +585,7 @@ def update_current_user():
config = data['sidebar_config']
if isinstance(config, dict):
preset = config.get('preset', 'full')
- valid_presets = ['full', 'web', 'email', 'devops', 'minimal', 'custom']
+ valid_presets = ['recommended', 'full', 'web', 'email', 'devops', 'minimal', 'custom']
if preset not in valid_presets:
return jsonify({'error': f'Invalid sidebar preset: {preset}'}), 400
hidden = config.get('hiddenItems', [])
diff --git a/backend/tests/test_setup_sidebar_preset.py b/backend/tests/test_setup_sidebar_preset.py
new file mode 100644
index 00000000..70ad8b81
--- /dev/null
+++ b/backend/tests/test_setup_sidebar_preset.py
@@ -0,0 +1,19 @@
+"""The wizard's default sidebar preference must survive onboarding."""
+
+
+def test_recommended_sidebar_preset_is_saved_and_read_back(client, auth_headers):
+ preference = {'preset': 'recommended', 'hiddenItems': []}
+ response = client.put('/api/v1/auth/me', headers=auth_headers,
+ json={'sidebar_config': preference})
+ assert response.status_code == 200
+ assert response.json['user']['sidebar_config'] == preference
+ assert client.get('/api/v1/auth/me', headers=auth_headers).json['user']['sidebar_config'] == preference
+
+
+def test_invalid_sidebar_preset_does_not_overwrite_saved_preference(client, auth_headers):
+ preference = {'preset': 'recommended', 'hiddenItems': []}
+ client.put('/api/v1/auth/me', headers=auth_headers, json={'sidebar_config': preference})
+ response = client.put('/api/v1/auth/me', headers=auth_headers,
+ json={'sidebar_config': {'preset': 'unknown', 'hiddenItems': []}})
+ assert response.status_code == 400
+ assert client.get('/api/v1/auth/me', headers=auth_headers).json['user']['sidebar_config'] == preference
diff --git a/frontend/src/plugins/contributions.js b/frontend/src/plugins/contributions.js
index 0208b11d..304ba2ef 100644
--- a/frontend/src/plugins/contributions.js
+++ b/frontend/src/plugins/contributions.js
@@ -20,6 +20,7 @@
*/
import { useEffect, useState } from 'react';
import api from '../services/api';
+import { useAuth } from '../contexts/useAuth';
import pluginsManifest from './plugins-manifest.json';
import { loadRuntimeFrontends, getRuntimeModule } from './runtime/loader';
@@ -218,6 +219,8 @@ export function resolveCustomLayout(layoutId, layouts) {
let cachedPromise = null;
let cachedValue = null;
+// Responses from before sign-out or a newer refresh must not restore stale UI.
+let generation = 0;
const subscribers = new Set();
function notify(value) {
@@ -233,14 +236,22 @@ function notify(value) {
// 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() {
+ if (!api.getToken()) return refreshContributions();
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() {
+ const requestGeneration = ++generation;
+ if (!api.getToken()) {
+ cachedPromise = null;
+ notify(EMPTY);
+ return Promise.resolve(EMPTY);
+ }
cachedPromise = api.getPluginContributions()
.then(async (data) => {
+ if (requestGeneration !== generation) return cachedValue || EMPTY;
const merged = normalizeContributions({ ...EMPTY, ...(data || {}) });
// Load any runtime ESM bundles BEFORE notifying, so contributed
// routes render with their components already available (fail-soft:
@@ -249,6 +260,7 @@ export function refreshContributions() {
// built against an incompatible SDK before fetching it (plan 32 #1).
await loadRuntimeFrontends(
merged.frontends, merged.panel_sdk_version || merged.sdk_version);
+ if (requestGeneration !== generation) return cachedValue || EMPTY;
// Mark the envelope as loaded so consumers (e.g. the NotFound page)
// can tell "contributions still loading" from "genuinely no route".
merged.__ready = true;
@@ -256,6 +268,7 @@ export function refreshContributions() {
return merged;
})
.catch(() => {
+ if (requestGeneration !== generation) return cachedValue || EMPTY;
// If the backend contribution endpoint is unavailable (common
// while running only the Vite dev server), use the active plugin
// manifest baked into this frontend build instead of leaving
@@ -269,6 +282,7 @@ export function refreshContributions() {
}
export function useContributions() {
+ const { isAuthenticated } = useAuth();
const [value, setValue] = useState(cachedValue || EMPTY);
useEffect(() => {
@@ -276,7 +290,7 @@ export function useContributions() {
ensureContributions();
if (cachedValue) setValue(cachedValue);
return () => subscribers.delete(setValue);
- }, []);
+ }, [isAuthenticated]);
- return value;
+ return isAuthenticated ? value : EMPTY;
}