siyuan-note/siyuan · error
view state patch is too large
Error message
view state patch is too large
What it means
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.
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
Example fix
// before
PatchViewState(key, map[string]any{"snapshot": hugeBase64String})
// after
PatchViewState(key, map[string]any{"snapshotRef": assetPath}) // store data as an asset, patch holds the reference Defensive patterns
Strategy: validation
Validate before calling
func patchTooLarge(values map[string]any) bool {
data, err := json.Marshal(map[string]any{"values": values})
return err == nil && len(data) > 256*1024
} Try / catch
ret, err := PatchViewState(key, values, removeKeys)
if err != nil && err.Error() == "view state patch is too large" {
// split or trim the payload and retry
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.
Related errors
- view state patch contains too many entries
- attribute view custom colors count exceeds the
- createDocTree document title exceeds
- createDocTree exceeds the maximum document count of
- Failed to view state
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/a978268805c48e30.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/model/view_state.go:105
return nil, errors.New("view state patch contains too many entries")
}
for valueKey := range values {
if err = validateViewStateDataKey(valueKey); err != nil {
return nil, err
}
}
for _, removeKey := range removeKeys {
if err = validateViewStateDataKey(removeKey); err != nil {
return nil, err
}
}
patch := &viewStatePatch{Values: values, RemoveKeys: removeKeys}
patchData, marshalErr := gulu.JSON.MarshalJSON(patch)
if nil != marshalErr {
return nil, marshalErr
}
if maxViewStatePatchBytes < len(patchData) {
return nil, errors.New("view state patch is too large")
}
// 后续合并使用序列化快照,避免调用方在请求执行期间继续修改嵌套值。
normalizedPatch := &viewStatePatch{}
if err = gulu.JSON.UnmarshalJSON(patchData, &normalizedPatch); nil != err {
return nil, err
}
values = normalizedPatch.Values
removeKeys = normalizedPatch.RemoveKeys
viewStateStorageLock.Lock()
defer viewStateStorageLock.Unlock()
storage, err := getViewStateStorage()
if err != nil {
return nil, err
}
state := storage.Views[key]
if nil == state {View on GitHub (pinned to 9f775e8a12)