nuxt3: After the browser refreshes, the middleware gets lost in localStorage
Ena-Heleneto opened this issue · 2 comments
Ena-Heleneto commented
Describe the bug
"nuxt": "^3.6.3",
"pinia": "^2.1.4",
"@pinia-plugin-persistedstate/nuxt": "^1.1.1",
system:window
piniaPersistedstateOption in nuxt.config.ts
piniaPersistedstate: {
cookieOptions: { sameSite: 'strict' },
storage: 'localStorage',
debug: true,
},
the pinia composables
import { acceptHMRUpdate, defineStore } from 'pinia'
export const userStore = defineStore('users', () => {
/**
* @description: 用户昵称
*/
const userName = ref<string>('')
/**
* @description: 用户头像
*/
const userAvatar = ref<string>('')
/**
* @description: 用户token
*/
const userToken = ref<string>('')
return { userName, userAvatar, userToken }
}, { persist: true })
if (import.meta.hot)
import.meta.hot.accept(acceptHMRUpdate(userStore, import.meta.hot))
the Problem code in middleware/effective.global.ts
export default defineNuxtRouteMiddleware((to) => {
consola.info([userStore().userToken, userStore().userName, userStore().userAvatar])
if (!(userStore().userToken) && to.path !== '/login')
return navigateTo('/login')
})
Details of the issue
When the browser is not refreshed for routing jumps, middleware can get the data to be localStorage, but when I refresh the browser, middleware cannot get the data, I think it is a problem of data persistence
Reproduction
System Info
system:Windows
browswers:google chrome
Core dependencies:
"nuxt": "^3.6.3",
"pinia": "^2.1.4",
"@pinia-plugin-persistedstate/nuxt": "^1.1.1",
Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guide.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- The provided reproduction is a minimal reproducible of the bug.
MZ-Dlovely commented
maybe you're finding this one #221
prazdevs commented
Ye, this is pretty much a duplicate. If app is already up and running, persistence is set up and middleware will trigger it. While on initial render, it probably isnt for some reason. Gonna close this one specific issue as duplicate of #221. and eventually investigate in middleware usage if something is doable !