Dashboard Widget Architecture
Technical deep-dive into dashboard widget packages — folder structure, widget.json manifest, dual registration, per-tile scope, RBAC, and licensing gates.
On this page
1. Package Model
Every dashboard widget is a Portable Module: a self-contained folder that can be shipped, uploaded to marketplace.sveltycms.com, and installed into any SveltyCMS installation without touching dashboard core code.
widgets/<kebab-folder>/
├── <component>.svelte # Required — the widget component
├── widget.json # Required — marketplace/telemetry manifest
├── <component>.mdx # Required — marketplace description (features, licensing, data source)
└── tests/ # Optional — unit tests
Why a folder per widget?
- Marketplace uploads — the marketplace API accepts package folders directly;
widget.jsonis the package manifest and the co-located<component>.mdxis the package description used for the marketplace listing. - Clean installs —
installDashboardWidget()writes the whole folder via the packageinstallPath, no file-by-file merging. - Stable registry keys — the folder id (
widget.json.id) is the key persisted insystem-preferences. The entry filename is alwaysindex.svelte; renaming the implementation file never resets user layouts.
2. The widget.json Manifest
{
"id": "system-health",
"name": "System Health",
"description": "Monitor system services and overall health",
"icon": "mdi:heart-pulse",
"version": "1.0.0",
"type": "dashboard-widget",
"author": "SveltyCMS",
"license": "free",
"price": 0,
"component": "index",
"defaultSize": { "w": 2, "h": 2 },
"category": "monitoring",
"sveltycms": ">=0.0.8"
}
| Field | Type | Description |
|---|---|---|
id |
string |
Kebab-case folder name (package id). |
name |
string |
Display name shown in the picker / marketplace. |
description |
string |
Short description for cards and listings. |
icon |
string |
Iconify icon id. |
version |
string |
Semver package version. |
type |
"dashboard-widget" |
Fixed type discriminator. |
author |
string |
Publisher name. |
license |
"free" \| "freemium" \| "paid" |
Monetization model. |
price |
number |
EUR price (0 for free). |
component |
string |
Entry filename without .svelte (always "index"). |
defaultSize |
{ w: 1-4, h: 1-4 } |
Recommended grid size. |
category |
"monitoring" \| "logs" \| "content" \| "static" |
Default fetch/cache/refresh behavior group. |
requiresPlugin |
string (optional) |
Picker omits the package unless this plugin is on. |
requiresPermission |
string (optional) |
RBAC permission required to add/view the widget (e.g. dashboard:read). |
requiresAdmin |
boolean (optional) |
Only administrators may add/view the widget. |
The manifest must agree with the component’s widgetMeta export (name, icon, description, defaultSize). manifest-registry.ts reads manifests at build time via import.meta.glob("./*/widget.json", { eager: true }).
3. Dual Registration
Widgets are registered in two places, both compile-time — Svelte modules are not evaluated to build the picker:
-
Server metadata —
+page.server.tsreadsgetInstalledDashboardWidgets()(widget.jsonglob) and maps picker entries viamanifestsToPickerList(). Packages whosesveltycmsrange the host does not satisfy are omitted. The same load hydrates the saved layout fromsystem.preferencesso the client skips a/api/system-preferencesround-trip. -
Client runtime —
+page.sveltebuilds the picker fromdata.availableWidgets(JSON). Widgets are optional: none ship on an empty dashboard.import.meta.glob("./widgets/*/*.svelte")is a lazy chunk map only — a package is imported when the user adds it or its tile intersects the viewport. Packages withrequiresPluginstay out of the picker until that plugin is enabled (Commerce Orders / Inventory →commerce, off by default). The registry is keyed by the package folder id (widget.json.id).
The folder id is the stable key — widget.json.id matches the folder, layouts persist that id, and the entry filename (index.svelte) is an implementation detail that can never break a saved layout. In dev, the Vite plugin (sveltyCmsPlugin) watches src/routes/(app)/dashboard/widgets/** and triggers a full reload on package add/remove — new widgets appear in the picker without a manual restart (same treatment as src/widgets).
Polling widgets pause while document.hidden is true or navigator.onLine is false, and refetch when the tab becomes visible again if the poll interval has elapsed. Package fetches go through sandboxedWidgetFetch: only same-origin /api/... paths, a 15-second abort timeout, CSRF headers, sanitized request headers, and cache: "no-store" instead of a _=Date.now() query string. Encoded traversal (%2E%2E, %2f, double-encoded %252e) is rejected before the request runs.
4. Base Widget Contract
All widgets compose base-widget.svelte, which provides fetch/poll/retry/cache/refresh chrome, the header, resize handles, and keyboard-accessible controls:
<BaseWidget
{label} {theme} {icon} {widgetId} {size}
endpoint="/api/dashboard/health"
pollInterval={5000}
onSizeChange={onSizeChange}
onCloseRequest={onRemove}
>
{#snippet children({ data, error, isLoading, refresh, sandbox })}
<!-- render data -->
{/snippet}
</BaseWidget>
Per-tile runtime scope
The page wraps each tile in widget-scope.svelte. BaseWidget reads from that context, so a package does not have to forward these props through its own signature:
| Scope value | Used for |
|---|---|
settings |
Persisted instance settings; settings.pollInterval overrides the package’s pollInterval after reload. |
tenantId / userId |
Namespacing the widget’s isolated storage. |
license |
The freemium/paid badge in the header. |
accessAllowed |
Manifest RBAC (requiresPermission / requiresAdmin) checked per tile. |
Header actions are standard across packages: refresh, expand (maximize dialog), configure (title / refresh rate / grid size), size menu, and remove. The children snippet receives { data, updateWidgetState, getWidgetState, refresh, isLoading, error, sandbox }; sandbox exposes fetch, storage, notify, requestRefresh, and updateSettings.
Core packages that currently declare requiresAdmin: database-pool-diagnostics, scim-status, and tenant-analytics.
HTTP failures throw WidgetHttpError; 4xx responses are not retried, while 5xx/429 use exponential backoff. A render crash is isolated by the page’s <svelte:boundary> and offers Retry/Remove instead of taking down the grid. Settings saves are merged into the persisted object, so widget-specific keys (collectionId, …) survive a title/refresh-rate edit.
5. Component Metadata (widgetMeta)
Each component still exports module-level metadata (keep it in sync with widget.json). The Add Widget picker reads widget.json only so Svelte modules stay lazy:
<script lang="ts" module>
export const widgetMeta = {
name: "System Health",
icon: "mdi:heart-pulse",
description: "Monitor system services and overall health",
defaultSize: { w: 2, h: 2 },
};
</script>
widget.json drives the picker, packaging, catalog, and telemetry. widgetMeta remains on the component for local documentation — keep the two in sync.
6. 🔑 Licensing Gates
Dashboard widgets use the same license model as content widgets and plugins:
| Model | Behavior |
|---|---|
| Free | No checks; bundled or community packages. |
| Freemium | 14-day key-less trial; premium features gate on checkExtensionLicense. |
| Paid | License required from install; widget renders an upgrade prompt otherwise. |
- Client-side gating —
checkExtensionLicense("dashboard", widgetId); on failure render<UpgradePrompt extensionId="dashboard:<widgetId>" price="…" />instead of content. - Server-side gating — premium dashboard endpoints are gated in
src/routes/api/[...path]/handlers/dashboard.ts(requireDashboardWidgetLicense/checkDashboardEndpointLicense), which callscheckExtensionLicense("dashboard", widgetId)and returns403 LICENSE_REQUIREDwhen the trial expired without a license. The endpoint → widget-id map (DASHBOARD_ENDPOINT_LICENSE) covers/api/dashboard/{audit, logs, security, scim, cache-metrics, online-user, metrics, commerce-orders, commerce-inventory}and/api/database/pool-diagnostics(gated directly inhandlers/system.ts). Free widgets are not gated. When you add a premium widget backed by a new endpoint, register it in that map.
Example (cache-monitor/index.svelte):
{#if licenseStatus && !licenseStatus.active && !licenseStatus.hasLicense}
<!-- Upgrade prompt -->
{:else}
<BaseWidget endpoint="/api/dashboard/cache-metrics" ...>…</BaseWidget>
{/if}
7. Installed-Package Inventory
manifest-registry.ts exposes the installed inventory for consumers that must not compile Svelte:
import { getInstalledDashboardWidgets } from "./widgets/manifest-registry";
const packages = getInstalledDashboardWidgets(); // sorted by name
// → [{ id: "system-health", name: "System Health", license: "free", … }]
Used by the marketplace catalog (marketplace-service.ts) and telemetry (telemetry-service.ts).