Skip to content

Commit

Permalink
storage: Use local, not sync
Browse files Browse the repository at this point in the history
We were running out of sync storage space in FireFox at about 8 bookmarks. Turns out the max available sync storage is 102 kb.

The max for local is 5 MB. Let's use that instead, even though we lose the sync advantage.

We'll keep the color mode in sync storage, since that doesn't take up much space.

Closes #30
  • Loading branch information
masonmcelvain committed Aug 29, 2022
1 parent 2593d05 commit 617cb24
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
84 changes: 48 additions & 36 deletions src/contexts/Links/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,61 @@ export const LinksProvider = ({

const fetchLegacyLinks = async () => await getLegacyLinks();

browser.storage.sync
.get([StorageKey.LINK_STORAGE_KEYS, StorageKey.NEXT_LINK_ID])
.then((result) => {
const nextLinkId = result[StorageKey.NEXT_LINK_ID];
const storedLinkKeys = result[StorageKey.LINK_STORAGE_KEYS];
type StorageArea =
| browser.Storage.Static["sync"]
| browser.Storage.Static["local"];
const getFromStorage = async (storageArea: StorageArea) => {
storageArea
.get([StorageKey.LINK_STORAGE_KEYS, StorageKey.NEXT_LINK_ID])
.then((result) => {
const nextLinkId = result[StorageKey.NEXT_LINK_ID];
const storedLinkKeys = result[StorageKey.LINK_STORAGE_KEYS];

if (nextLinkId && storedLinkKeys && storedLinkKeys.length > 0) {
payload.nextLinkId = nextLinkId;
payload.linkKeys = storedLinkKeys;
browser.storage.sync.get(storedLinkKeys).then((result) => {
const storedLinks = storedLinkKeys.map(
(key: string) => result[key] as LinkData
);
payload.links = storedLinks;

dispatch({
type: LinkAction.SET_STATE_FROM_STORAGE,
payload,
});
});
} else if (nextLinkId) {
// Migrate legacy links to new storage format
fetchLegacyLinks().then((legacyLinks) => {
if (legacyLinks && legacyLinks.length > 0) {
payload.nextLinkId = nextLinkId;
payload.links = legacyLinks;
payload.linkKeys = legacyLinks.map((link) =>
getStorageKeyForLink(link)
if (nextLinkId && storedLinkKeys && storedLinkKeys.length > 0) {
payload.nextLinkId = nextLinkId;
payload.linkKeys = storedLinkKeys;
storageArea.get(storedLinkKeys).then((result) => {
const storedLinks = storedLinkKeys.map(
(key: string) => result[key] as LinkData
);
payload.links = storedLinks;

setStoredLinksAndKeys(legacyLinks, payload.linkKeys);
dispatch({
type: LinkAction.SET_STATE_FROM_STORAGE,
payload,
});
} else {
cleanseStoredState();
}
});
} else {
cleanseStoredState();
}
});
return true;
});
} else if (nextLinkId) {
// Migrate legacy links to new storage format
fetchLegacyLinks().then((legacyLinks) => {
if (legacyLinks && legacyLinks.length > 0) {
payload.nextLinkId = nextLinkId;
payload.links = legacyLinks;
payload.linkKeys = legacyLinks.map((link) =>
getStorageKeyForLink(link)
);

setStoredLinksAndKeys(legacyLinks, payload.linkKeys);
dispatch({
type: LinkAction.SET_STATE_FROM_STORAGE,
payload,
});
return true;
} else {
cleanseStoredState();
}
});
} else {
cleanseStoredState();
}
return false;
});
};

if (!getFromStorage(browser.storage.local)) {
getFromStorage(browser.storage.sync);
}
}, []);

React.useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/webextension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function getLinkIdForStorageKey(key: string): number {
function setStorageWithKey(key: string, value: any) {
const storageObj: Record<string, any> = {};
storageObj[key] = value;
browser.storage.sync.set(storageObj);
browser.storage.local.set(storageObj);
}

/**
Expand Down

0 comments on commit 617cb24

Please sign in to comment.