Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The state data of Vuex may be reset when using JSX, if there is a circular dependency #103

Open
youthug opened this issue May 30, 2024 · 2 comments

Comments

@youthug
Copy link

youthug commented May 30, 2024

Describe the bug

In the development environment of our project with Vite + Vue2.7 + Vuex + Vue Router, we encountered an issue where there is a circular reference between src/store/modules/user.js and src/router/index.js. Modifying any JSX code seems to cause the state of src/store referenced elsewhere (src/utils/permission.js) to be reset after HMR. We have to refresh the page, which is very bad for the development experience.

image

Reproduction

https://github.com/youthug/vite-vuex-state-repro.git

Steps to reproduce

  1.  git clone https://github.com/youthug/vite-vuex-state-repro.git
     cd vite-vuex-state-repro
     yarn
     yarn dev
  2. Editing any code in each file in the src/components folder to see the changes on the page.

System Info

  System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 363.20 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.19.1 - ~/.nvm/versions/node/v16.19.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.19.1/bin/yarn
    npm: 8.19.3 - ~/.nvm/versions/node/v16.19.1/bin/npm
    pnpm: 7.29.0 - ~/.nvm/versions/node/v16.19.1/bin/pnpm
  Browsers:
    Chrome: 125.0.6422.142
    Safari: 17.4.1
  npmPackages:
    @vitejs/plugin-vue2: ^2.3.1 => 2.3.1
    vite: ^4.5.3 => 4.5.3

Used Package Manager

yarn

@youthug youthug closed this as completed Jun 12, 2024
@youthug
Copy link
Author

youthug commented Jun 12, 2024

moved: vitejs/vite#17450

@youthug youthug reopened this Aug 22, 2024
@youthug youthug changed the title When using JSX and there is a circular dependency, the component state will be reset The state data of Vuex may be reset when using JSX, if there is a circular dependency Dec 25, 2024
@youthug
Copy link
Author

youthug commented Dec 25, 2024

I found a simpler way to fix this. Add the following code in src/store/index.js. Not sure if it will have any unexcepted effects.

if (import.meta.hot) { // for tree-shaking
    import.meta.hot.accept(
        [
            './modules/user.js',
        ],
        () => {
            // Not handling hot updates;
            // the purpose is to resolve the issue where the state is reset
            // after Vite HMR due to circular dependencies
            window.location.reload()
        },
    )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant