useWebExtensionStorage composable provides a reactive interface to the Chrome Storage API, making it easy to persist and sync user settings.
Overview
This composable wraps thechrome.storage.local API with Vue’s reactivity system. Any changes to the returned data ref automatically save to storage, and changes from other tabs or extension contexts are synced back.
Usage Example
Fromsrc/newtab/NewTab.vue:16-20:
API Reference
Function Signature
Parameters
Storage key used in
chrome.storage.local. Must be unique per stored value.Default value when no stored data exists. Can be a plain value, ref, or getter function.
Configuration options for storage behavior
Options
When
true, merges stored values with initialValue using object spread. Useful for adding new properties to existing configs.Can also be a custom merge function: (storedValue: T, defaults: T) => TWrites
initialValue to storage if no value existsEnables deep watching of nested object properties
Controls when the watcher callback runs relative to component rendering
Uses
shallowRef instead of ref for performance when deep reactivity isn’t neededListens for changes from other extension contexts and syncs them to
dataCustom serializer for storage read/write. Defaults to auto-detected serializer based on
initialValue type.Error handler for storage operations. Defaults to
console.errorReturn Value
Reactive reference to the stored value. Assign to this ref to update storage.Setting to
null removes the item from storage.Promise that resolves when the initial storage read completes. Always await this before using
data.value.How It Works
Storage Interface
The composable implements aStorageLikeAsync interface (lines 31-44) that wraps the Chrome Storage API:
Serialization
Values are automatically serialized based on type (lines 11-29):- Object
- Array
- Set
- Map
- Date
- Primitives
JSON serialization for plain objects
Merge Behavior
WhenmergeDefaults: true, the composable merges stored data with defaults (lines 79-85):
initialValue are added to existing stored configs.
Reactivity
A Vue watcher (lines 113-117) automatically saves changes:Cross-Context Sync
The composable listens to storage changes from other contexts (lines 119-140):Real-World Example
Updating a single property inNewTab.vue:110:
- The watch callback runs
- The new value is serialized
chrome.storage.local.set()is called- Other open tabs receive the change via
onChangedlistener
Always await
dataReady before reading data.value to ensure the initial storage read has completed.