From 1ca7211729c552c1c834b12c2d343ce981b02cf1 Mon Sep 17 00:00:00 2001 From: Leandro Date: Thu, 9 Jan 2025 15:52:16 +0000 Subject: [PATCH] fix(token-lists): remove old token lists from cache (#5275) * fix(token-lists): remove old token lists from localStorage * fix: exit early if localStorage key is not set --- apps/cowswap-frontend/public/emergency.js | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/apps/cowswap-frontend/public/emergency.js b/apps/cowswap-frontend/public/emergency.js index 82101e40bd..4063cd523c 100644 --- a/apps/cowswap-frontend/public/emergency.js +++ b/apps/cowswap-frontend/public/emergency.js @@ -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}