0

The Problem

When I dispatch a function from a Pinia store that has a for loop inside that fills an array, the array ends up having double the size according to Chrome and the Vue Chrome extension.

Detailed explanation

I use Nuxt with Pinia. When I dispatch a function with Pinia where I fill an array with a for loop, it has double the size and entries in the Vue extension and in the Chrome log itself. Since Nuxt logs console.log directly in the terminal, I tried to log the size of the array and in the terminal it says 10 but in Chrome it says 20. That makes no sense. I've never see such a weird behavior before.

Heres the code and the logs

TestStore.ts

import { defineStore } from "pinia";

export const useTestStore = defineStore("testStore", {
    state: () => {
        return {
            test_array: [] as number[]
        };
    },
    actions: {
        test() {
            for(let i = 0; i < 10; i++) {
                this.test_array.push(i)
            }
            console.log(this.test_array.length);
        }
    }
})

app.vue

<script setup lang="ts">
import { useTestStore } from "@/stores/TestStore";
const teststore = useTestStore();
teststore.test();
</script>

Array in Chrome before the first loop:

[0,1,2,3,4,5,6,7,8,9]

Array in Chrome after the first loop:

[0,1,2,3,4,5,6,7,8,9,0]

Array after the for loop:

[0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]

console.log(this.test_array.length) in the Nuxt terminal: 10

console.log(this.test_array.length) in Chrome: 20

Chrome log: Chrome log

Edit: I realized every time I press CTRL+S and it hot reloads, it fills the array with another 10 digits. Log after 20 saves:

enter image description here

Edit 2: The issue with it filling it on every save disappeared for now. This whole bug is so confusing...

Nuxt terminal log: enter image description here

Edit 3: I kinda have an error now. When I took a closer look it appeared to me that the console log in the terminal was always quicker. So it would log what you can see above in the terminal and then the website would reload and show the array times two. So it seems like it already has data in that array even before the reload: enter image description here

MaxCodes
  • 82
  • 10
  • Regarding the HMR "issue", it's kinda okay I think. It's not the first time that we do have some issues regarding HMR, happens. Not a big deal usually if a hard refresh works to fix it. Regarding the values, if it's not related to a browser specifically (maybe works better on Firefox), it's probably a lifecycle gotcha or just the fact that the server is handling it a bit different from the client side. Wouldn't stress too much neither, maybe the way `console.log` works. The DOM mismatch is quite a big deal tho and deserves a fix. https://stackoverflow.com/a/67978474/8816585 – kissu Jul 18 '22 at 06:11
  • @kissu Thank you for your answer. The HMR issue is fine for now. It's solved with a hard refresh. I already tried Firefox but no change. Also this is beyond console.log. I printed out another array to the DOM and it looks the same. So [1,2,3,4,5] renders [1,2,3,4,5,1,2,3,4,5] in the DOM. Also Pinia shows the same in the Vue extension in Chrome. When you said lifecycle I though maybe wrapping it into onMounted() would help. It kind of did for the array. Now console log shows 20 for the length but now async functions don't work correctly. useFetch returns undefined when inside onMounted(). – MaxCodes Jul 18 '22 at 11:26
  • @kissu I just feel like if this is how my first Nuxt development starts, I don't know if I can stick with it. Errors like this one where the Server renders on top of the client is absolutely breaking the reason to go further with it. What if this happens when the site goes live. I'm just speaking coming from Vue where I never had an issue like that happening. Maybe it has to do with SSR having it's quirks but I've never imagined it breaking something. – MaxCodes Jul 18 '22 at 11:33
  • 1
    @kissu So I just turned off SSR with `ssr: false` and now everything works like I'm used to. I think I'll pass on SSR since this will be a web app. – MaxCodes Jul 18 '22 at 11:59
  • SSR indeed have quite some gotchas. This is why Nuxt may be a bit it hard. – kissu Jul 18 '22 at 13:36

0 Answers0