[SSR] LocalStorage.getItem is not a function



  • Please help,
    I switched to SSR mode for an existing project but had the same error:
    [view-router] uncaught error during route navigation:

    TypeError: quasar__WEBPACK_IMPORTED_MODULE_2 __. LocalStorage.getItem is not a function
    how can I fix that? I used LocalStorage.getItem in several places of my code.
    

    quasar v1

    You can help me please adapt SSR mode in my boot:

    import { LocalStorage } from 'quasar'
    import { onFail } from '../session/logout'
    import appSetup from './app-setup'
    import config from '../config'
    
    var authorized = false
    
    export default () => {
      if (config.localStorage.enableListener) {
        window.addEventListener('storage', (e) => {
          if (!authorized) {
            console.warn('Unauthorized local storage change')
            switch (config.localStorage.unauthChange) {
              case 'block':
                if (e.key === 'null' || e.key === null) {
                  reload()
                } else {
                  _LocalStorage.setNative(e.key, e.oldValue)
                }
                break
              case 'clear':
                reload()
                break
              default:
                reload()
                break
            }
          }
        }, false)
      }
      return Promise.resolve(true)
    }
    
    const reload = () => {
      onFail().then(success => appSetup())
    }
    
    export const _LocalStorage = {
      setNative (key, value) {
        authorized = true
        localStorage.setItem(key, value)
        authorized = false
      },
      set (key, value) {
        authorized = true
        LocalStorage.set(key, value)
        authorized = false
      },
      remove (key) {
        authorized = true
        LocalStorage.remove(key)
        authorized = false
      },
      clear () {
        authorized = true
        LocalStorage.clear()
        authorized = false
      },
      get (key) {
        return LocalStorage.getItem(key)
      }
    }
    

Log in to reply