siyuan-note/siyuan · error · Error

View state must not be empty

Error message

View state ${name} must not be empty

What it means

validateIdentityPart requires every component of a view state identity (scope, surface, hostID) to be a non-empty string. When one is empty or falsy, the library throws naming the offending part. View state keys are composed of these three parts, so an empty part would create ambiguous keys.

Solutions

  1. Ensure hostID (e.g. document/panel ID) is resolved before constructing ViewStateService
  2. Provide non-empty scope and surface constants for the component
  3. Defer service construction until identity is known, or pass a stable placeholder that is non-empty
  4. Guard construction: if (!identity.hostID) skip persisting view state

Example fix

// before
new ViewStateService({scope: "protyle", surface: "", hostID: id});
// after
new ViewStateService({scope: "protyle", surface: "editor", hostID: id});
Defensive patterns

Strategy: validation

Validate before calling

if (!identity.scope || !identity.surface || !identity.hostID) return; // skip persistence

Type guard

const hasIdentity = (i) => typeof i.scope === "string" && i.scope.length > 0 && typeof i.surface === "string" && i.surface.length > 0 && typeof i.hostID === "string" && i.hostID.length > 0;

Try / catch

try { const svc = new ViewStateService(identity); } catch (e) { if (e.message.includes("must not be empty")) svc = null; }

Prevention

When it happens

Trigger: Constructing ViewStateService or calling getViewStateKey (via validateIdentityPart, app/src/util/viewState.ts:64) with identity such as {scope: "", surface: "dock", hostID: "abc"} — typically an uninitialized hostID because the hosting element or document ID was not resolved yet.

Common situations: Creating a view-state service before the document/editor ID is assigned; passing an empty surface string for a plugin panel; hostID derived from an attribute that is missing on first render.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/917e5def8b2cdc5e. Report an issue: GitHub.

Appendix: source

Thrown at app/src/util/viewState.ts:64

};

const defaultTransport: IViewStateTransport = {
    async get(key) {
        const {fetchSyncPost} = await import("./fetch");
        const response = await fetchSyncPost("/api/storage/getViewState", {key});
        checkResponse(response, "get");
        return isViewStateData(response.data) ? response.data : {};
    },
    async patch(key, values, removeKeys) {
        const {fetchSyncPost} = await import("./fetch");
        const response = await fetchSyncPost("/api/storage/patchViewState", {key, values, removeKeys});
        checkResponse(response, "patch");
    },
};

const validateIdentityPart = (name: string, value: string) => {
    if (!value) {
        throw new Error(`View state ${name} must not be empty`);
    }
};

const validateField = (field: string) => {
    if (!field || field.length > 2048) {
        throw new Error("Invalid view state field");
    }
};

export const getViewStateKey = (identity: IViewStateIdentity) => {
    validateIdentityPart("scope", identity.scope);
    validateIdentityPart("surface", identity.surface);
    validateIdentityPart("hostID", identity.hostID);
    const key = [identity.scope, identity.surface, identity.hostID].map(encodeURIComponent).join(":");
    if (key.length > 1024) {
        throw new Error("View state key is too long");
    }
    return key;

View on GitHub (pinned to 9f775e8a12)