Skip to content

Pinia state is undefined in action with production build #537

@annacollins85

Description

@annacollins85

We are using Vue 3 version 3.0.7 with Pinia version 2.0.0-beta.2. It works fine when we run the app locally with vue-cli-service serve, but not when run in production mode with vue-cli-service build --mode production . We suspect it could have something to do with what bundler to use for Pinia in production, as we see some code in the library that relates to this

Our store (We are using a wrapper which we call with the required params and export from another file):

import { defineStore } from 'pinia'
import { Router } from 'vue-router'
import { AuthenticationModule } from '..'
import { authApiFactory, User, TokenInformationDTO } from '../api'
import { ApiState } from '../../common/store/api-state'
export const useAuthStoreFactory = (authApi: ReturnType<typeof authApiFactory>, router: Router) => {
  return defineStore({
    id: AuthenticationModule.name,
    state: () => ({
      tokenInformation: {} as TokenInformationDTO,
      authApiState: {} as ApiState
    }),
    actions: {
      async login(user: User) {
        const tokenInformation: TokenInformationDTO = await authApi.login(user)
        this.tokenInformation = tokenInformation
      },
      async checkSupervisorPassword(password: string) {
        await authApi.checkSupervisorPassword(password, this.tokenInformation.accessToken) // this.tokenInformation is null
        router.push('/')
      }
    }
  })
}

vue.config.js:

const path = require('path')
const config = require('./config')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { DefinePlugin } = require('webpack')
const IS_PRODUCTION = process.env.VUE_APP_MODE === 'production'
module.exports = {
  publicPath: process.env.PUBLIC_PATH || '/',
  lintOnSave: true,
  devServer: {
    port: 3000
  },
  configureWebpack: {
    devtool: IS_PRODUCTION ? 'hidden-source-map' : 'source-map',
    resolve: {
      extensions: ['.js', '.vue', '.json', '.ts'],
      alias: {
        vue: "vue/dist/vue.esm-bundler.js"
      }
    },
    plugins: [
        // ...some HtmlWebpackPlugins,
        new DefinePlugin({
          "__VUE_OPTIONS_API__": false,
          "__VUE_PROD_DEVTOOLS__": false,
          "__INTLIFY_PROD_DEVTOOLS__": false
        })
    ]
  }
}

Steps to reproduce the behavior

When the login action is called, the correct tokenInformation is received and set in the state, but on checkSupervisorPassword, the state is undefined. The checkSupervisorPassword action is only ever called after the login action so the tokenInformation should be available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    need reproThis issue couldn't be reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions