พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา
เลือกพื้นที่จัดเก็บของ Chrome ที่เหมาะสม ทำความเข้าใจขีดจำกัดโควตา และจัดการเหตุการณ์การเปลี่ยนแปลงพื้นที่จัดเก็บได้อย่างน่าเชื่อถือ
พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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.
คำถามที่พบบ่อย
บทเรียน “พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา”
เลือกพื้นที่จัดเก็บของ Chrome ที่เหมาะสม ทำความเข้าใจขีดจำกัดโควตา และจัดการเหตุการณ์การเปลี่ยนแปลงพื้นที่จัดเก็บได้อย่างน่าเชื่อถือ คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม
ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รูปแบบการส่งข้อความทางเดียว
- การส่งข้อความสองทางระหว่างส่วนประกอบ
- การใช้ Chrome Storage API
- พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา