fix: top level await (chrome 129) (#2970)

This commit is contained in:
Joaquín Sánchez 2024-09-23 12:53:10 +02:00 committed by GitHub
parent 7ab2f16f35
commit 10076be909
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 160 additions and 36 deletions

34
plugins/0.setup-users.ts Normal file
View file

@ -0,0 +1,34 @@
import { useAsyncIDBKeyval } from '~/composables/idb'
import type { UserLogin } from '~/types'
import { STORAGE_KEY_USERS } from '~/constants'
const mock = process.mock
export default defineNuxtPlugin({
parallel: import.meta.server,
async setup() {
const users = useUsers()
let defaultUsers = mock ? [mock.user] : []
// Backward compatibility with localStorage
let removeUsersOnLocalStorage = false
if (globalThis?.localStorage) {
const usersOnLocalStorageString = globalThis.localStorage.getItem(STORAGE_KEY_USERS)
if (usersOnLocalStorageString) {
defaultUsers = JSON.parse(usersOnLocalStorageString)
removeUsersOnLocalStorage = true
}
}
if (import.meta.server) {
users.value = defaultUsers
}
if (import.meta.client) {
await useAsyncIDBKeyval<UserLogin[]>(STORAGE_KEY_USERS, defaultUsers, { deep: true }, users)
}
if (removeUsersOnLocalStorage)
globalThis.localStorage.removeItem(STORAGE_KEY_USERS)
},
})