Skip to content

Commit

Permalink
Avoid loading multiple instances of the twitter script when used in a…
Browse files Browse the repository at this point in the history
… v-for
  • Loading branch information
DannyFeliz committed Mar 29, 2024
1 parent 4d1c6e5 commit a7c8147
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/vue-tweet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,40 @@ function getTweetParams() {
}
function addScript(src: string, cb: () => void): void {
if (window['___$twitterScriptLoaded___'] === undefined) {
window['___$twitterScriptLoaded___'] = false;
}
if (window['___$twitterScriptLoaded___']) {
cb();
return;
}
if (window['___$twitterScriptLoading___'] === undefined) {
window['___$twitterScriptLoading___'] = false;
}
if (window['___$twitterScriptLoading___']) {
// If the script is currently being loaded, set up an interval to wait until it's loaded
const waitInterval = setInterval(() => {
if (window['___$twitterScriptLoaded___']) {
clearInterval(waitInterval);
cb();
}
}, 100);
return;
}
window['___$twitterScriptLoading___'] = true;
const s = document.createElement("script");
s.setAttribute("src", src);
s.addEventListener("load", () => cb(), false);
s.async = true;
s.addEventListener("load", () => {
window['___$twitterScriptLoaded___'] = true;
window['___$twitterScriptLoading___'] = false;
cb(); // Call the callback once the script is successfully loaded
}, false);
document.body.appendChild(s);
}
</script>

0 comments on commit a7c8147

Please sign in to comment.