{"record":{"id":"a978268805c48e30","repo":"siyuan-note/siyuan","slug":"view-state-patch-is-too-large","errorCode":null,"errorMessage":"view state patch is too large","messagePattern":"view state patch is too large","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/view_state.go","lineNumber":105,"sourceCode":"\t\treturn nil, errors.New(\"view state patch contains too many entries\")\n\t}\n\tfor valueKey := range values {\n\t\tif err = validateViewStateDataKey(valueKey); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\tfor _, removeKey := range removeKeys {\n\t\tif err = validateViewStateDataKey(removeKey); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\tpatch := &viewStatePatch{Values: values, RemoveKeys: removeKeys}\n\tpatchData, marshalErr := gulu.JSON.MarshalJSON(patch)\n\tif nil != marshalErr {\n\t\treturn nil, marshalErr\n\t}\n\tif maxViewStatePatchBytes < len(patchData) {\n\t\treturn nil, errors.New(\"view state patch is too large\")\n\t}\n\t// 后续合并使用序列化快照，避免调用方在请求执行期间继续修改嵌套值。\n\tnormalizedPatch := &viewStatePatch{}\n\tif err = gulu.JSON.UnmarshalJSON(patchData, &normalizedPatch); nil != err {\n\t\treturn nil, err\n\t}\n\tvalues = normalizedPatch.Values\n\tremoveKeys = normalizedPatch.RemoveKeys\n\n\tviewStateStorageLock.Lock()\n\tdefer viewStateStorageLock.Unlock()\n\n\tstorage, err := getViewStateStorage()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tstate := storage.Views[key]\n\tif nil == state {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/view_state.go#L87-L123","documentation":"PatchViewState serializes the patch to JSON and rejects it if the serialized size exceeds maxViewStatePatchBytes (256 KB). This bounds the size of a single mutation to the shared view-state storage file.","triggerScenarios":"Calling PatchViewState with values whose JSON encoding exceeds 256 KB — e.g. embedding large blobs, long strings, or deeply nested payloads in view state values.","commonSituations":"Storing base64 image/file data or large cached responses in view state instead of assets; a single field holding a huge JSON document accumulated over time; accidental inclusion of an entire document model in the patch.","solutions":["Shrink the patch: move large data to assets or separate storage and keep only small scalar/small-JSON values in view state","Split the payload across multiple PatchViewState calls, each under 256 KB after serialization","Trim or truncate large string values before patching"],"exampleFix":"// before\nPatchViewState(key, map[string]any{\"snapshot\": hugeBase64String})\n// after\nPatchViewState(key, map[string]any{\"snapshotRef\": assetPath}) // store data as an asset, patch holds the reference","handlingStrategy":"validation","validationCode":"func patchTooLarge(values map[string]any) bool {\n    data, err := json.Marshal(map[string]any{\"values\": values})\n    return err == nil && len(data) > 256*1024\n}","typeGuard":null,"tryCatchPattern":"ret, err := PatchViewState(key, values, removeKeys)\nif err != nil && err.Error() == \"view state patch is too large\" {\n    // split or trim the payload and retry\n}","preventionTips":["Keep view state values small; store large data as assets and keep references","Estimate JSON size before patching large payloads","Audit for accidental inclusion of base64 blobs or whole documents in state"],"tags":["view-state","limit","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"}