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

useScriptTag child component unload removes parent component tag when same src. #2027

Closed
7 tasks done
shtse8 opened this issue Aug 1, 2022 · 3 comments
Closed
7 tasks done

Comments

@shtse8
Copy link

shtse8 commented Aug 1, 2022

Describe the bug

load get the existing element

let el = document.querySelector<HTMLScriptElement>(`script[src="${src}"]`)

unload deletes the element

const el = document.querySelector<HTMLScriptElement>(`script[src="${src}"]`)
if (el)
document.head.removeChild(el)

If there is two component using the same script, the child component will remove the parent script.
if the component is self referencing, on child component unmounted, the parent script will be removed.

It can be addressed by managing a list of usages, the last unmounted removes the tag.

Reproduction

as above.

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04 LTS 22.04 LTS (Jammy Jellyfish)
    CPU: (8) x64 Intel(R) Xeon(R) CPU @ 2.20GHz
    Memory: 1.58 GB / 3.83 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.7.0 - ~/.nvm/versions/node/v18.7.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.7.0/bin/yarn
    npm: 8.15.1 - ~/.nvm/versions/node/v18.7.0/bin/npm
  Browsers:
    Firefox: 103.0
  npmPackages:
    @vueuse/components: ^9.0.2 => 9.0.2
    @vueuse/core: ^9.0.2 => 9.0.2
    @vueuse/motion: ^2.0.0-beta.12 => 2.0.0-beta.12
    @vueuse/nuxt: ^9.0.2 => 9.0.2

Used Package Manager

yarn

Validations

@antfu
Copy link
Member

antfu commented Aug 29, 2022

I general I think this is something you should avoid doing. Consider wrapping it with https://vueuse.org/shared/createSharedComposable/

@shtse8
Copy link
Author

shtse8 commented Aug 29, 2022

I think it's hard to avoid. For example, consider making a useFacebookSdk componsable which has useScriptTag inside
App.vue uses logPageView()
and there is a component like Sharer.vue also use useFacebookSdk, send(). They are using the same script tag.
but Sharer.vue may unload the tag when dispose.
I will try to use createdSharedComponsable. Thanks.

@stale
Copy link

stale bot commented Oct 29, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

Successfully merging a pull request may close this issue.

2 participants