{"record":{"id":"b059c87ce997faaf","repo":"siyuan-note/siyuan","slug":"view-state-value-is-too-large","errorCode":null,"errorMessage":"View state value is too large","messagePattern":"View state value is too large","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/util/viewState.ts","lineNumber":239,"sourceCode":"                const nextRemoveKeys = [...removeKeys, field];\n                if (count > 0 && (count >= MAX_PATCH_ENTRIES ||\n                    getPatchByteLength(values, nextRemoveKeys) > MAX_PATCH_BYTES)) {\n                    break;\n                }\n                removeKeys.push(field);\n                this.pendingRemovals.delete(field);\n                count++;\n            }\n        }\n        if (count === 0) {\n            throw new Error(\"View state patch cannot be split within the storage limits\");\n        }\n        return {values, removeKeys};\n    }\n\n    private validateValue(field: string, value: TViewStateValue) {\n        if (getPatchByteLength({[field]: value}, []) > MAX_PATCH_BYTES) {\n            throw new Error(\"View state value is too large\");\n        }\n    }\n\n    private scheduleFlush() {\n        this.clearFlushTimer();\n        this.flushTimer = setTimeout(() => {\n            this.flushTimer = undefined;\n            this.flush().catch((error) => console.error(error));\n        }, this.flushDelay);\n    }\n\n    private clearFlushTimer() {\n        if (this.flushTimer !== undefined) {\n            clearTimeout(this.flushTimer);\n            this.flushTimer = undefined;\n        }\n    }\n","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/util/viewState.ts#L221-L257","documentation":"validateValue checks each field value at set/patch time by JSON-encoding {field: value} and comparing against MAX_PATCH_BYTES (256 KiB, the per-patch storage limit). Any single value that cannot fit into one patch alone is rejected immediately so the service never queues a patch that could never be split successfully.","triggerScenarios":"service.set(field, value) or service.patch({field: value}) where the UTF-8 byte length of the JSON-encoded value (plus field name, envelope, escaping of <>&/U+2028/U+2029) exceeds 262144 bytes.","commonSituations":"Persisting large documents, base64 images, big search results or serialized JSON blobs as a single view state field; accidental storage of a whole file's content instead of a reference/ID; accumulating an array in one field until it crosses the limit.","solutions":["Reduce the value size: store an ID or asset path instead of the full content","Split large payloads across multiple fields (chunking) or persist them server-side and keep only the reference in view state","Compress or trim the data before storing (e.g. store only the visible window of a list)","Use a different persistence mechanism (e.g. a document attribute or server-side storage API) for oversized data"],"exampleFix":"// before\nservice.set(\"searchResult\", JSON.stringify(entireResultList)); // > 256 KiB\n// after: store a trimmed window or reference\nservice.set(\"searchResultIds\", ids.slice(0, 500));\nservice.set(\"searchQuery\", query);","handlingStrategy":"validation","validationCode":"const MAX_PATCH_BYTES = 256 * 1024;\nconst valueBytes = (field, value) =>\n  new TextEncoder().encode(JSON.stringify({[field]: value})).byteLength;\nif (valueBytes(\"myField\", myValue) > MAX_PATCH_BYTES) throw new Error(\"value too large for view state\");","typeGuard":"const isStorableValue = (field: string, value: unknown): boolean =>\n  new TextEncoder().encode(JSON.stringify({[field]: value})).byteLength <= 256 * 1024;","tryCatchPattern":"try {\n  service.set(\"data\", bigValue);\n} catch (e) {\n  if (e.message === \"View state value is too large\") {\n    service.set(\"dataRef\", await persistExternally(bigValue));\n  }\n}","preventionTips":["Store references/IDs instead of large content blobs","Chunk arrays or documents across multiple fields","Cap list sizes persisted to view state","Add a size assertion in dev builds before calling set/patch"],"tags":["storage","limit-exceeded","payload-size"],"backgroundTag":"payload-too-large","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"}