vue-gtm icon indicating copy to clipboard operation
vue-gtm copied to clipboard

Fix return gtmPlugin from app context

Open filiphazardous opened this issue 1 year ago • 5 comments

I have a problem with Vite inline, which in production mode just copies the useGtm function wholesale to the calling file, without copying the scope. This fix solves that problem, since it always fetches the gtm instance from the vue app context

filiphazardous avatar Feb 07 '24 07:02 filiphazardous

Here's the bug in nuxt-gtm that this would solve (at least in my use-case): https://github.com/zadigetvoltaire/nuxt-gtm/issues/14

filiphazardous avatar Feb 07 '24 08:02 filiphazardous

Looks like getCurrentInstance is there since Vue v3.0.0. So I guess this is safe.

https://github.com/vuejs/core/commit/9c0f820a8e4a934227175b2911090658064d6017

Shinigami92 avatar Feb 07 '24 18:02 Shinigami92

Is it possible to add a test for this? So we can prevent regression?

Shinigami92 avatar Feb 07 '24 18:02 Shinigami92

Is it possible to add a test for this? So we can prevent regression?

I'll see if I can come up with a way to test it. What triggered it in our case was two levels of indirection (it was initialized through nuxt-gtm and useGtm was called directly in our project, so I guess Vite couldn't connect the dots). I'll see if I can reproduce a similar sandbox.

filiphazardous avatar Feb 08 '24 09:02 filiphazardous

Is it possible to add a test for this? So we can prevent regression?

I'll see if I can come up with a way to test it. What triggered it in our case was two levels of indirection (it was initialized through nuxt-gtm and useGtm was called directly in our project, so I guess Vite couldn't connect the dots). I'll see if I can reproduce a similar sandbox.

Maybe spy or mocks can help to simplify it

Shinigami92 avatar Feb 08 '24 09:02 Shinigami92