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

If called multiple times, only insert one script #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
@@ -1,5 +1,9 @@
/*! load-script2. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports = function loadScript2 (src, attrs, parentNode) {
const node = parentNode || document.head || document.getElementsByTagName('head')[0]
const existingScript = node.querySelector(`:scope > script[src="${src}"]`)
if (existingScript) return existingScript
Copy link
Author

@caub caub Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feross this allows to cache the added script

it can be useful when you render more than once a component that uses loadScript, for example a modal component


return new Promise((resolve, reject) => {
const script = document.createElement('script')
script.async = true
Expand All @@ -19,7 +23,6 @@ module.exports = function loadScript2 (src, attrs, parentNode) {
reject(new Error(`Failed to load ${src}`))
}

const node = parentNode || document.head || document.getElementsByTagName('head')[0]
node.appendChild(script)
})
}