Skip to content
New issue

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

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

Already on GitHub? # to your account

v-if will report an error in the vite production environment #10870

Closed
ekkotest opened this issue May 6, 2024 · 9 comments
Closed

v-if will report an error in the vite production environment #10870

ekkotest opened this issue May 6, 2024 · 9 comments
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage.

Comments

@ekkotest
Copy link

ekkotest commented May 6, 2024

Vue version

^3.4.26

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-dxqht5?file=src%2FApp.vue,src%2Fcomponents%2Ftest.svg,package.json,src%2Fmain.js&terminal=dev

Steps to reproduce

npm run preview , but in this demo i dont know how to show previw

What is expected?

no error: TypeError: Cannot read properties of null (reading 'insertBefore')
at insert (main-25115900.js:17:178)

What is actually happening?

TypeError: Cannot read properties of null (reading 'insertBefore')
at insert (main-25115900.js:17:178)
at C (main-25115900.js:13:38292)
at y (main-25115900.js:13:37900)
at N (main-25115900.js:13:44713)
at W (main-25115900.js:13:43277)
at E (main-25115900.js:13:39866)
at w (main-25115900.js:13:38654)
at y (main-25115900.js:13:38004)
at N (main-25115900.js:13:43762)
at W (main-25115900.js:13:43277)

System Info

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M2
    Memory: 214.86 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.17.1 - ~/.nvm/versions/node/v16.17.1/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v16.17.1/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.1/bin/npm
    pnpm: 8.14.1 - ~/.nvm/versions/node/v16.17.1/bin/pnpm
  Browsers:
    Chrome: 124.0.6367.119
    Safari: 17.4.1
  npmPackages:
    vue: ^3.4.26 => 3.4.26

Any additional comments?

I want to switch the v-if state after one second

@ekkotest ekkotest changed the title v-if在vite生产环境会报错 v-if will report an error in the vite production environment May 6, 2024
@edison1105
Copy link
Member

edison1105 commented May 6, 2024

duplicate of #10747?

@daniluk4000
Copy link

Finally someone reported this.

You seem to not use teleport, I don't use it as well, but I have this weird error in my code. And I have no idea why that happens honestly.

@yyx990803
Copy link
Member

Can't reproduce from your repro link. (I built the project locally with Vite)

@yyx990803 yyx990803 added need more info Further information is requested can't reproduce labels May 6, 2024
@daniluk4000
Copy link

For me I was able to reproduce randomly, in production only, and nothing in stacktrace could give a hint.

I'll try to make repro later with same template stucture. If anyone will be able to find proper repro earlier it would be nice.

@damms005
Copy link

damms005 commented May 7, 2024

tldr;
Add v-if to the parent of where things are blowing up for you

I have a monkey-patch that works for me.

In my case, in [call to insertBefore](

parent.insertBefore(child, anchor || null)
, I console logged the function arguments and inspected them. I noticed that the parentNode and parentElement property were null in the preceeding call to the same function (it was called twice)

I was sure I wasn't manipulating the parent, but the node to be inserted was an element in v-for.

So, I added a v-if to the parent. I only ensured that the v-if evaluates to true when there are items to iterate in the children v-for.

@daniluk4000
Copy link

This happens for me also in v-for so I'll try it out and write back here

@daniluk4000
Copy link

daniluk4000 commented May 7, 2024

All right this one of weirdest bugs I have experienced.

image

This is perfectly valid code. Placed inside scoped slot if this matters on few levels deep.

image

This v-if is present when it's false. Then we switch it to true.

image

Gone and crashed.

Turns out the error was in getHoursAndMinutes. It's content:

export function getHoursAndMinutes(date: number) {
    const diff = Math.abs(useStore().datetime - date) / (1000 * 60);

    return `${ (`0${ Math.floor(diff / 60) }`).slice(-2) }:${ (`0${ Math.floor(diff % 60) }`).slice(-2) }`;
}

Fixed by moving this from template to computed. I don't know what's going on here tbh, but I've tried to give you as much info as I can. Also didn't reproduce with replacing second v-if to v-show, didn't reproduce when changing function to return some random string or date.

@ekkotest
Copy link
Author

Can't reproduce from your repro link. (I built the project locally with Vite)

https://stackblitz.com/edit/vitejs-vite-dxqht5?file=src%2FApp.vue,src%2Fcomponents%2Ftest.svg,package.json,src%2Fmain.js&terminal=dev

I updated the code, and you can try it again, comment almost any other code, and the bug won't recur

@yyx990803 yyx990803 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed need more info Further information is requested can't reproduce labels Jul 11, 2024
yyx990803 added a commit that referenced this issue Jul 11, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 26, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage.
Projects
None yet
6 participants
@yyx990803 @edison1105 @damms005 @daniluk4000 @ekkotest and others