Skip to content

Commit

Permalink
fix(token-lists): remove old token lists from cache (#5275)
Browse files Browse the repository at this point in the history
* fix(token-lists): remove old token lists from localStorage

* fix: exit early if localStorage key is not set
  • Loading branch information
alfetopito authored Jan 9, 2025
1 parent 1b4cfc1 commit 1ca7211
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions apps/cowswap-frontend/public/emergency.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,51 @@ if (window.location.pathname !== '/') {
window.location.pathname = '/'
}

/**
* Removes deprecated token lists for a particular localStorage key: `allTokenListsInfoAtom:v5`
*
* A change introduced new token lists and removed old ones.
* This code removes the old token lists from the local storage to avoid duplication without resetting user added token lists.
*/
;(function () {
const key = 'allTokenListsInfoAtom:v5'
const storageValue = localStorage.getItem(key)

// Exit early if the storage value is not set
if (!storageValue) return

const tokenLists = JSON.parse(storageValue)

const listsToSkip = new RegExp(
'CoingeckoTokensList\\.json$|' +
'UniswapTokensList\\.json$|' +
'CoinGecko\\.json$|' +
'compound\\.tokenlist\\.json$|' +
'set\\.tokenlist\\.json$|' +
'tokensoft\\.eth$|' +
'opyn-squeeth|' +
'tryroll\\.com|' +
'snx\\.eth$|' +
'aave\\.eth$|' +
'cmc\\.eth$',
)

const updatedTokenLists = Object.keys(tokenLists).reduce((acc, chainId) => {
acc[chainId] = Object.keys(tokenLists[chainId]).reduce((_acc, listPath) => {
if (!listsToSkip.test(listPath)) {
_acc[listPath] = tokenLists[chainId][listPath]
} else {
console.log('[Service worker] Skip token list', listPath)
}
return _acc
}, {})

return acc
}, {})

localStorage.setItem(key, JSON.stringify(updatedTokenLists))
})()

/**
* Remove old versions of the local storage atom stores
* We rely on the fact that store names are in the format {name}Atom:v{version}
Expand Down

0 comments on commit 1ca7211

Please sign in to comment.