Archiviazione sync o locale e quote
Scegliete l’area di archiviazione Chrome più adatta, comprendete i limiti delle quote e gestite in modo affidabile gli eventi di modifica dell’archiviazione.
Archiviazione sync o locale e quote è una lezione Browser Extensions Development (Chrome & Edge) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Browser Extensions Development (Chrome & Edge), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Browser Extensions Development (Chrome & Edge) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Two Main Storage Areas
The Chrome storage API offers several areas. The two you use most are storage.local and storage.sync.
- local — stays on this device, larger quota
- sync — syncs across the user's signed-in browsers, smaller quota
When to Use sync
Use storage.sync for user preferences you want to follow them everywhere, like theme or settings. Keep the data small.
chrome.storage.sync.set({ theme: 'dark', fontSize: 14 })When to Use local
Use storage.local for larger or device-specific data such as caches, logs, or downloaded content that does not need to sync.
chrome.storage.local.set({ cache: bigObject })Sync Quota Limits
storage.sync is intentionally small. Roughly: about 100 KB total, 8 KB per item, and a limited number of writes per minute.
Exceeding these throws errors, so plan your data shape.
// Stay well under ~8 KB per key for syncLocal Quota Limits
storage.local allows far more, commonly around 10 MB by default, and the unlimitedStorage permission can raise it. Still avoid storing huge blobs.
{
"permissions": ["storage", "unlimitedStorage"]
}Reading Data Back
Both areas read with get. Pass a key, an array of keys, or an object of defaults.
const data = await chrome.storage.sync.get({ theme: 'light' })
console.log(data.theme)Watching for Changes
The onChanged event fires whenever stored values change, even from another context. Use it to keep your UI in sync.
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'sync' && changes.theme) {
console.log('New theme: ' + changes.theme.newValue)
}
})Removing and Clearing
Delete specific keys with remove, or wipe an entire area with clear.
chrome.storage.local.remove('cache')
chrome.storage.sync.clear()Checking Bytes Used
Use getBytesInUse to see how close you are to the quota before writing more data.
const used = await chrome.storage.sync.getBytesInUse()
console.log(used + ' bytes used')Handling Write Failures
Sync writes can fail on quota errors. Wrap writes in try/catch and fall back to local storage when sync is full.
try {
await chrome.storage.sync.set({ big: value })
} catch (e) {
await chrome.storage.local.set({ big: value })
}Choosing Wisely
A common pattern: keep small settings in sync, keep everything else in local. Never store secrets like tokens in plain storage, since extension storage is not encrypted.
Quick Check
Test your storage knowledge.
Recap
You compared storage areas:
syncfor small cross-device settingslocalfor larger device data- Respect quotas and check
getBytesInUse - React to
onChanged - Handle write failures and avoid storing secrets
Picking the right area keeps your data fast, durable, and within limits.
Domande Frequenti
La lezione «Archiviazione sync o locale e quote» è gratuita?
Sì — il testo completo di «Archiviazione sync o locale e quote» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Browser Extensions Development (Chrome & Edge), passa a CoddyKit PRO. Il corso Browser Extensions Development (Chrome & Edge) include 4 lezioni in totale.
Cosa imparerò in «Archiviazione sync o locale e quote»?
Scegliete l’area di archiviazione Chrome più adatta, comprendete i limiti delle quote e gestite in modo affidabile gli eventi di modifica dell’archiviazione. Eserciti Browser Extensions Development (Chrome & Edge) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Browser Extensions Development (Chrome & Edge)?
Non è richiesta alcuna esperienza precedente. Browser Extensions Development (Chrome & Edge) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Archiviazione sync o locale e quote»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Browser Extensions Development (Chrome & Edge)?
Sì. Ogni lezione Browser Extensions Development (Chrome & Edge) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Modelli di messaggistica unidirezionale
- Messaggistica bidirezionale tra componenti
- Utilizzo dell'API Chrome Storage
- Archiviazione sync o locale e quote