Chrome拡張データ同期戦略 ― storage.sync・Google Drive・Firebase比較
概要
Chrome拡張でマルチデバイス同期を実装する場合、chrome.storage.sync から始めるのが最適解である。OAuth審査不要で即公開可能、ユーザー設定ゼロで動作し、100KBの容量制限はメタデータ同期には十分である。本記事ではブックマーク管理拡張「petapeta」を例に、chrome.storage.sync、Google Drive API、Firebase Firestoreの3方式を比較し、段階的拡張パスを含めた設計判断を解説する。
課題
ブックマーク管理拡張で複数PC間のデータ同期を実装する際、以下の選択肢がある。
chrome.storage.sync/chrome.storage.local- Google Drive API(appDataFolder)
- Firebase Firestore / Supabase
それぞれ容量制限・審査要件・モバイル対応・実装コストが異なり、プロダクトのフェーズに応じた適切な選択が求められる。
解決策
方式比較
| 方式 | 容量 | 審査 | モバイル | 実装コスト |
|---|---|---|---|---|
chrome.storage.sync | 100KB | 不要 | 不可 | 低 |
chrome.storage.local | 無制限* | 不要 | 不可 | 低 |
| Google Drive API | 無制限 | 必要(数週間〜数ヶ月) | 可能 | 高 |
| Firebase Firestore | 1GB+ | 不要 | 可能 | 中 |
| Supabase | 500MB | 不要 | 可能 | 中 |
*unlimitedStorage permission使用時
chrome.storage.sync の実装
// manifest.json の permissions に "storage" を追加
// 保存
async function saveBookmarks(data: BookmarkData): Promise<void> {
await chrome.storage.sync.set({
bookmarks: data,
settings: { theme: 'dark', viewMode: 'grid' },
});
}
// 取得
async function loadBookmarks(): Promise<BookmarkData | null> {
const result = await chrome.storage.sync.get(['bookmarks', 'settings']);
return result.bookmarks ?? null;
}
// 変更監視(他デバイスからの同期を検出)
chrome.storage.onChanged.addListener(
(changes: Record<string, chrome.storage.StorageChange>, namespace: string) => {
if (namespace === 'sync' && changes.bookmarks) {
console.log('Bookmarks synced from another device');
renderBookmarks(changes.bookmarks.newValue);
}
}
);
chrome.storage.sync の制限事項
// 容量チェックユーティリティ
async function checkStorageUsage(): Promise<{
bytesUsed: number;
bytesTotal: number;
percentUsed: number;
}> {
return new Promise((resolve) => {
chrome.storage.sync.getBytesInUse(null, (bytes) => {
resolve({
bytesUsed: bytes,
bytesTotal: 102_400, // QUOTA_BYTES: 100KB
percentUsed: (bytes / 102_400) * 100,
});
});
});
}
// 定数一覧
// QUOTA_BYTES: 102,400 (100KB total)
// QUOTA_BYTES_PER_ITEM: 8,192 (8KB per item)
// MAX_ITEMS: 512
// MAX_WRITE_OPERATIONS_PER_HOUR: 1,800
// MAX_WRITE_OPERATIONS_PER_MINUTE: 120
Google Drive API(appDataFolder)の実装
OAuth審査が必要で、審査前は「テストユーザー」100人までの制限がある。
// manifest.json
// {
// "oauth2": {
// "client_id": "YOUR_CLIENT_ID.apps.googleusercontent.com",
// "scopes": ["https://www.googleapis.com/auth/drive.appdata"]
// },
// "permissions": ["identity"]
// }
async function getAuthToken(): Promise<string> {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({ interactive: true }, (token) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(token!);
}
});
});
}
async function listAppDataFiles(token: string): Promise<DriveFile[]> {
const response = await fetch(
'https://www.googleapis.com/drive/v3/files?spaces=appDataFolder',
{ headers: { Authorization: `Bearer ${token}` } }
);
const data = await response.json();
return data.files;
}
Firebase Firestoreの実装
import { initializeApp } from 'firebase/app';
import { getFirestore, doc, setDoc, onSnapshot, serverTimestamp } from 'firebase/firestore';
import { getAuth, signInWithPopup, GoogleAuthProvider } from 'firebase/auth';
const app = initializeApp({
apiKey: '...',
authDomain: '...',
projectId: '...',
});
const db = getFirestore(app);
const auth = getAuth(app);
// 保存
async function syncBookmarks(userId: string, data: BookmarkData): Promise<void> {
await setDoc(doc(db, 'users', userId), {
bookmarks: data,
updatedAt: serverTimestamp(),
});
}
// リアルタイム同期
function subscribeToChanges(userId: string, callback: (data: BookmarkData) => void): void {
onSnapshot(doc(db, 'users', userId), (snapshot) => {
if (snapshot.exists()) {
callback(snapshot.data().bookmarks);
}
});
}
採用アーキテクチャ
chrome.storage.sync + chrome.storage.local のハイブリッド構成を採用する。
┌────────────────────────────────────────┐
│ chrome.storage.sync │
│ ・bookmarks (URL, title, tags) │
│ ・settings (theme, view mode) │
│ ・tagColors │
│ ↕ 自動同期 │
└────────────────────────────────────────┘
┌────────────────────────────────────────┐
│ chrome.storage.local │
│ ・articles (Readability content) │
│ ・imageCache (Base64) │
│ ローカルのみ │
└────────────────────────────────────────┘
容量設計
// ブックマーク1件あたりの概算サイズ
interface BookmarkSizeEstimate {
url: number; // ~100 bytes (平均)
title: number; // ~50 bytes
tags: number; // ~30 bytes
metadata: number; // ~20 bytes (color, dates)
}
// 合計: 約200 bytes/件
// 100KB ÷ 200 bytes = 500件が理論上限
// 実運用では300〜400件を目安に警告を表示
async function checkCapacityWarning(): Promise<boolean> {
const { bookmarks } = await chrome.storage.sync.get(['bookmarks']);
const count = bookmarks?.lists?.reduce(
(sum: number, list: { cards: unknown[] }) => sum + list.cards.length, 0
) ?? 0;
return count > 350; // 350件超で警告
}
段階的拡張パス
Phase 1(初期リリース)
├── chrome.storage.sync: メタデータ同期
└── chrome.storage.local: 本文・画像キャッシュ
Phase 2(スマホ対応時)
├── Firebase Auth: 認証
├── Firestore: データ同期
└── PWA: モバイルアクセス
Phase 3(大容量対応時)
├── Google Drive: 本文・画像保存
└── 差分同期: 効率化
Before/After表
| 項目 | Before(検討前) | After(採用構成) |
|---|---|---|
| 同期方式 | 未定 | chrome.storage.sync + local |
| 審査要件 | Google Drive検討中 | 不要(即リリース可能) |
| ユーザー設定 | OAuth認証フロー必要 | 設定ゼロ(Chromeログインのみ) |
| 容量 | 無制限を検討 | sync: 100KB(~500件)/ local: 無制限 |
| モバイル対応 | 未対応 | Phase 2でFirebase追加予定 |
| 実装コスト | 高(Drive API) | 低(storage API) |
まとめ
Chrome拡張のデータ同期は chrome.storage.sync から始めるのが最適である。OAuth審査不要で即リリース可能、ユーザー設定ゼロで動作し、Chromeにログインしているだけで複数PC間の同期が実現する。100KBの容量制限はブックマークのメタデータ(URL・タイトル・タグ)であれば約500件まで対応でき、本文や画像は chrome.storage.local に分離する設計とする。将来のモバイル対応やl大容量対応はFirebase/Google Driveで段階的に拡張可能であり、初期フェーズで過剰な設計をする必要はない。
この記事は、開発の記録をもとに Claude Code が下書きし、私が確認して公開しています。