{"record":{"id":"df678b2d1c403de4","repo":"siyuan-note/siyuan","slug":"view-state-key-is-too-long","errorCode":null,"errorMessage":"View state key is too long","messagePattern":"View state key is too long","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/util/viewState.ts","lineNumber":80,"sourceCode":"const validateIdentityPart = (name: string, value: string) => {\n    if (!value) {\n        throw new Error(`View state ${name} must not be empty`);\n    }\n};\n\nconst validateField = (field: string) => {\n    if (!field || field.length > 2048) {\n        throw new Error(\"Invalid view state field\");\n    }\n};\n\nexport const getViewStateKey = (identity: IViewStateIdentity) => {\n    validateIdentityPart(\"scope\", identity.scope);\n    validateIdentityPart(\"surface\", identity.surface);\n    validateIdentityPart(\"hostID\", identity.hostID);\n    const key = [identity.scope, identity.surface, identity.hostID].map(encodeURIComponent).join(\":\");\n    if (key.length > 1024) {\n        throw new Error(\"View state key is too long\");\n    }\n    return key;\n};\n\nexport class ViewStateService {\n    public readonly key: string;\n    public readonly ready: Promise<void>;\n    private readonly data: TViewStateData = {};\n    private readonly pendingValues = new Map<string, TViewStateValue>();\n    private readonly pendingRemovals = new Set<string>();\n    private readonly flushDelay: number;\n    private readonly transport: IViewStateTransport;\n    private flushTimer?: ReturnType<typeof setTimeout>;\n    private flushChain: Promise<void> = Promise.resolve();\n    private destroyed = false;\n    private destroyPromise?: Promise<void>;\n\n    constructor(identity: IViewStateIdentity, options: IViewStateServiceOptions = {}) {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/util/viewState.ts#L62-L98","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nnew ViewStateService({scope, surface, hostID: window.location.href});\n// after\nconst shortHost = await hashToId(window.location.href); // fixed-length digest\nnew ViewStateService({scope, surface, hostID: shortHost});","handlingStrategy":"validation","validationCode":"const encoded = [scope, surface, hostID].map(encodeURIComponent).join(\":\");\nif (encoded.length > 1024) hostID = await hashToId(hostID);","typeGuard":null,"tryCatchPattern":"try { const svc = new ViewStateService(identity); } catch (e) { if (e.message.includes(\"too long\")) { identity.hostID = shortHash(identity.hostID); svc = new ViewStateService(identity); } }","preventionTips":["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"],"tags":["validation","view-state","key-length"],"backgroundTag":"value-out-of-range","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}