try to clean the old persistented data when storage full #2851
MergedNewFuture(GitHub profile, opens in a new tab)merged 5 commits into
master fromrefs/pull/2851/headmerged by TkDodo View on GitHubTkDodo requested changes
OutdatedResolvedby TkDodo@@ -18,5 +18,20 @@1818 }: CreateWebStoragePersistorOptions): Persistor {19+ //try to save data to storage20+ function trySave(persistedClient: PersistedClient) {21+ try {22+ storage.setItem(key, JSON.stringify(persistedClient))23+ } catch {24+ return false25+ }26+ return true27+ }28+1929 if (typeof storage !== 'undefined') {2030 return {2131 persistClient: throttle(persistedClient => {22- storage.setItem(key, JSON.stringify(persistedClient))32+ if (trySave(persistedClient) !== true) {33+ const { mutations, queries } = persistedClient.clientState34+35+ // try to remove mutations and save36+ while (mutations.length > 0) {37+ mutations.unshift()OutdatedResolvedby TkDodo@@ -20,3 +30,20 @@2030 return {2131 persistClient: throttle(persistedClient => {22- storage.setItem(key, JSON.stringify(persistedClient))32+ if (trySave(persistedClient) !== true) {33+ const { mutations, queries } = persistedClient.clientState34+35+ // try to remove mutations and save36+ while (mutations.length > 0) {37+ mutations.unshift()38+ persistedClient.clientState.mutations = mutations39+ if (trySave(persistedClient)) {40+ return41+ }42+ }43+44+ // clean old queries and try save45+ const sortedQueries = queries.sort(46+ (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt47+ )48+ while (sortedQueries.length > 0) {49+ const oldestData = sortedQueries.shift()TkDodo reviewed
OutdatedResolvedby TkDodo@@ -19,4 +26,20 @@26+ return true27+ }28+1929 if (typeof storage !== 'undefined') {2030 return {2131 persistClient: throttle(persistedClient => {22- storage.setItem(key, JSON.stringify(persistedClient))32+ if (trySave(persistedClient) !== true) {33+ const mutations = [...persistedClient.clientState.mutations]34+ const queries = [...persistedClient.clientState.queries]35+ const client: PersistedClient = {36+ ...persistedClient,37+ clientState: { mutations, queries },38+ }39+40+ // clean mutations and try to save41+ while (mutations.shift()) {42+ if (trySave(client)) {43+ return // save success44+ }45+ }Resolvedby TkDodo@@ -22,0 +38,21 @@38+ }39+40+ // clean mutations and try to save41+ while (mutations.shift()) {42+ if (trySave(client)) {43+ return // save success44+ }45+ }46+47+ // sort queries by dataUpdatedAt (oldest first)48+ const sortedQueries = [...queries].sort(49+ (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt50+ )51+ // clean old queries and try to save52+ while (sortedQueries.length > 0) {53+ const oldestData = sortedQueries.shift()54+ client.clientState.queries = queries.filter(q => q !== oldestData)55+ if (trySave(client)) {56+ return // save success57+ }58+ }TkDodo approved these changes
TkDodo merged thisb52b277
TkDodo closed this
tannerlinsley added labelsreleased
Sign in to join this conversation.
This pull request is being automatically deployed with Vercel (learn more). To see the status of your deployment, click below or on the icon next to each commit.
🔍 Inspect: https://vercel.com/tanstack/react-query/9G6eshRnHBpbHWfzGhwPDqwW3A7e ✅ Preview: https://react-query-git-fork-newfuture-fix-storage-full-tanstack.vercel.app