Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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)
})
}