Preferences

Preferences: Simple Key-Value Storage

Preferences stores strings under string keys, in SharedPreferences on Android, UserDefaults on iOS and localStorage on the web. The Capacitor 243,123 storage guide explains why it exists: the OS may reclaim a WebView's localStorage when the device runs low on space, and Preferences, being native, avoids that:

Saving the last book and the cart in src/js/lab.jsJavaScript
export async function prefs() {
  await Preferences.set({ key: 'lastBookId', value: '3' });
  await Preferences.set({ key: 'cart', value: JSON.stringify([1, 4]) }); // strings only
  const { value } = await Preferences.get({ key: 'cart' });
  const { keys } = await Preferences.keys();
  return show('prefs', { cart: JSON.parse(value), keys });
}
Output
LAB prefs {"cart":[1,4],"keys":["lastBookId","cart"]}

The values sit in an XML file named after the default group, CapacitorStorage:

Where Preferences keeps its data on AndroidShell
adb -s emulator-5558 shell run-as com.example.booknest cat shared_prefs/CapacitorStorage.xml
Output
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="lastBookId">3</string>
    <string name="cart">[1,4]</string>
</map>

That is plain text, so keep tokens and passwords out of it (Secure Storage), and keep records in SQLite 4,756 : Preferences is meant for small amounts of data. configure({ group: 'NativeStorage' }) reads data left by the Cordova 129 plugin cordova-plugin-nativestorage (Migrating from Cordova).