Skip to content

Commit

Permalink
refactor(Storage)!: use localStorage instead of localForage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Jul 26, 2022
1 parent 5fb850a commit fef5a2b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 101 deletions.
17 changes: 1 addition & 16 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ export default {
{ src: '~/plugins/z.vue-matomo.js', mode: 'client' }
],

buildModules: [
// https://github.com/nuxt-community/localforage-module
'@nuxtjs/localforage'
],
buildModules: [],

tailwindcss: {
viewer: false,
Expand All @@ -106,11 +103,6 @@ export default {
cssPath: '~/assets/css/main.css'
},

localforage: {
name: 'Rule34App',
storeName: 'localForage'
},

modules: [
'@nuxtjs/pwa',
'@nuxtjs/axios',
Expand Down Expand Up @@ -266,13 +258,6 @@ export default {
// Matomo
'ao.sync',

// localForage
'Database',
'indexedDB',
'window.webkitStorageInfo',
'QuotaExceededError',
'Transaction timed out due to inactivity',

// Axios
'timeout of 0ms exceeded'
],
Expand Down
55 changes: 8 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"dependencies": {
"@nuxtjs/auth-next": "5.0.0-1648802546.c9880dc",
"@nuxtjs/axios": "^5.13.6",
"@nuxtjs/localforage": "^1.1.0",
"@nuxtjs/pwa": "^3.3.5",
"@nuxtjs/sentry": "^5.1.7",
"@nuxtjs/sitemap": "^2.4.0",
Expand Down
2 changes: 0 additions & 2 deletions pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ export default {
async removeLocalStorage() {
localStorage.clear()
await this.$localForage.clear()
location.reload()
},
},
Expand Down
54 changes: 19 additions & 35 deletions plugins/c.vuex-persist.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,72 @@
import VuexPersistence from 'vuex-persist'
import { VuexPersistence } from 'vuex-persist'

export default (context) => {
const {
store,
$localForage,
app: { router },
} = context
const { store } = context

// Booru state
new VuexPersistence({
key: 'vuex-booru',

storage: $localForage,
asyncStorage: true,
storage: window.localStorage,

reducer: (state) => ({
booru: {
history: state.booru.history,
},
}),
history: state.booru.history
}
})
}).plugin(store)

// Default state
new VuexPersistence({
key: 'vuex-root',

storage: $localForage,
asyncStorage: true,
storage: window.localStorage,

reducer: (state) => ({
version: state.version,

statistics: state.statistics,
}),
statistics: state.statistics
})
}).plugin(store)

// Notifications state
new VuexPersistence({
key: 'vuex-notifications',

storage: $localForage,
asyncStorage: true,
storage: window.localStorage,

reducer: (state) => ({
notifications: {
// Double because the state and module are named the same
notifications: {
latestTitle: state.notifications.notifications.latestTitle,
},
},
}),
latestTitle: state.notifications.notifications.latestTitle
}
}
})
}).plugin(store)

// User state
new VuexPersistence({
key: 'vuex-user',

storage: $localForage,
asyncStorage: true,
storage: window.localStorage,

reducer: (state) => {
const SETTINGS_OBJ = {}

// Recreate every setting's value path to save them to localStorage (this way we dont save other data like titles, defaultValue, etc.)
Object.keys(state.user.settings).forEach((key) => {
SETTINGS_OBJ[key] = {
value: state.user.settings[key].value,
value: state.user.settings[key].value
}
})

return {
user: {
custom: state.user.custom,
settings: SETTINGS_OBJ,
},
settings: SETTINGS_OBJ
}
}
},
}
}).plugin(store)

// Fix for async storage
const waitForStorageToBeReady = async (to, from, next) => {
await store.restored
next()
}

router.beforeEach(waitForStorageToBeReady)
}

0 comments on commit fef5a2b

Please sign in to comment.