siyuan-note/siyuan · error · Error
View state key is too long
Error message
View state key is too long
What it means
getViewStateKey builds the storage key by URL-encoding and joining scope, surface, and hostID with colons, then enforces a maximum encoded key length of 1024 characters. Longer composite keys would exceed storage limits, so the library throws this Error at construction time of ViewStateService.
Solutions
- Shorten hostID to a stable short identifier (block/doc ID, or a hash of the long value)
- Cap identity parts at the component boundary before constructing the service
- Keep scope/surface as short constants rather than descriptive strings
- Catch the error and degrade to non-persistent state
Example fix
// before
new ViewStateService({scope, surface, hostID: window.location.href});
// after
const shortHost = await hashToId(window.location.href); // fixed-length digest
new ViewStateService({scope, surface, hostID: shortHost}); Defensive patterns
Strategy: validation
Validate before calling
const encoded = [scope, surface, hostID].map(encodeURIComponent).join(":");
if (encoded.length > 1024) hostID = await hashToId(hostID); Try / catch
try { const svc = new ViewStateService(identity); } catch (e) { if (e.message.includes("too long")) { identity.hostID = shortHash(identity.hostID); svc = new ViewStateService(identity); } } Prevention
- Hash or truncate long host identifiers to fixed-length IDs
- Remember non-ASCII IDs expand up to 9x when URL-encoded
- Keep scope and surface as short constant names
When it happens
Trigger: Constructing ViewStateService (via getViewStateKey, app/src/util/viewState.ts:80) where the encodeURIComponent-ed scope:surface:hostID join exceeds 1024 chars — usually a very long hostID such as a full URL, a concatenated multi-ID string, or unbounded user-derived text.
Common situations: Using an entire query string or asset path as hostID; encoding non-ASCII IDs (each char can become up to 9 encoded chars, tripling length); combining multiple identifiers into hostID instead of hashing.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Invalid view state field
- invalid view state data key
- invalid view state key
- View state must not be empty
- 106
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/df678b2d1c403de4.
Report an issue: GitHub.
Appendix: source
Thrown at app/src/util/viewState.ts:80
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;
};
export class ViewStateService {
public readonly key: string;
public readonly ready: Promise<void>;
private readonly data: TViewStateData = {};
private readonly pendingValues = new Map<string, TViewStateValue>();
private readonly pendingRemovals = new Set<string>();
private readonly flushDelay: number;
private readonly transport: IViewStateTransport;
private flushTimer?: ReturnType<typeof setTimeout>;
private flushChain: Promise<void> = Promise.resolve();
private destroyed = false;
private destroyPromise?: Promise<void>;
constructor(identity: IViewStateIdentity, options: IViewStateServiceOptions = {}) {View on GitHub (pinned to 9f775e8a12)