Skip to content

data-handler

A module for handling data loading and saving to the plugin’s data file.

PluginDataHandler bounds the plugin’s data.json in bytes, in both directions, because Obsidian does not. Read from Obsidian 1.15.1: Plugin.loadData reads the whole file and JSON.parses it with no limit, so an oversized file is fully read and parsed into memory on every startup, and Plugin.saveData writes JSON.stringify(data, undefined, 2) with no limit either. Obsidian also swallows a failed read or write: its readJson logs a parse failure and resolves undefined, and its writeJson catches every error. So the guard has to decide BEFORE handing the call to Obsidian, which is the only point where refusing still changes the outcome.

The same readJson makes a data.json that EXISTS but does not parse indistinguishable from no file at all: both resolve undefined. A settings component reading that as “nothing stored” saves its defaults over the file, and the user’s settings are gone. So when Obsidian answers undefined or null, PluginDataHandler.loadData reads the file raw and parses it itself; a file that does not parse is copied into the device-local SettingsRecoveryStore and reported as a PluginDataParseError, never as missing.

Obsidian workaround load-data-hides-parse-error, seen in Obsidian 1.15.1: its canary is in data-handler.obsidian.integration.test.ts.

Class Description
PluginDataHandler A handler for loading and saving data to the plugin’s data file, refusing a data.json larger than a byte limit.
PluginDataParseError Thrown when a plugin’s data.json exists but is not valid JSON. The file is never touched when this is thrown. When recoveryCopy is not null, a copy of the file’s raw text was kept on this device first, so even a later save over the file loses nothing.
PluginDataSizeLimitExceededError Thrown when a plugin’s data.json is, or would become, larger than the configured limit. The file on disk is never touched when this is thrown: an oversized existing file is left intact for the user to inspect or trim, and an oversized save is refused before anything is written, so the last good file remains.
Interface Description
DataHandler A handler for loading and saving data to the plugin’s data file.
PluginDataHandlerConstructorOptions Options for PluginDataHandler.
PluginDataParseErrorConstructorParams Parameters for PluginDataParseError.
PluginDataSizeLimitExceededErrorConstructorParams Parameters for PluginDataSizeLimitExceededError.
Enum Description
PluginDataOperation The operation a PluginDataSizeLimitExceededError refused.
Variable Description
DEFAULT_PLUGIN_DATA_MAX_SIZE_IN_BYTES The default maximum size of a plugin’s data.json, in bytes: 16 MiB. This is a protective budget against startup exhaustion, not a sync quota. Obsidian Sync’s own per-file limit depends on the plan and is a separate constraint a plugin may want to stay well under.